【ES】学习3-请求体查询

1.空查询

GET /index_2014*/type1,type2/_search
{}
GET /_search
{
  "from": 30,
  "size": 10
}

2.查询表达式

DSL只需将查询语句传递给 query 参数

GET /_search
{
    "query": YOUR_QUERY_HERE
}

查询全部 match_all 跟空查询等价

GET /_search
{
    "query": {
        "match_all": {}
    }
}

针对某个字段,结构

{
    QUERY_NAME: {
        FIELD_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
}
GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}

3.查询与过滤

查询:一个评分的匹配,计算相似度

过滤:一个不评分的匹配,只有是或否。过滤的性能更好。

4.重要字段

match_all:匹配所有

{ "match_all": {}}

match:全文匹配或精确匹配

{ "match": { "tweet": "About Search" }}
{ "match": { "age":    26           }}
{ "match": { "date":   "2014-09-01" }}
{ "match": { "public": true         }}
{ "match": { "tag":    "full_text"  }}

multi_match:在多个字段上执行相同的match查询

{
    "multi_match": {
        "query":    "full text search",
        "fields":   [ "title", "body" ]
    }
}

range:找出那些落在指定区间内的数字或者时间

{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}

term:用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些not_analyzed 的字符串

{ "term": { "age":    26           }}
{ "term": { "date":   "2014-09-01" }}
{ "term": { "public": true         }}
{ "term": { "tag":    "full_text"  }}

terms:和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件

{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

exists:查找指定字段中有值的文档

{
    "exists":   {
        "field":    "title"
    }
}

missing:查找指定字段中无值的文档

5.组合多查询

bool:将多查询组合在一起。它支持参数:

  must:文档 必须 匹配这些条件才能被包含进来。

  must_not:文档 必须不 匹配这些条件才能被包含进来。

  should:如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。

  filter:必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

{
    "bool": {
        "must":     { "match": { "title": "how to make millions" }},
        "must_not": { "match": { "tag":   "spam" }},
        "should": [
            { "match": { "tag": "starred" }}
        ],
        "filter": {
          "range": { "date": { "gte": "2014-01-01" }} 
        }
    }
}
{
    "bool": {
        "must":     { "match": { "title": "how to make millions" }},
        "must_not": { "match": { "tag":   "spam" }},
        "should": [
            { "match": { "tag": "starred" }}
        ],
        "filter": {
          "bool": { 
              "must": [
                  { "range": { "date": { "gte": "2014-01-01" }}},
                  { "range": { "price": { "lte": 29.99 }}}
              ],
              "must_not": [
                  { "term": { "category": "ebooks" }}
              ]
          }
        }
    }
}

constant_score查询:

它将一个不变的常量评分应用于所有匹配的文档。它被经常用于你只需要执行一个 filter 而没有其它查询(例如,评分查询)的情况下。可以使用它来取代只有 filter 语句的 bool 查询。

{
    "constant_score":   {
        "filter": {
            "term": { "category": "ebooks" } 
        }
    }
}

6. 验证查询

_validate, explain:判断查询是否合法以及原因。

GET /gb/tweet/_validate/query?explain 
{
   "query": {
      "tweet" : {
         "match" : "really powerful"
      }
   }
}
原文地址:https://www.cnblogs.com/dplearning/p/6940715.html