Elasticsearch - 搜尋API



此API用於搜尋Elasticsearch中的內容。使用者可以透過傳送帶有查詢字串作為引數的GET請求進行搜尋,或者可以在POST請求的訊息正文中釋出查詢。主要所有搜尋API都是多索引、多型別的。

多索引

Elasticsearch允許我們搜尋所有索引或某些特定索引中存在的文件。例如,如果我們需要搜尋所有名稱包含“central”的文件,我們可以按如下所示操作:

GET /_all/_search?q=city:paprola 

執行上述程式碼後,我們將獲得以下響應:

{
   "took" : 33,
   "timed_out" : false,
   "_shards" : {
      "total" : 7,
      "successful" : 7,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 1,
         "relation" : "eq"
      },
      "max_score" : 0.9808292,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "5",
            "_score" : 0.9808292,
            "_source" : {
               "name" : "Central School",
               "description" : "CBSE Affiliation",
               "street" : "Nagan",
               "city" : "paprola",
               "state" : "HP",
               "zip" : "176115",
               "location" : [
                  31.8955385,
                  76.8380405
               ],
               "fees" : 2200,
               "tags" : [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating" : "3.3"
            }
         }
      ]
   }
}

URI 搜尋

可以使用統一資源識別符號 (URI) 在搜尋操作中傳遞許多引數:

序號 引數 & 描述
1

Q

此引數用於指定查詢字串。

2

lenient

此引數用於指定查詢字串。透過將此引數設定為true,可以忽略基於格式的錯誤。預設為false。

3

fields

此引數用於指定查詢字串。

4

sort

我們可以使用此引數獲取排序結果,此引數的可能值為fieldName,fieldName:asc/fieldname:desc

5

timeout

我們可以使用此引數限制搜尋時間,並且響應只包含在指定時間內的命中結果。預設情況下,沒有超時。

6

terminate_after

我們可以將響應限制為每個分片指定數量的文件,達到該數量後,查詢將提前終止。預設情況下,沒有terminate_after。

7

from

要返回的命中的起始索引。預設為0。

8

size

表示要返回的命中數。預設為10。

請求體搜尋

我們也可以在請求體中使用查詢DSL指定查詢,之前的章節已經給出許多示例。這裡給出一個這樣的示例:

POST /schools/_search
{
   "query":{
      "query_string":{
         "query":"up"
      }
   }
}

執行上述程式碼後,我們將獲得以下響應:

{
   "took" : 11,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 1,
         "relation" : "eq"
      },
      "max_score" : 0.47000363,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "4",
            "_score" : 0.47000363,
            "_source" : {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : "250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees" : 3500,
               "tags" : [
                  "fully computerized"
               ],
               "rating" : "4.5"
            }
         }
      ]
   }
}
廣告
© . All rights reserved.