Elasticsearch API約定

Elasticsearch提供了一個REST API,經過HTTP經過JSON訪問。 Elasticsearch使用如下約定 -html

多索引

API中的大多數操做(主要是搜索和其餘操做)用於一個或多個索引。 這有助於用戶經過只執行一次查詢來搜索多個位置或全部可用數據。 許多不一樣的符號用於在多個索引中執行操做。 咱們將在本節討論其中的一些。json

逗號分隔符號

舉例以下:api

POST http://localhost:9200/index1,index2,index3/_search
{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

響應結果
來自index1index2index3的JSON對象,這些JSON對象都包含any_string字符串。markdown

全部索引的_all關鍵字

POST http://localhost:9200/_all/_search    //_all表示查詢全部索引
{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

響應結果
獲得來自全部索引的包含「any_string」字符串的JSON對象,yii

通配符(*,+, - )

POST http://localhost:9200/school*/_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

響應結果:
從以school開頭的索引庫中查詢到包含字符串「CBSE」的JSON對象elasticsearch

擴展:

POST http://localhost:9200/school*,-schools_gov /_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

響應結果:
對全部以「school」開頭,但不是schools_gov的索引庫進行搜索,而後獲得包含字符串「CBSE」的JSON對象ide

其餘參數

ignore_unavailable參數的講解

舉例說明以下:
若是URL中存在的一個或多個索引不存在,則不會發生錯誤或操做不會中止。 例如,schools 索引存在,但book_shops不存在spa

POST http://localhost:9200/school*,book_shops/_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

響應結果報錯以下: (緣由就是由於book_shops這個索引庫不存在)code

{
   "error":{
      "root_cause":[{
         "type":"index_not_found_exception", "reason":"no such index",
         "resource.type":"index_or_alias", "resource.id":"book_shops", 
         "index":"book_shops"
      }],

      "type":"index_not_found_exception", "reason":"no such index",
      "resource.type":"index_or_alias", "resource.id":"book_shops", 
      "index":"book_shops"

   },"status":404
}

因此咱們須要修改一下: 在URL後面加上?ignore_unavailable = trueorm

POST http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

響應(無錯誤)
在以school開頭的索引庫中進行搜索,獲得包含字符串CBSE的JSON對象

allow_no_indices參數講解

若是從帶有通配符的網址中沒有找到索引,這個參數是true值時將防止錯誤,

舉例說明: 若是在elasticSearch裏面不存在以schools_pri開頭的索引,咱們卻用下面的代碼去搜索,那麼就會報錯

POST http://localhost:9200/schools_pri*/_search
{
   "query":{
      "match_all":{}
   }
}

作一下修改來解決這個報錯 (在URL後面加上?allow_no_indices = true便可)

POST http://localhost:9200/schools_pri*/_search?allow_no_indices = true
{
   "query":{
      "match_all":{}
   }
}

而後再次執行就沒有錯誤了,響應結果以下:

{
   "took":1,"timed_out": false, "_shards":{"total":0, "successful":0, "failed":0}, 
   "hits":{"total":0, "max_score":0.0, "hits":[]}
}

expand_wildcards

設置是否擴展通配符到closed的index中,open表示只在open的index中查詢,closed表示在匹配的全部的index中查詢, 默認爲closed, 例如查詢已經關閉的index
舉2個例子來講明該參數的做用:
①第一個例子:
查詢已經關閉的名爲test1索引
輸入:

GEt /test*/_search?expand_wildcards=closed

輸出:

{                              //這個test1就是咱們關閉的索引
   "error": "IndexClosedException[[test1] closed]",
   "status": 403
}

②第二個例子
咱們先執行下面的代碼把schools這個索引給關閉

POST http://localhost:9200/schools/_close

而後咱們再去用以下通配符查找這個索引庫的話,就會找不到了

POST http://localhost:9200/school*/_search

可是咱們若是加上?expand_wildcards = closed,見以下代碼

POST http://localhost:9200/school*/_search?expand_wildcards = closed
{
   "query":{
      "match_all":{}
   }
}

就能夠找到關閉了的索引庫

{
   "error":{
      "root_cause":[{                                  //下面的schools就是咱們關閉的索引
         "type":"index_closed_exception", "reason":"closed", "index":"schools"
      }],
                                                             //下面的schools就是咱們關閉的索引
      "type":"index_closed_exception", "reason":"closed", "index":"schools"
   }, "status":403
}

日期索引名稱中的數學支持

Elasticsearch提供了根據日期和時間搜索索引的功能。咱們須要以特定格式指定日期和時間。 例如,accountdetail-2015.12.30,索引將存儲2015年12月30日的銀行賬戶詳細信息。能夠執行數學操做以獲取特定日期或日期和時間範圍的詳細信息。

日期數字索引名稱的格式:

<static_name{date_math_expr{date_format|time_zone}}>

和下面的代碼作對比(<accountdetail-{now-2d{YYYY.MM.dd|utc}}><static_name{date_math_expr{date_format|time_zone}}>對應,其中accountdetailstatic_name對應,date_math_exprnow-2d對應,date_formatYYYY.MM.dd對應)

http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search

在這裏插入圖片描述
講解:static_name是表達式的一部分,在每一個日期數學索引(如賬戶詳細信息)中保持相同。 date_math_expr包含動態肯定日期和時間的數學表達式,如now-2ddate_format包含日期在索引中寫入的格式,如YYYY.MM.dd。 若是今天的日期是2015年12月30日,則<accountdetail- {now-2d {YYYY.MM.dd}}>將返回accountdetail-2015.12.28

美化結果

能夠經過附加一個網址查詢參數(即pretty = true),得到格式正確的JSON對象的響應。

POST http://localhost:9200/schools/_search?pretty = true
{
   "query":{
      "match_all":{}
   }
}

響應結果

……………………..
    {
       "_index" : "schools", "_type" : "school", "_id" : "1", "_score" : 1.0,
       "_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"
       }
    }    
    ………………….

響應過濾
能夠經過將其添加到field_path參數中來過濾對較少字段的響應。 例如,

POST http://localhost:9200/schools/_search?filter_path = hits.total
{
   "query":{
      "match_all":{}
   }
}

響應的結果

{"hits":{"total":3}}

本文轉載自:https://www.yiibai.com/elasticsearch/elasticsearch_api_conventions.html

相關文章
相關標籤/搜索