(十五)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.html

如今讓咱們從一些簡單的搜索開始吧。運行搜索有兩種基本方法:一種是經過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.express

讓咱們首先剖析搜索電話。咱們在銀行索引中搜索(_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:json

一、took – time in milliseconds for Elasticsearch to execute the search
  Elasticsearch執行搜索的時間(以毫秒爲單位)
二、timed_out – tells us if the search timed out or not
  告訴咱們搜索是否超時(默認10秒)
三、_shards - tells us how many shards were searched, as well as a count of the successful/failed searched shards
  告訴咱們搜索了多少個分片,以及搜索成功/失敗分片的數量

四、 hits – search results        
       搜索結果
五、hits.hits – actual array of search results (defaults to first 10 documents)       數組

  實際的搜索結果數組(默認爲前10個文檔) 服務器

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

   對結果進行排序(若是按分數排序則丟失)curl

七、  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.elasticsearch

這裏的區別在於,咱們不是在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等許多其餘平臺造成鮮明對比,其中您可能最初預先得到查詢結果的部分子集,而後若是要獲取(或翻頁)其他的則必須連續返回服務器使用某種有狀態服務器端遊標的結果。
相關文章
相關標籤/搜索