Elasticsearch搜索API

此API用于在Elasticsearch中搜索内容。html

多索引

Elasticsearch容许咱们搜索存在于全部索引或一些特定索引中的文档。 例如,若是咱们须要搜索名称包含central的全部文档。
咱们能够执行下面的命令json

//这里没有指定索引名称,因此是搜索全部的索引,找含有name字段,且字段名的值是central的文档
GET http://localhost:9200/_search?q = name:central

响应结果是api

{
   "took":78, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":1, "max_score":0.19178301, "hits":[{
         "_index":"schools", "_type":"school", "_id":"1", "_score":0.19178301,
         "_source":{
            "name":"Central School", "description":"CBSE Affiliation", 
            "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
            "location":[31.8955385, 76.8380405], "fees":2000, 
            "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
         }
      }]
   }
}

或者,一样地咱们能够在schoolsschools_gov索引中搜索markdown

多类型

还能够在全部类型或某种指定类型的索引中搜索全部文档。 例如,yii

//这里指定了索引名为schools,可是并无指定type,因此是在全部的type中进行搜索
Get http://localhost:9200/schools/_search?q = tags:sports

响应结果为elasticsearch

{
   "took":16, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},
   "hits":{
      "total":1, "max_score":0.5, "hits":[{
         "_index":"schools", "_type":"school", "_id":"2", "_score":0.5,
         "_source":{
            "name":"Saint Paul School", "description":"ICSE Afiliation", 
            "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075", 
            "location":[28.5733056, 77.0122136], "fees":5000, 
            "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
         }
      }]
   }
}

URI搜索

以下这些参数能够使用统一资源标识符在搜索操做中传递
在这里插入图片描述ide

请求正文搜索

还能够在请求正文中使用查询DSL来指定查询,而且在前面的章节中已经给出了不少示例,ui

//指定了索引名为schools,可是没有指定type类型名,因此是在该索引下的全部类型中搜索
POST http://localhost:9200/schools/_search
{
   "query":{
      "query_string":{   
         "query":"up"  //由于没有指定具体查询的字段,因此是在全部的字段中查询字段包含“up”的结果
      }
   }
}

响应结果为spa

……………………………………………….
{
   "_source":{
      "name":"City School", "description":"ICSE", "street":"West End",
      "city":"Meerut", "state":"UP", "zip":"250002", "location":[28.9926174, 77.692485],
      "fees":3500, "tags":["Well equipped labs"],"rating":"4.5"
   }
}

本文转载自:https://www.yiibai.com/elasticsearch/elasticsearch_search_apis.htmlcode

相关文章
相关标签/搜索