前缀搜索


一、前缀搜索

1、概念:以xx开头的搜索,前缀搜索不计算相关度评分

2、测试数据:

 1 POST /my_index/_bulk?filter_path=items.*.error
 2 {"index":{"_id":"1"}}
 3 {"text":"城管打电话喊商贩去摆摊摊"}
 4 {"index":{"_id":"2"}}
 5 {"text":"笑果文化回应商贩老农去摆摊"}
 6 {"index":{"_id":"3"}}
 7 {"text":"老农耗时17年种出椅子树"}
 8 {"index":{"_id":"4"}}
 9 {"text":"夫妻结婚30多年AA制,被城管抓"}
10 {"index":{"_id":"5"}}
11 {"text":"黑人见义勇为阻止抢劫反被铐住"}

查询以城管开头的数据:

 1 GET my_index/_search
 2 {
 3   "query": {
 4     "prefix": {
 5       "text": {
 6         "value": "城管"        如果my_index索引为dynamic_mapping,没有设置中文分词器是查不到数据的。
 7       }                       因为前缀搜索是搜索切词后词项中包含城管的数据,如果分词结果中没有城管则搜索不到。所以在设置mapping时要指定分词器
 8     }
 9   }
10 }

二、通配符

1、使用*匹配零个或多个字符,语法与prefix相同。同样也是匹配term分词后的数据,而不是直接匹配字段中的数据

2、

 1 GET my_index/_search
 2 {
 3   "query": {
 4     "wildcard": {
 5       "text": {
 6         "value": "eng*ish"
 7       }
 8     }
 9   }
10 }

三、正则表达式

1 GET product_en/_search
2 {
3   "query": {
4     "regexp": {
5       "title": "[\\s\\S]*nfc[\\s\\S]*"
6     }
7   }
8 }

相关