Elasticsearch 搜索API

章節


如今讓咱們從一些簡單的搜索開始。html

搜索參數傳遞有2種方法:shell

  • URI發送搜索參數
  • 請求體(request body)發送搜索參數

搜索相關的REST API能夠從_search端點訪問。下面的例子返回bank索引中的全部文檔:json

API數組

GET /bank/_search?q=*&sort=account_number:asc&pretty

CURLapp

curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"

本例採用uri方式傳遞搜索參數:curl

  • q=* 搜索索引中的全部文檔
  • sort=account_number:asc 搜索結果以字段account_number升序排列
  • pretty 返回結果以漂亮的JSON格式打印

響應:elasticsearch

{
  "took" : 55,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "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"
        },
        "sort" : [
          0
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "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"
        },
        "sort" : [
          1
        ]
      },
      
      ...
      
    ]
  }
}

看一下響應中的重要字段:ide

  • took – 搜索時間(毫秒)
  • timed_out – 搜索是否超時
  • _shards – 搜索了多少分片,搜索分片的成功/失敗計數
  • hits – 搜索結果
  • hits.total – 搜索命中總數信息
    • hits.total.value - 命中總數
    • hits.total.relation - 取值eq(等於)/gte(大於等於),表示hits.total.value與實際的搜索命中數量的關係。
  • hits.hits – 實際的搜索結果數組(默認爲前10個文檔)
  • hits.sort - 結果排序鍵(若是按分數排序,則忽略)
  • hits._scoremax_score - 分數是衡量文檔與搜索條件匹配程度的一個指標。分數越高,文檔越相關,分數越低,文檔越不相關。並不老是須要生成分數,需不須要Elasticsearch會自動判斷,以免計算無用的分數。

若是搜索結果不少,超過必定數量後,一般就再也不統計,只是籠統地表示爲:搜索結果超過XXXX個。hits.total的準確性由請求參數track_total_hits控制,當track_total_hitstrue時,搜索時將精確地跟蹤總命中數(「relationship」:「eq」)。track_total_hits默認值爲10,000,意味着總命中數能夠精確地跟蹤到10000個文檔,若是超過10000,會表示爲超過10000個結果,以下所示:ui

"total" : {
      "value" : 10000,
      "relation" : "gte"
    },

經過將track_total_hits顯式地設置爲true,能夠強制進行準確計數。詳細信息,請參閱request body文檔。url

一樣的例子,使用請求體(request body)發送搜索參數:

API

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

CURL

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}
'

能夠看到,沒有在URI中傳遞q=*,而是向_search API傳遞json風格的請求體,下一節中會詳細討論。

注意,一旦得到了搜索結果,Elasticsearch就會結束此次搜索,不會再維護任何服務端資源,也沒有結果遊標,這與其餘不少平臺,如SQL,不同。

相關文章
相關標籤/搜索