伸缩自如的ElasticSearch——请求体查询

空查询

GET /kibana_sample_data_flights_1/_search
{
}

只用一个查询字符串,你就可以在一个、多个或者 _all 索引库(indices)和一个、多个或者所有types中查询:

GET /kibana_sample_data_flights_*/type1,type2/_search
{
}

注意,kibana_sample_data_flights_*的星号表示查询所有前缀为kibana_sample_data_flights_的索引,且type1,type2表示查询这两个类型的索引。

/_index/_type/_id


同时可以使用 from 和 size 参数来分页:
GET /kibana_sample_data_flights_1/_doc/_search
{
	"from": 0,
	"size": 2
}

查询表达式

查询表达式(Query DSL)是一种非常灵活又富有表现力的 查询语言。 空查询(empty search) 在功能上等价于使用 match_all 查询, 正如其名字一样,匹配所有文档:

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

完整的查询请求如下:

GET /kibana_sample_data_flights/_doc/_search
{
	"from": 0,
	"size":2,
	"query":{
	  "match":{
	    "DestAirportID" : "VE05"
	  }
	}
}

查询语句(Query clauses) 就像一些简单的组合块 ,这些组合块可以彼此之间合并组成更复杂的查询。这些语句可以是如下形式:

GET /kibana_sample_data_flights/_doc/_search
{
	"from": 0,
	"size":2,
	"query":{
	  "bool":{
	    "must":{"match":{
	        "DestAirportID" : "VE05"
	      }},
	    "should":{"match":{
	      "FlightDelay" : true
	    }}
	   
	  }
	}
}

通过"bool"和{“must”"must_not""should"}和{"filter"}+"range"``"match"的形式。
比如:

{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}

当使用于 查询情况 时,查询就变成了一个“评分”的查询。和不评分的查询类似,也要去判断这个文档是否匹配,同时它还需要判断这个文档匹配的有 多好(匹配程度如何)。 一个评分查询计算每一个文档与此查询的 相关程度,同时将这个相关程度分配给表示相关性的字段 _score,并且按照相关性对匹配到的文档进行排序。这种相关性的概念是非常适合全文搜索的情况,因为全文搜索几乎没有完全 “正确” 的答案。

查询方式

match_all

不再过多介绍

match

无论你在任何字段上进行的是全文搜索还是精确查询,match 查询是你可用的标准查询。

如果你在一个全文字段上使用 match 查询,在执行查询前,它将用正确的分析器去分析查询字符串

{ "match": { "tweet": "About Search" }}

如果在一个精确值的字段上使用它, 例如数字、日期、布尔或者一个 not_analyzed 字符串字段,那么它将会精确匹配给定的值:

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

multi_match

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

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

range

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

{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}
  • gt 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于

term & terms

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 查询和 missing 查询

exists 查询和 missing 查询被用于查找那些指定字段中有值 (exists) 或无值 (missing) 的文档。这与SQL中的 IS_NULL (missing) 和 NOT IS_NULL (exists) 在本质上具有共性:

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

组合多查询

你可以用 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" }} 
        }
    }
}

验证查询

查询可以变得非常的复杂,尤其 和不同的分析器与不同的字段映射结合时,理解起来就有点困难了。不过 validate-query API 可以用来验证查询是否合法。/_validate/query?explain

GET /kibana_sample_data_flights/_doc/_validate/query?explain
{
	"query":{
	  "bool":{
	    "must":{"match":{
	        "DestAirportID" : "VE05"
	        
	      }},
	    "should":{"match":{
	      "FlightDelay" : true
	    }}
	   
	  }
	}
}

#! Deprecation: [types removal] Specifying types in validate query requests is deprecated.
{
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "valid" : true,
  "explanations" : [
    {
      "index" : "kibana_sample_data_flights",
      "valid" : true,
      "explanation" : "+(+DestAirportID:VE05 FlightDelay:T) #*:*"
    }
  ]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值