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

多索引

Elasticsearch允许我们搜索存在于所有索引或一些特定索引中的文档。 例如,如果我们需要搜索名称包含central的所有文档。
我们可以执行下面的命令

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

响应结果是

{
   "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索引中搜索

多类型

还可以在所有类型或某种指定类型的索引中搜索所有文档。 例如,

             //这里指定了索引名为schools,但是并没有指定type,所以是在所有的type中进行搜索
Get http://localhost:9200/schools/_search?q = tags:sports

响应结果为

{
   "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搜索

如下这些参数可以使用统一资源标识符在搜索操作中传递
Elasticsearch搜索API_Elasticsearch

请求正文搜索

还可以在请求正文中使用查询DSL来指定查询,并且在前面的章节中已经给出了很多示例,

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

响应结果为

……………………………………………….
{
   "_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.html