ES 搜索—— 常用查询语句

Posted 猪朵朵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ES 搜索—— 常用查询语句相关的知识,希望对你有一定的参考价值。

match_all query
#匹配所有文档,得分全为1.
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "match_all": {} } }
 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 

match查询类似,但用于匹配精确短语或单词近似匹配

GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : "this is a test"
        }
    }
}
#指定分词器
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "match_phrase" : { "message" : { "query" : "this is a test", "analyzer" : "my_analyzer" } } } }

 match_phrase_prefix

match_phrase查询一样,但在最后一个单词上做了通配符搜索。

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     查询与任何字段匹配的文档  _score来自最佳字段的文档

默认type

most_fields    查询与任何字段匹配的文档  _score来自每个字段的文档

cross_fields   不能使用模糊查询

phrase           在每个字段上执行match_phrase查询,_score来自每个字段

phrase_prefix 在每个字段上执行phrase_match_phrase查询,_score来自每个字段

#query String  和 query field
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query": "this is a test", "fields": [ "subject", "message" ] } } }
#包含通配符的多字段查询 query fields 为:title,first_name,last_name
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query": "Will Smith", "fields": [ "title", "*_name" ] } } }
#subject字段的重要性是message字段的三倍
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query" : "this is a test", "fields" : [ "subject^3", "message" ] } } }
#通常best_fields得分为最佳匹配文档得分,如果指定了tie_breaker,则加上tie_breaker*_score for all
other match field。也可以指定其他参数:analyzerboostoperatorminimum_should_matchfuzziness
lenientprefix_lengthmax_expansionsrewritezero_terms_querycutoff_frequency,
 auto_generate_synonyms_phrase_query and fuzzy_transpositions
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query": "brown fox", "type": "best_fields", "fields": [ "subject", "message" ], "tie_breaker": 0.3 } } }
#相当于执行

curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d‘
{
  "query": {
     "dis_max": {
         "queries": [
              { "match": { "subject": "brown fox" }},
              { "match": { "message": "brown fox" }}
          ],
     "tie_breaker": 0.3
    }
  }
}

#operator为and要求所有字段都要匹配
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query": "Will Smith", "type": "best_fields", "fields": [ "first_name", "last_name" ], "operator": "and" } } }
#相当于执行
(+first_name:will +first_name:smith) | (+last_name:will +last_name:smith)
#得分计算:每个match子句的分数相加,再除以match子句的个数
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "multi_match" : { "query": "quick brown fox", "type": "most_fields", "fields": [ "title", "title.original", "title.shingles" ] } } } #相当于执行 curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "bool": { "should": [ { "match": { "title": "quick brown fox" }}, { "match": { "title.original": "quick brown fox" }}, { "match": { "title.shingles": "quick brown fox" }} ] } } }
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 给定一个更高的相关性分值

term查询确切匹配倒排索引
match相关匹配
保存数据时,
映射类型为text会按分词器分词结果存储在倒排索引中
映射类型为keyword 不按分词器分词,整个词直接存储在倒排索引中。
例如:
https://www.elastic.co/guide/en/elasticsearch/
reference/6.1/query-dsl-term-query.html
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"
            }
        }
    }
}
#根据脚本指定至少匹配的文档数字段
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_script": { "source": "Math.min(params.num_terms, doc[‘required_matches‘].value)" } } } } }
4. range query  

TermRangeQuery 针对string字段

NumericRangeQuery 针对数据、日期字段

#gte大于等于 gt大于 lte小于等于 lt小于
curl -XGET ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d{ "query": { "range" : { "age" : { "gte" : 10, "lte" : 20, "boost" : 2.0 } } } }

查询日期范围时,可以使用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"
            }
        }
    }
}

 

   
   

以上是关于ES 搜索—— 常用查询语句的主要内容,如果未能解决你的问题,请参考以下文章

ES问题

ES问题

es 的搜索

使用es的head插件进行简单搜索

Elasticsearch慢查询故障诊断

es深入搜索之部分匹配