elasticsearch 第四篇(API約定)_elasticsearch

來源:互聯網
上載者:User
對多個indices進行操作

es中大多resetapi支援要求多個index, 例如”test1,test2,test3”,index也可以使用萬用字元, 例如”test*“, 還可以使用+,-來包含或移除某個或某類index, 例如”test*,-test1”
支援設定多個的api的請求字串可設定以下參數: ignore_unavailable: 是否忽略單個index是否可用(不存在或關閉), true表示忽略, false表示不忽略, 預設為false, 例如查詢已經關閉的index:

輸入: GET /test1/user,account/_search?ignore_unavailable=false
輸出:

1234
{   "error": "IndexClosedException[[test1] closed]",   "status": 403}

輸入: GET /test1/user,account/_search?ignore_unavailable=false
輸出:

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

allow_no_indices: 是否忽略萬用字元匹配不到index(不存在或關閉)的情況, true表示允許, false表示不允許,預設為true, 例如查詢已經關閉的index:

輸入: GET /test*/_search?allow_no_indices=false
輸出:

1234
{   "error": "IndexMissingException[[test*] missing]",   "status": 404}

輸入: GET /test*/_search?allow_no_indices=true

1234567891011121314
{   "took": 1,   "timed_out": false,   "_shards": {      "total": 0,      "successful": 0,      "failed": 0   },   "hits": {      "total": 0,      "max_score": 0,      "hits": []   }}
expand_wildcards: 設定是否擴充萬用字元到closed的index中,open表示只在匹配並為open的index中查詢,closed表示在匹配的所有的index中查詢, 預設為closed, 例如查詢已經關閉的index
輸入: GEt /test*/_search?expand_wildcards=closed
輸出:
1234
{   "error": "IndexClosedException[[test1] closed]",   "status": 403}
公用參數

format: 表示返回資料的格式, 可選值為yaml和json兩種, 例如:
輸入: GET /test1/user/_search?format=yaml
輸出:

1234567891011121314151617
---took: 23timed_out: false_shards:  total: 5  successful: 5  failed: 0hits:  total: 1  max_score: 1.0  hits:  - _index: "test1"    _type: "user"    _id: "1"    _score: 1.0    _source:      name: "silence"

pretty: 表示在已json格式返回資料時是否以可視化的格式返回, false或未在設定表示不格式化, 否則格式化

human: 表示是否對返回結果進行格式化處理,比如3600(s)顯示1h

查詢結果過濾
主要使用filter_path參數進行設定

1.在返回結果中我們只關注took, hits.total, hits.hits._id, hits._source, 則我們可以發起如此請求:
輸入:GET /test1/user/_search?filter_path=took,hits.total,hits.hits._id,hits.hits._source
輸出:

1234567891011121314
{   "took": 1,   "hits": {      "total": 1,      "hits": [         {            "_id": "1",            "_source": {               "name": "silence"            }         }      ]   }}

2.也可以使用統配符進行設定
輸入: GET /_nodes/stats?filter_path=nodes.*.*ost*,nodes.*.os.*u
輸出:

12345678910111213141516
{   "nodes": {      "9jfW4VeWRta-Uq7Cq7bK34": {         "host": "silence",         "os": {            "cpu": {               "sys": 1,               "user": 1,               "idle": 96,               "usage": 2,               "stolen": 0            }         }      }   }}

3.若層級較多時可使用**進行簡化
輸入: GET /_nodes/stats?filter_path=nodes.**.*sys*
輸出:

12345678910111213141516
{   "nodes": {      "9jfW4VeWRta-Uq7Cq7bK34": {         "os": {            "cpu": {               "sys": 2            }         },         "process": {            "cpu": {               "sys_in_millis": 139106            }         }      }   }}

4.若只需要_source中的某些值,則可以將filter_path和_source參數共同使用
輸入: GET /test1/account/_search?filter_path=hits.hits._source&_source=firstname,lastname,gender&size=2
輸出:

