Request Body Search

搜索请求能够通过DSL来执行,在请求体中包含这个查询DSL即可。下面是一个示例:

GET /twitter/tweet/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

这是一个示例的响应:

{
    "took": 1,
    "timed_out": false,
    "_shards":{
        "total" : 1,
        "successful" : 1,
        "failed" : 0
    },
    "hits":{
        "total" : 1,
        "max_score": 1.3862944,
        "hits" : [
            {
                "_index" : "twitter",
                "_type" : "tweet",
                "_id" : "0",
                "_score": 1.3862944,
                "_source" : {
                    "user" : "kimchy",
                    "message": "trying out Elasticsearch",
                    "date" : "2009-11-15T14:12:12",
                    "likes" : 0
                }
            }
        ]
    }
}

参数

在上述中,search_typerequest_cache必须作为查询字符串参数传递。搜索请求的其余部分应在主体本身内传递。请求体内容也可以作为名为source的REST参数传递。

HTTP GETHTTP POST都可以用来执行与body的搜索。由于并非所有客户端都支持GET,所以POST也是允许的。

速检查任何匹配的文档

如果我们只想知道是否有匹配特定查询的文档,我们可以将大小设置为0,表示我们对搜索结果不感兴趣。此外,我们可以将terminate_after设置为1,以指示每当找到第一个匹配文档(每个分片)时,查询执行可以被终止。

GET /_search?q=message:elasticsearch&size=0&terminate_after=1

响应不会包含大小设置为0的任何采样。hits.total等于0表示没有匹配的文档,或大于0表示至少有与查询匹配的数量的文档(当它被提前终止时)。此外,如果查询提前终止,则在响应中将terminate_early标志设置为true

{
  "took": 3,
  "timed_out": false,
  "terminated_early": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.0,
    "hits": []
  }
}

Last updated