ES-模糊查询

模糊查询

1 wildcard

  • 准备数据
bash 复制代码
POST demolike/_bulk
{
  "index": {
    "_id": "1"
  }
}
{
  "text": "草莓熊是个大坏蛋"
}
{
  "index": {
    "_id": "2"
  }
}
{
  "text": "wolf 也是一个坏蛋"
}
{
  "index": {
    "_id": "3"
  }
}
{
  "text": "我们一起去看小姐姐"
}
{
  "index": {
    "_id": "4"
  }
}
{
  "text": "真相只有一个"
} 
  • 使用案例
bash 复制代码
GET demolike/_search  
{
  "query": {
    "wildcard": {
      "text.keyword": {
        "value": "*坏蛋*"
      }
    }
  }
}

GET demolike/_search  
{
  "query": {
    "wildcard": {
      "text.keyword": {
        "value": "*个*"
      }
    }
  }
}


  • 正则
bash 复制代码
GET demolike/_search
{
  "query":{
    "regexp": {
        "text": "[\\s\\S]*是[\\s\\S]*"
    }
  }
}

-fuzzy(更适合用于生产环境)

拥有纠错的能力

bash 复制代码
POST demolikefu/_bulk
{"index":{"_id":"1"}}
{"text":"hello cat"}
{"index":{"_id":"2"}}
{"text":"hello fdsaf"}
{"index":{"_id":"3"}}
{"text":"hello cfasat"}
GET demolikefu/_search
{
  "query": {
    "fuzzy": {
      "text": {
        "value": "act", #fuzzy 会进行纠错
        "fuzziness": 1, #编辑距离 也就是可以进行多少次操作变成正确的字符 act -> cat c和a 交换就可已变成cat 编辑距离为1
        "transpositions": true #es 里面有两种算法 老算法:认为ac都移动了   新算法:交换只算移动了一次 false 是老算法 true是新算法
      }
    }
  }
}
  • 前缀搜索
bash 复制代码
GET demolikefu/_search
{
  "query":{
    "match_phrase_prefix": {
      "text": "zhangsan and l" #会搜索出 zhangsan and list
    }
  }
}
#这个也是分词的 会搜索分词后的
GET demolike/_search
{
  "query":{
    "prefix": {
      "text": {
        "value": "是" #如果要搜整个句子 用 text.keyword
      }
    }
  }
}
  • ngram
    性能会比 fuzzy 好,但是ngram会浪费空间,如果是要追求极致的性能一般使用ngram
bash 复制代码
PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "2_3_ngram": {
          "type": "ngram",
          "min_gram": 2, #最小
          "max_gram": 3  #最大  比如she经过这个作用 sh he she 等
        }
      },
      "analyzer": {
        "my_ngram": {
          "type": "custom",
          "filter": "2_3_ngram", #这个是在分词的基础上对每个单词进行分词
          "tokenizer": "standard" #这个是分词的 比如 hello world 分为 hello和world
        }
      }
    }
  },
  "mappings": { #建立索引的时候一般就默认 流量特别大的时候更合适用这个自定义的方式创建索引
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "my_ngram", #存储的时候怎么切分
        "search_analyzer": "standard" #查询语句怎么切分
      }
    }
  }
}
  • edge_ngram
bash 复制代码
put my_index
{
  "settings":{
    "analysis":{
      "filter":{
        "2_3_ngram":{
          "type":"ngram",
          "min_gram":2,
          "max_gram":3
        }
      },
      "analyzer":{
        "my_ngram":{
          "type":"custom",
          "filter":"2_3_ngram",
          "tokenizer":"standard"
        }
      }
    }
  },
  "mappings":{
    "properties":{
      "text":{
        "type":"text",
        "analyzer":"my_ngram",
        "search_analyzer":"standard"
      }
    }
  }
}

从左向右切分,比ngram 切分的数量更少。

  • suggest
bash 复制代码
POST product_suggest/_bulk
{"index":{"_id" : 1}}
{"text":"你是一个小笨蛋"}
{"index":{"_id" : 2}}
{"text":"疯狂学习中"}
{"index":{"_id" : 3}}
{"text":"来呀摆烂躺平呀"}
{"index":{"_id" : 4}}
{"text":"我真的好想成为优秀的工程师"}


 PUT product_suggest
 {
   "mappings":{
     "properties":{
       "text":{
         "type":"text",
         "analyzer":"ik_smart",
         "fields":{
           "suggest":{
             "type":"completion", #补全
              "analyzer":"ik_smart"
           }
         }
       },
       "content":{
         "type":"text",
         "analyzer":"ik_smart"
       }
     }
   }
 }
 #推荐补全
GET product_suggest/_search
{
  "suggest":{
    "my_suggest":{
      "prefix":"我", #suggest 中prefix是性能最好的
      "completion":{
        "field":"text.suggest"
      }
    } 
  }
}
相关推荐
tan180°1 小时前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
2301_781668611 小时前
Elasticsearch 02
大数据·elasticsearch·搜索引擎
isfox2 小时前
Google GFS 深度解析:分布式文件系统的开山之作
大数据·hadoop
用户Taobaoapi20142 小时前
京东店铺所有商品API技术开发文档
大数据·数据挖掘·数据分析
LaughingZhu2 小时前
Product Hunt 每日热榜 | 2025-09-07
人工智能·经验分享·搜索引擎·产品运营
在未来等你3 小时前
Kafka面试精讲 Day 8:日志清理与数据保留策略
大数据·分布式·面试·kafka·消息队列
江畔独步3 小时前
Flink TaskManager日志时间与实际时间有偏差
大数据·flink
TDengine (老段)4 小时前
TDengine 选择函数 Last() 用户手册
大数据·数据库·sql·物联网·时序数据库·tdengine·涛思数据
TDengine (老段)5 小时前
TDengine 选择函数 First 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
沧海一粟青草喂马6 小时前
抖音批量上传视频怎么弄?抖音矩阵账号管理的专业指南
大数据·人工智能·矩阵