Elasticsearch 简明教程

Elasticsearch - Search APIs

此 API 用于在 Elasticsearch 中搜索内容。用户可以通过使用查询字符串作为参数发送 GET 请求或在 POST 请求的信息主体中发布查询来进行搜索。基本上,所有搜索 API 都是多索引多类型。

This API is used to search content in Elasticsearch. A user can search by sending a get request with query string as a parameter or they can post a query in the message body of post request. Mainly all the search APIS are multi-index, multi-type.

Multi-Index

Elasticsearch 允许我们搜索所有索引或某些特定索引中存在的文档。例如,如果我们需要搜索名称中包含中心的所有文档,我们可以像这里所示的那样进行操作:

Elasticsearch allows us to search for the documents present in all the indices or in some specific indices. For example, if we need to search all the documents with a name that contains central, we can do as shown here −

GET /_all/_search?q=city:paprola

在运行上述代码后,我们会得到以下响应:

On running the above code, we get the following response −

{
   "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"
            }
         }
      ]
   }
}

在搜索操作中可以使用统一资源标识符传入许多参数:

Many parameters can be passed in a search operation using Uniform Resource Identifier −

S.No

Parameter & Description

1

Q This parameter is used to specify query string.

2

lenient This parameter is used to specify query string.Format based errors can be ignored by just setting this parameter to true. It is false by default.

3

fields This parameter is used to specify query string.

4

sort We can get sorted result by using this parameter, the possible values for this parameter is fieldName, fieldName:asc/fieldname:desc

5

timeout We can restrict the search time by using this parameter and response only contains the hits in that specified time. By default, there is no timeout.

6

terminate_after We can restrict the response to a specified number of documents for each shard, upon reaching which the query will terminate early. By default, there is no terminate_after.

7

from The starting from index of the hits to return. Defaults to 0.

8

size It denotes the number of hits to return. Defaults to 10.

我们还可以在请求正文中使用查询 DSL 指定查询,在之前的章节中已经给出了许多示例。这里给出了一个这样的示例:

We can also specify query using query DSL in request body and there are many examples already given in previous chapters. One such example is given here −

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

在运行上述代码后,我们会得到以下响应:

On running the above code, we get the following response −

{
   "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"
            }
         }
      ]
   }
}