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"
      }
    } 
  }
}
相关推荐
weishuangyun1233 小时前
微信小程序怎么选公司?上线流程详解
大数据·小程序
小岐AI观9 小时前
智赋岐黄适合养生馆吗?
大数据·人工智能·物联网
大大大大晴天9 小时前
SR不只查内部表:External Catalog、联邦查询与统一分析
大数据
anxiao_m11 小时前
2026制造业云桌面选型攻略,不同生产场景适配方案汇总
大数据·网络·数据库
土星云SaturnCloud11 小时前
产线SOP动作级AI监管方案:土星云SE110S-WC8赋能合规识别、预警与效率分析
大数据·服务器·人工智能·ai·边缘计算
牛企老板俱乐部12 小时前
广东机器人结构验证手板产业格局:深圳珠海东莞三城分析
大数据·人工智能
Keystone_Onion12 小时前
基于5大仓群路由算法的中大件美国海外仓降本架构与数据实测分析
大数据
buligbulig13 小时前
智能电网的数据底座:电力行业大数据治理路径解析
大数据
ltqvibe13 小时前
企业AI改造的四层路径:从点上应用到企业大脑
大数据·人工智能·本体语义平台·企业ai改造·数据打通·企业大脑·企业认知基础设施
anxiao_m13 小时前
园区数字孪生怎么搭建?主流方案与服务商深度对比
大数据·运维·人工智能