ES 搜索(5)—— 常用查询语句
match_all query |
#匹配所有文档,得分全为1. |
boost改变得分 |
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": { "boost" : 1.2 } } } ' |
不匹配任何文档 |
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_none": {} } } ' |
2.全文查询 full text query 执行查询之前先分析查询字符串 通常是文本字段查询 | |
match_query 执行全文查询的标准查询,包括模糊匹配和短语或近似查询 |
GET /_search { "query": { "match" : { "message" : "this is a test" } } } |
match_phrase 与 |
GET /_search { "query": { "match_phrase" : { "message" : "this is a test" } } } #指定分词器 |
match_phrase_prefix
|
GET /_search { "query": { "match_phrase_prefix" : { "message" : "quick brown f" } } } #max_expansions控制可接受的后缀的数量,如10 返回10个结果
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_phrase_prefix" : { "message" : { "query" : "quick brown f", "max_expansions" : 10 } } } } ' |
multi_match query 多字段查询 如果没有指定query field,查询时按照index.query.default_field默认字段查询。 一次查询最多支持1024个字段 multi_match的查询类型包括: best_fields 查询与任何字段匹配的文档 (默认type) most_fields 查询与任何字段匹配的文档 cross_fields 不能使用模糊查询 phrase 在每个字段上执行match_phrase查询,_score来自每个字段 phrase_prefix 在每个字段上执行phrase_match_phrase查询,_score来自每个字段 |
#query String 和 query field #包含通配符的多字段查询 query fields 为:title,first_name,last_name #subject字段的重要性是message字段的三倍 #通常best_fields得分为最佳匹配文档得分,如果指定了tie_breaker,则加上tie_breaker*_score for all curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' #operator为and要求所有字段都要匹配 #得分计算:每个match子句的分数相加,再除以match子句的个数 curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "multi_match" : { "query": "Will Smith", "type": "cross_fields", "fields": [ "first_name", "last_name" ], "operator": "and" } } } ' #相当于 +(first_name:will last_name:will) +(first_name:smith last_name:smith)
|
common terms query 将查询词分为两种: 1.重要的 (more important low frequency) 3.不重要的(less important high frequency)通常为stopwords minimum_should_match:2//默认为低频词 |
GET /_search { "query": { "common": { "body": { "query": "this is bonsai cool", "cutoff_frequency": 0.001 } } } } curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "common": { "body": { "query": "nelly the elephant not as a cartoon", "cutoff_frequency": 0.001, "minimum_should_match": { "low_freq" : 2, "high_freq" : 3 } } } } } ' #相当于 curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "bool": { "should": [ { "term": { "body": "nelly"}}, { "term": { "body": "elephant"}}, { "term": { "body": "cartoon"}} ], "minimum_should_match": 2 } }, "should": { "bool": { "should": [ { "term": { "body": "the"}}, { "term": { "body": "not"}}, { "term": { "body": "as"}}, { "term": { "body": "a"}} ], "minimum_should_match": 3 } } } } } ' |
query_string |
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "query_string" : { "default_field" : "content", "query" : "this AND that OR thus" } } } ' curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "query_string" : { "fields" : ["content", "name"], "query" : "this AND that" } } } ' curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "query_string" : { "fields" : ["city.*"], "query" : "this AND that OR thus" } } } ' |
3.term query 按照存储在倒排索引中的确切词进行操作 常用于数字, 日期和枚举等结构化数据,而不是全文本字段 |
|
term query 查询特定字段中包含的特定术语的文档 boost 给定一个更高的相关性分值 |
curl -XPOST 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "term" : { "user" : "Kimchy" } } } ' curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "should": [ { "term": { "status": { "value": "urgent", "boost": 2.0 } } }, { "term": { "status": "normal" } } ] } } } '
|
terms query 查询符合任何一个term的文档 过滤机制: 可指定参数:index、type、id、path、routing |
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "terms" : { "user" : ["kimchy", "elasticsearch"]} } } ' curl -XPUT 'localhost:9200/users/user/2?pretty' -H 'Content-Type: application/json' -d' { "followers" : ["1", "3"] } ' curl -XPUT 'localhost:9200/tweets/tweet/1?pretty' -H 'Content-Type: application/json' -d' { "user" : "1" } ' curl -XGET 'localhost:9200/tweets/_search?pretty' -H 'Content-Type: application/json' -d' { "query" : { "terms" : { "user" : { "index" : "users", "type" : "user", "id" : "2", "path" : "followers" } } } } '
|
terms_set query |
curl -XPUT 'localhost:9200/my-index?pretty' -H 'Content-Type: application/json' -d' { "mappings": { "doc": { "properties": { "required_matches": { "type": "long" } } } } } ' curl -XPUT 'localhost:9200/my-index/doc/1?refresh&pretty' -H 'Content-Type: application/json' -d' { "codes": ["ghi", "jkl"], "required_matches": 2 } ' curl -XPUT 'localhost:9200/my-index/doc/2?refresh&pretty' -H 'Content-Type: application/json' -d' { "codes": ["def", "ghi"], "required_matches": 2 } ' #可根据minimum_should_match_field参数指定至少匹配的文档数字段
curl -XGET 'localhost:9200/my-index/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "terms_set": { "codes" : { "terms" : ["abc", "def", "ghi"], "minimum_should_match_field": "required_matches" } } } } ' #根据脚本指定至少匹配的文档数字段 |
4. range query | |
TermRangeQuery 针对string字段 NumericRangeQuery 针对数据、日期字段 |
#gte大于等于 gt大于 lte小于等于 lt小于 |
查询日期范围时,可以使用date math表达式 日期格式 时区问题 |
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "range" : { "date" : { "gte" : "now-1d/d", "lt" : "now/d" } } } } ' curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "range" : { "born" : { "gte": "01/01/2012", "lte": "2013", "format": "dd/MM/yyyy||yyyy" } } } } ' #2015-01-01 00:00:00将转为2014-12-31T23:00:00 UTC ;now不受时区影响
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "range" : { "timestamp" : { "gte": "2015-01-01 00:00:00", "lte": "now", "time_zone": "+01:00" } } } } '
|