教程 > elastic search > 阅读:28

elasticsearch 搜索api——迹忆客-ag捕鱼王app官网

此 api 用于在 elasticsearch 中搜索内容。 用户可以通过发送带有查询字符串作为参数的获取请求进行搜索,也可以在发布请求的消息正文中发布查询。 主要是所有的搜索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 搜索

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

序号 参数 描述
1 q 此参数用于指定查询字符串。
2 lenient 此参数用于指定查询字符串。只需将此参数设置为 true 即可忽略基于格式的错误。 默认情况下为假。
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"
            }
         }
      ]
   }
}

查看笔记

扫码一下
查看教程更方便
网站地图