ElasticSearch查询

一:查询语句的结构

典型结构:
{
    QUERY_NAME: {
        ARGUMENT: VALUE,
        ARGUMENT: VALUE,...
    }
}
针对某个字段: { QUERY_NAME: { FIELD_NAME: { ARGUMENT: VALUE, ARGUMENT: VALUE,... } } }

合并查询语句;
{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}

二:最重要的查询

match_all   经常与 filter 结合使用,相同的相关性查询。

match         相关性得分查询

multi_match   

range     gt,gte,lt,lte

term      精确值匹配

terms

exists和missing

三:组合查询

must  文档 必须 匹配这些条件才能被包含进来。
must_not  文档 必须不 匹配这些条件才能被包含进来。
should  如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。
filter  必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。 

四:验证查询

验证查询是否合法:
GET /gb/tweet/_validate/query { "query": { "tweet" : { "match" : "really powerful" } } }

查询不合法的原因:
GET /gb/tweet/_validate/query?explain
{
   "query": {
      "tweet" : {
         "match" : "really powerful"
      }
   }
}

五:排序查询 

基本用法:

单级排序:
"
sort": { "date": { "order": "desc" }}

多级排序:
"sort": [
     { "date":   { "order": "desc" }},
     { "_score": { "order": "desc" }}
 ]

字符串排序和多字段:

为一个多字段映射如:
"tweet": { 
    "type":     "string",
    "analyzer": "english",
    "fields": {
        "raw": { 
            "type":  "string",
            "index": "not_analyzed"
        }
    }
}

  排序:
GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    },
    "sort": "tweet.raw"
}

参考文献:ElasticSearch权威指南

原文地址:https://www.cnblogs.com/parent-absent-son/p/10104857.html