Elasticsearch提供了一個REST API,經過HTTP經過JSON訪問。 Elasticsearch使用如下約定 -html
API中的大多數操做(主要是搜索和其餘操做)用於一個或多個索引。 這有助於用戶經過只執行一次查詢來搜索多個位置或全部可用數據。 許多不一樣的符號用於在多個索引中執行操做。 咱們將在本節討論其中的一些。json
舉例以下:api
POST http://localhost:9200/index1,index2,index3/_search { "query":{ "query_string":{ "query":"any_string" } } }
響應結果
來自index1
,index2
,index3
的JSON對象,這些JSON對象都包含any_string
字符串。markdown
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
舉例說明以下:
若是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 = true
orm
POST http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true { "query":{ "query_string":{ "query":"CBSE" } } }
響應(無錯誤)
在以school開頭的索引庫中進行搜索,獲得包含字符串CBSE
的JSON對象
若是從帶有通配符的網址中沒有找到索引,這個參數是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":[]} }
設置是否擴展通配符到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}}>
對應,其中accountdetail
和static_name
對應,date_math_expr
和now-2d
對應,date_format
和YYYY.MM.dd
對應)
http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search
講解:static_name
是表達式的一部分,在每一個日期數學索引(如賬戶詳細信息)中保持相同。 date_math_expr包含動態肯定日期和時間的數學表達式,如now-2d
。date_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