(十五)The Search API

Now let’s start with some simple searches. There are two basic ways to run searches: one is by sending search parameters through the REST request URIand the other by sending them through the REST request body. The request body method allows you to be more expressive and also to define your searches in a more readable JSON format. We’ll try one example of the request URI method but for the remainder of this tutorial, we will exclusively be using the request body method.

现在让我们从一些简单的搜索开始吧。运行搜索有两种基本方法:一种是通过REST请求URI发送搜索参数,另一种是通过REST请求体发送搜索参数。请求体方法允许您更具表现力,并以更易读的JSON格式定义搜索。我们将尝试一个请求URI方法的示例,但是对于本教程的其余部分,我们将专门使用请求体方法。
 
The REST API for search is accessible from the _search endpoint. This example returns all documents in the bank index:
可以从_search端点访问用于搜索的REST API。此示例返回银行索引中的所有文档:
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"

Let’s first dissect the search call. We are searching (_search endpoint) in the bank index, and the q=* parameter instructs Elasticsearch to match all documents in the index. The sort=account_number:asc parameter indicates to sort the results using the account_number field of each document in an ascending order. The pretty parameter, again, just tells Elasticsearch to return pretty-printed JSON results.

让我们首先剖析搜索电话。我们在银行索引中搜索(_search endpoint),q = *参数指示Elasticsearch匹配索引中的所有文档。 sort = account_number:asc参数指示使用升序中的每个文档的account_number字段对结果进行排序。漂亮的参数再次告诉Elasticsearch返回漂亮的JSON结果。
 
And the response (partially shown):部分展示
{
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : null,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "0",
      "sort": [0],
      "_score" : null,
      "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
    }, {
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "1",
      "sort": [1],
      "_score" : null,
      "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    }, ...
    ]
  }
}

As for the response, we see the following parts:

1、took – time in milliseconds for Elasticsearch to execute the search
  Elasticsearch执行搜索的时间(以毫秒为单位)
2、timed_out – tells us if the search timed out or not
  告诉我们搜索是否超时(默认10秒)
3、_shards - tells us how many shards were searched, as well as a count of the successful/failed searched shards
  告诉我们搜索了多少个分片,以及搜索成功/失败分片的数量

4、 hits – search results        
       搜索结果
5、hits.hits – actual array of search results (defaults to first 10 documents)       

  实际的搜索结果数组(默认为前10个文档)

6、hits.sort - sort key for results (missing if sorting by score)         

   对结果进行排序(如果按分数排序则丢失)

7、 hits._score and max_score - ignore these fields for now
  暂时忽略这些字段
 
 Here is the same exact search above using the alternative request body method:
以下是使用替代请求正文方法的上述完全相同的搜索:
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}
'

The difference here is that instead of passing q=* in the URI, we provide a JSON-style query request body to the _search API. We’ll discuss this JSON query in the next section.

这里的区别在于,我们不是在URI中传递q = *,而是向_search API提供JSON样式的查询请求体。我们将在下一节讨论这个JSON查询。
 
It is important to understand that once you get your search results back, Elasticsearch is completely done with the request and does not maintain any kind of server-side resources or open cursors into your results. This is in stark contrast to many other platforms such as SQL wherein you may initially get a partial subset of your query results up-front and then you have to continuously go back to the server if you want to fetch (or page through) the rest of the results using some kind of stateful server-side cursor.
重要的是要理解,一旦您获得了搜索结果,Elasticsearch就完全完成了请求,并且不会在结果中维护任何类型的服务器端资源或打开游标。这与SQL等许多其他平台形成鲜明对比,其中您可能最初预先获得查询结果的部分子集,然后如果要获取(或翻页)其余的则必须连续返回服务器使用某种有状态服务器端游标的结果。
 
 
 
 
posted @ 2019-01-15 18:08  帅LOVE俊  阅读(242)  评论(0编辑  收藏  举报