Terms Query 词项查询
Terms Query 能够在结构化的数据中基于精确的值取查询文档。如日期范围,ip地址,价格和产品ID等数据。
和全文检索不同,term-level 查询不会分析“检索词”,会匹配存储字段中的准确的词项。
term-level 查询分类
exists query
f返回在指定field上包含被索引值的文档
被索引的值在以下情况下可能不会存在文档属性中
- 在原json文档中,field的值为null 或 []
- mapping 设置中,该field 配置为 index :false
- 属性长度值超出了 mapping中的
ignore_above
配置
curl -X GET "localhost:9200/myindex/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "exists": { "field": "user" } } } '
exists 顶级参数
field (required)想要搜索的属性名
属性值为 Json 中的 的 null 和[] 时,被认为不存在,以下类型的值认为是存在的
- 空字符串, 如 “” 或 “-”
- 数组中包含null 和其它类型的值 ,如[null, “foo”]
- 自定义的
null-value
, 定义在mapping field中
取查询没有索引值的文档,可以在 bool 查询中 must_not 联合 exists 使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must_not": { "exists": { "field": "user.id" } } } } } '
fuzzy query
模糊查询,查询属性和检索项相似的文档,和wildcard query 不同。使用 Levenshtein edit distance 来去判断是否匹配。
一个编辑距离是 一个词项转换为另一个词项时,所欲需要改变的字符数量,如:
- 变更一个字符 (box → fox)
- 溢出一个字符 (black → lack)
- 插入一个字符 (sic → sick)
- 交换两个相邻的字符 (act → cat)
为了查找相似词项,fuzzy query 会根据 “检索项”已 edit distance去 创建一系列所有可能的变种,扩展项。查询会返回符合扩展项的精确匹配项。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "fuzzy": { "user.id": { "value": "ki", "fuzziness": "AUTO", "max_expansions": 50, "prefix_length": 0, "transpositions": true, "rewrite": "constant_score" } } } } '
fuzzy 顶级属性
field 需要在哪个属性上查找。
field 参数
- value :用于搜索的检索项
- fuzziness 最大的edit distance
- max_expansions 创建的检索项的最大的变种,默认50个。
- prefix_length 创建变种时,不参与变种创建的字符数,默认0。
- transpositions 是否开启调整相邻字符去匹配。
- rewrite
若要开启 fuzzy queries,search.allow_expensive_queries
需要开启。
ids query
根据文档id去查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "ids" : { "values" : ["1", "4", "100"] } } } '
prefix query
查询在指定属性上包含指定前缀的文档
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "prefix": { "user.id": { "value": "ki" } } } } '
case_insensitive 7.10 新增, true or false
range query
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "range": { "age": { "gte": 10, "lte": 20, "boost": 2.0 } } } } '
term query
精确匹配文档属性,类似于sql 中的“=”
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "term": { "user.id": { "value": "kimchy", "boost": 1.0 } } } } '
应该避免使用term query 去查询属性类型为 text的属性,因为类型为text的field在存储时,会被分析器处理,分词等操作。搜索 text的属性时,使用match query 是更好的选择,match query 也会对 “检索项”进行分析操作,能更好的匹配操作文档。
term query 不会对检索项进行分词等处理,只会精确匹配属性值。
为了明白term query 和match query 之间的区别,可以参考以下示例
1. 创建索引,包含一个属性类型为text
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d' { "mappings": { "properties": { "full_text": { "type": "text" } } } } '
2. 索引文档 ”Quick Brown Foxes!“, 由于full_text类型为 text, elasticsearch 在分析文档时将起转变为 [quick, brown, fox]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X PUT "localhost:9200/my-index-000001/_doc/1?pretty" -H 'Content-Type: application/json' -d' { "full_text": "Quick Brown Foxes!" } '
3. 使用term在 full_text 属性上检索 ”Quick Brown Foxes!“,由于 full_text 不再包含精确的词项”Quick Brown Foxes!“,查询不会匹配任何文档
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/my-index-000001/_search?pretty&pretty" -H 'Content-Type: application/json' -d' { "query": { "term": { "full_text": "Quick Brown Foxes!" } } } '
4. 使用match 查询文档,可以正确返回文档
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/my-index-000001/_search?pretty&pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "full_text": "Quick Brown Foxes!" } } } '
terms query
在指定的文档上,检索一个或多个给定的 term,可以同时在一个属性上检索多个值,类似于 sql 中的 in ("a", "b")
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "terms": { "user.id": [ "kimchy", "elkbee" ], "boost": 1.0 } } } '
Terms set query
term_set 查询 和 terms 查询类似,不过 term_set 可以指定最少需要匹配多少个词项才算是匹配, 如在属性 programming_languages 上进行 term_set 查询 [ c++
, java
, php
],可以要求 返回的文档的programming_languages 中字少包含两个词项
wildcard query
通配符匹配,可以使用*作为通配符去匹配文档, ” * “ 代表0或多个字符,”?“代表一个字符。 使用该功能,需要 search.allow_expensive_queries
参数为打开状态
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "wildcard": { "user.id": { "value": "ki*y", "boost": 1.0, "rewrite": "constant_score" } } } } '