当前位置: 首页 > news >正文

es-09模糊查询

模糊查询

  • 前缀搜索:prefix

    概念:以xx开头的搜索,不计算相关度评分。
    注意:
    • 前缀搜索匹配的是term,而不是field。
    • 前缀搜索的性能很差
    • 前缀搜索没有缓存
    • 前缀搜索尽可能把前缀长度设置的更长
    语法:
    GET <index>/_search
    {"query": {"prefix": {"<field>": {"value": "<word_prefix>"}}}
    }
    index_prefixes: 默认   "min_chars" : 2,   "max_chars" : 5 
    
#prefix: 前缀搜索
DELETE my_index
# elasticsearch stack
# elasticsearch search
# el
# ela 
# elas elasticsearch
PUT my_index
{"mappings": {"properties": {"text": {"analyzer": "ik_max_word","type": "text","index_prefixes":{"min_chars":2,"max_chars":4},"fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}
}
GET my_index/_mapping
POST /my_index/_bulk?filter_path=items.*.error
{"index":{"_id":"1"}}
{"text":"城管打电话喊商贩去摆摊摊"}
{"index":{"_id":"2"}}
{"text":"笑果文化回应商贩老农去摆摊"}
{"index":{"_id":"3"}}
{"text":"老农耗时17年种出椅子树"}
{"index":{"_id":"4"}}
{"text":"夫妻结婚30多年AA制,被城管抓"}
{"index":{"_id":"5"}}
{"text":"黑人见义勇为阻止抢劫反被铐住"}
GET my_index/_search
GET my_index/_mapping
GET _analyze
{"text": ["夫妻结婚30多年AA制,被城管抓"]
}
GET my_index/_search
{"query": {"prefix": {"text": {"value": "城管"}}}
}
# 通配符
DELETE my_index
POST /my_index/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }
DELETE product_en
POST /product_en/_bulk
{ "index": { "_id": "1"} }
{ "title": "my english","desc" :  "shouji zhong de zhandouji","price" :  3999, "tags": [ "xingjiabi", "fashao", "buka", "1"]}
{ "index": { "_id": "2"} }
{ "title": "xiaomi nfc phone","desc" :  "zhichi quangongneng nfc,shouji zhong de jianjiji","price" :  4999, "tags": [ "xingjiabi", "fashao", "gongjiaoka" , "asd2fgas"]}
{ "index": { "_id": "3"} }
{ "title": "nfc phone","desc" :  "shouji zhong de hongzhaji","price" :  2999, "tags": [ "xingjiabi", "fashao", "menjinka" , "as345"]}
{ "title": { "_id": "4"} }
{ "text": "xiaomi erji","desc" :  "erji zhong de huangmenji","price" :  999, "tags": [ "low", "bufangshui", "yinzhicha", "4dsg" ]}
{ "index": { "_id": "5"} }
{ "title": "hongmi erji","desc" :  "erji zhong de kendeji","price" :  399, "tags": [ "lowbee", "xuhangduan", "zhiliangx" , "sdg5"]}
GET my_index/_search
GET product_en/_searchGET my_index/_search
{"query": {"wildcard": {"text.keyword": {"value": "my eng*ish"}}}
}
GET product_en/_mapping
#exact value
GET product_en/_search
{"query": {"wildcard": {"tags.keyword": {"value": "men*inka"}}}
}
#正则
GET product_en/_search
GET product_en/_search
{"query": {"regexp": {"title": "[\\s\\S]*nfc[\\s\\S]*"}}
}
GET product_en/_search
GET product_en/_search
{"query": {"regexp": {"desc": {"value": "zh~dng","flags": "COMPLEMENT"}}}
}
GET product_en/_search
{"query": {"regexp": {"tags.keyword": {"value": ".*<2-3>.*","flags": "INTERVAL"}}}
}
flags
  • ALL

    启用所有可选操作符。

  • COMPLEMENT

    启用操作符。可以使用对下面最短的模式进行否定。例如

    a~bc # matches ‘adc’ and ‘aec’ but not ‘abc’

  • INTERVAL

    启用<>操作符。可以使用<>匹配数值范围。例如

    foo<1-100> # matches ‘foo1’, ‘foo2’ … ‘foo99’, ‘foo100’

    foo<01-100> # matches ‘foo01’, ‘foo02’ … ‘foo99’, ‘foo100’

  • INTERSECTION

    启用&操作符,它充当AND操作符。如果左边和右边的模式都匹配,则匹配成功。例如:

    aaa.+&.+bbb # matches ‘aaabbb’

  • ANYSTRING

    启用@操作符。您可以使用@来匹配任何整个字符串。
    您可以将@操作符与&和~操作符组合起来,创建一个“everything except”逻辑。例如:

    @&~(abc.+) # matches everything except terms beginning with ‘abc’

  • 模糊查询:fuzzy

    混淆字符 (box → fox) 缺少字符 (black → lack)

    多出字符 (sic → sick) 颠倒次序 (act → cat)

    语法
    GET <index>/_search
    {"query": {"fuzzy": {"<field>": {"value": "<keyword>"}}}
    }
    
# fuzzy:模糊查询
GET product_en/_search
GET product_en/_search
{"query": {"fuzzy": {"desc": {"value": "quangongneng nfc","fuzziness": "2"}}}
}GET product_en/_search
{"query": {"match": {"desc": {"query": "nfe quasdasdasdasd","fuzziness": 1}}}
}
参数:
# match_phrase_prefix
GET product_en/_search
{"query": {"match_phrase": {"desc": "shouji zhong de"}}
}GET product_en/_search
{"query": {"match_phrase_prefix": {"desc": {"query": "de zhong shouji hongzhaji","max_expansions": 50,"slop":3}}}
}GET product_en/_search
{"query": {"match_phrase_prefix": {"desc": {"query": "zhong hongzhaji","max_expansions": 50,"slop": 3}}}
}# source: zhong de hongzhaji
# query:  zhong >  hongzhaji# source: shouji zhong de hongzhaji
# query:  de zhong shouji hongzhaji# de shouji/zhong  hongzhaji  1次
# shouji/de zhong  hongzhaji  2次
# shouji zhong/de  hongzhaji  3次
# shouji zhong de  hongzhaji  4

#ngram min_gram =1 “max_gram”: 2

GET _analyze
{
“tokenizer”: “ik_max_word”,
“filter”: [ “edge_ngram” ],
“text”: “reba always loves me”
}

#min_gram =1 “max_gram”: 1
#r a l m

#min_gram =1 “max_gram”: 2
#r a l m
#re al lo me

#min_gram =2 “max_gram”: 3
#re al lo me
#reb alw lov me

PUT my_index
{
“settings”: {
“analysis”: {
“filter”: {
“2_3_edge_ngram”: {
“type”: “edge_ngram”,
“min_gram”: 2,
“max_gram”: 3
}
},
“analyzer”: {
“my_edge_ngram”: {
“type”:“custom”,
“tokenizer”: “standard”,
“filter”: [ “2_3_edge_ngram” ]
}
}
}
},
“mappings”: {
“properties”: {
“text”: {
“type”: “text”,
“analyzer”:“my_edge_ngram”,
“search_analyzer”: “standard”
}
}
}
}
GET /my_index/_mapping

POST /my_index/_bulk
{ “index”: { “_id”: “1”} }
{ “text”: “my english” }
{ “index”: { “_id”: “2”} }
{ “text”: “my english is good” }
{ “index”: { “_id”: “3”} }
{ “text”: “my chinese is good” }
{ “index”: { “_id”: “4”} }
{ “text”: “my japanese is nice” }
{ “index”: { “_id”: “5”} }
{ “text”: “my disk is full” }

GET /my_index/_search
GET /my_index/_mapping
GET /my_index/_search
{
“query”: {
“match_phrase”: {
“text”: “my eng is goo”
}
}
}

PUT my_index2
{
“settings”: {
“analysis”: {
“filter”: {
“2_3_grams”: {
“type”: “edge_ngram”,
“min_gram”: 2,
“max_gram”: 3
}
},
“analyzer”: {
“my_edge_ngram”: {
“type”:“custom”,
“tokenizer”: “standard”,
“filter”: [ “2_3_grams” ]
}
}
}
},
“mappings”: {
“properties”: {
“text”: {
“type”: “text”,
“analyzer”:“my_edge_ngram”,
“search_analyzer”: “standard”
}
}
}
}
GET /my_index2/_mapping
POST /my_index2/_bulk
{ “index”: { “_id”: “1”} }
{ “text”: “my english” }
{ “index”: { “_id”: “2”} }
{ “text”: “my english is good” }
{ “index”: { “_id”: “3”} }
{ “text”: “my chinese is good” }
{ “index”: { “_id”: “4”} }
{ “text”: “my japanese is nice” }
{ “index”: { “_id”: “5”} }
{ “text”: “my disk is full” }

GET /my_index2/_search
{
“query”: {
“match_phrase”: {
“text”: “my eng is goo”
}
}
}

GET _analyze
{
“tokenizer”: “ik_max_word”,
“filter”: [ “ngram” ],
“text”: “用心做皮肤,用脚做游戏”
}

http://www.lryc.cn/news/19425.html

相关文章:

  • 57 - 深入解析任务调度
  • CAN总线开发一本全(3) - 微控制器集成的FlexCAN外设
  • Elasticsearch7.8.0版本进阶——段合并
  • Java版贪食蛇游戏
  • 2023年度数学建模竞赛汇总
  • 了解Python语言和版本
  • nvm (node版本管理工具)安装的详细步骤,并解决安装过程中遇到的问题
  • 朴素贝叶斯笔记
  • 【GUI】用于电动助力车性能分析的GUI(Matlab代码实现)
  • Android:反编译apk踩坑/apktool/dex2jar/JDGUI
  • React 跨域的配置
  • Elasticsearch7.8.0版本进阶——持久化变更
  • CF Edu 127 A-E vp补题
  • 剑指 Offer 05. 替换空格
  • 通过操作Cortex-A7核,串口输入相应的命令,控制LED灯进行工作
  • Python实现某du文库vip内容下载,保存成PDF
  • vue3.0 模板语法
  • 【GlobalMapper精品教程】054:标签(标注)功能案例详解
  • 超详细树状数组讲解(+例题:动态求连续区间和)
  • 【学习笔记】AGC055
  • 墨者——内部文件上传系统漏洞分析溯源 内部文件上传系统漏洞分析溯源
  • 5.2 Python if语句
  • ubuntu gerrit 配置
  • 运动蓝牙耳机什么牌子好,运动蓝牙耳机品牌推荐
  • (7)C#传智:方法及参数、重载(第7天)
  • Python 函数式编程
  • pandas读取EXCEL列名重复问题解决——pandas设置多行为列名(多层列名)
  • CMake常用语法
  • Java知识复习(一)基础知识
  • springboot+vue.js校园车辆用车预约管理系统