1234567891011121314151617181920
{   "hits": {      "hits": [         {            "_source": {               "firstname": "Rodriquez",               "gender": "F",               "lastname": "Flores"            }         },         {            "_source": {               "firstname": "Opal",               "gender": "M",               "lastname": "Meadows"            }         }      ]   }}

5.flat_settings用於設定在查詢setting時,setting中的key格式, 預設為false:
輸入: GET /test1/_settings?flat_settings=true
輸出:

1234567891011
{   "test1": {      "settings": {         "index.creation_date": "1442230557598",         "index.uuid": "70bg061IRdKUdDNvgkUBoQ",         "index.version.created": "1060099",         "index.number_of_replicas": "1",         "index.number_of_shards": "5"      }   }}

輸入: GET /test1/_settings?flat_settings=false
輸出:

123456789101112131415
{   "test1": {      "settings": {         "index": {            "creation_date": "1442230557598",            "number_of_shards": "5",            "uuid": "70bg061IRdKUdDNvgkUBoQ",            "version": {               "created": "1060099"            },            "number_of_replicas": "1"         }      }   }}

請求參數格式
1.boolean: 在es中將”0”, 0, false, “false”, “off”識別為false,其他均按ture處理
2.number
3.time: 可以提交一個以毫秒時間的整數或者以日期標識結尾的字串,例如”2d”表示2天,支援的格式有: y(year),M(month),w(week),d(day),h(hour),m(minute),s(second)
4.距離: 可以提交一個以米為單位的認證或者以距離表示結尾的字串,例如”2km”表示2千米,支援的格式有: mi/miles(mile英裡), yd/yards(yard碼), ft/feet(feet尺), in/inch(inch英寸), km/kilometers(kilometer千米), m/meters(meter米), cm/centimeters(centimeter厘米), mm/millimeters(millimeter毫米), NM/nmi/nauticalmiles(Nautical mile納米)
5.模糊類型:
a.數字,時間, IP:類似於range -fuzzines<=value<=+fuzzines
b.字串: 計算編輯距離

返回結果中key的格式為駝峰還是底線分割, 通過case設定為camelCase則返回駝峰格式,否則為底線分割形式 jsonp: 可以用jsonp回調的方式調用es api, 需要通過callback設定回呼函數名稱,並且需要在elasticsearch.yml中配置http.jsonp.enable: true來啟用jsonp格式 url存取控制

可以通過代理方式進行es的url存取控制,但是對於multi-search,multi-get和bulk等在請求參數中設定不同的index的情況很難解決.
為防止通過請求體設定index的情況,需要在elasticsearch.yml中設定rest.action.multi.allow_explicit_index:false, 此時es不允許在request body中設定index

如在修改前:
輸入:

12345
POST /test1/user3/_bulk?pretty{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 1}}{"name" : "silence1"}{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 2}}{"name" : "silence2"}

輸出:

123456789101112131415161718192021222324
{   "took": 225,   "errors": false,   "items": [      {         "index": {            "_index": "test2",            "_type": "user1",            "_id": "1",            "_version": 1,            "status": 201         }      },      {         "index": {            "_index": "test2",            "_type": "user1",            "_id": "2",            "_version": 1,            "status": 201         }      }   ]}

如在修改後(已重啟):
輸出:

1234
{   "error": "IllegalArgumentException[explicit index in bulk is not allowed]",   "status": 500}

輸入:

12345
POST /test1/user3/_bulk?pretty{"index" : {"_id" : 1}}{"name" : "silence1"}{"index" : {"_id" : 2}}{"name" : "silence2"}

輸出:

123456789101112131415161718192021222324
{   "took": 8,   "errors": false,   "items": [      {         "index": {            "_index": "test1",            "_type": "user3",            "_id": "1",            "_version": 1,            "status": 201         }      },      {         "index": {            "_index": "test1",            "_type": "user3",            "_id": "2",            "_version": 1,            "status": 201         }      }   ]}
from: http://imsilence.github.io/2015/09/16/elasticsearch/elasticsearch_04/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.