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"
      }
    } 
  }
}
相关推荐
武子康1 小时前
大数据-238 离线数仓 - 广告业务 Hive分析实战:ADS 点击率、购买率与 Top100 排名避坑
大数据·后端·apache hive
武子康1 天前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天1 天前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
Elasticsearch2 天前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
Elasticsearch3 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
武子康3 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康4 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库5 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟5 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体