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

相關文章
相關標籤/搜索