ES Document API之多文檔API

來源:互聯網
上載者:User

標籤:自適應   settings   counter   ati   動態   red   timeout   樣本   htm   

多文檔API

  • 多擷取API Get API
#擷取一個類型的多個文檔,有多種API寫法,如下:
#1
curl -XGET ‘localhost:9200/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ]}‘
#2
curl -XGET ‘localhost:9200/test/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_type" : "type", "_id" : "1" }, { "_type" : "type", "_id" : "2" } ]}‘#3
curl -XGET ‘localhost:9200/test/type/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_id" : "1" }, { "_id" : "2" } ]}‘#4
curl -XGET ‘localhost:9200/test/type/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "ids" : ["1", "2"]}‘
#_type欄位可選,如果沒有指定type,則預設第一個類型中滿足條件的id文檔作為返回結果,下面的例子中,返回兩個相同的結果
curl -XGET ‘localhost:9200/test/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "ids" : ["1", "1"]}‘
#可以明確指定不同的類型
curl -XGET ‘localhost:9200/test/_mget/?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_type":"typeA", "_id" : "1" }, { "_type":"typeB", "_id" : "1" } ]}‘
#過濾_source
curl -XGET ‘localhost:9200/_mget?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "_source" : false }, { "_index" : "test", "_type" : "type", "_id" : "2", "_source" : ["field3", "field4"] }, { "_index" : "test", "_type" : "type", "_id" : "3", "_source" : { "include": ["user"], "exclude": ["user.location"] } } ]}‘
#指定stored_field欄位返回 1 返回 field1,field2,2返回field3,field4
curl -XGET ‘localhost:9200/test/type/_mget?stored_fields=field1,field2&pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_id" : "1" }, { "_id" : "2", "stored_fields" : ["field3", "field4"] } ]}‘
#指定routing
curl -XGET ‘localhost:9200/_mget?routing=key1&pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "routing" : "key2" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ]}‘
  • 批量API Bulk API 提高索引效率

大量操作在寫法上需要注意的事項:換行\n結尾; 斷行符號\r ;開始 Content-Type頭部應該被設定為application/x-ndjson;

支援的參數:version_type;routing;wait_for_active_shards;refresh;

update操作支援的動作參數:retry_on_conflict;doc;doc_as_upsert;script;lang;source.

 

#大量操作資料結構
action_and_meta_data \ noptional_source \ naction_and_meta_data \ noptional_source \ n....action_and_meta_data \ noptional_source \ n

curl -XPOST ‘localhost:9200/_bulk?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }{ "field1" : "value1" }{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }{ "field1" : "value3" }{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }{ "doc" : {"field2" : "value2"} }‘

 

curl -XPOST ‘localhost:9200/_bulk?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }{ "doc" : {"field" : "value"} }{ "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }{ "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}{ "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }{ "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "_source" : true} }{ "doc" : {"field" : "value"} }{ "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }{ "doc" : {"field" : "value"}, "_source": true}‘
  • Term vectors 返回特定文檔資訊中的術語資訊和統計資訊
#樣本
curl -XGET ‘localhost:9200/twitter/tweet/1/_termvectors?pretty‘

  #可以指定要檢索的欄位

  curl -XGET ‘localhost:9200/twitter/tweet/1/_termvectors?fields=message&pretty‘

返回欄位包含:term infomation,term statistics,field statistics。預設返回:term infomation和field statistics。

term infomation:1.欄位的term頻率;2.term 位置;3.開始和結束位移量;4.term 承載      即使沒儲存也可以計算。

term statistics :1.術語在總文檔中出現的頻次 2.包含當前術語的總文檔數

field statistics : 1.包含該欄位的文檔數doc_count  2.該欄位中所有term的文檔總頻次 sum_doc_freq 3.該term中每個term的總頻次 sum_ttf

  • multi term vectors 一次性取回多個termvectors
curl -XPOST ‘localhost:9200/_mtermvectors?pretty‘ -H ‘Content-Type: application/json‘ -d‘{   "docs": [      {         "_index": "twitter",         "_type": "tweet",         "_id": "2",         "term_statistics": true      },      {         "_index": "twitter",         "_type": "tweet",         "_id": "1",         "fields": [            "message"         ]      }   ]}‘

 

 

  • Search API

查詢API通常是多文檔 多類型的,除explain api

查詢API可指定多個routing,用“,”隔開

自適應副本選擇:代替迴圈查詢副本的方式,將請求發送到被認為是最佳的分區上:

  • Response time of past requests between the coordinating node and the node containing the copy of the data
  • Time past search requests took to execute on the node containing the data
  • The queue size of the search threadpool on the node containing the data

 

統計資訊組:search 與資訊統計組關聯,每個統計組包含其統計彙總

全域搜尋逾時:search.default_search_timeout  在叢集更新中設定,設定為-1時,即不逾時

搜尋任務取消:task cancell中取消搜尋任務

 

#指定routing
curl -XPOST ‘localhost:9200/twitter/tweet/_search?routing=kimchy&pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "query": { "bool" : { "must" : { "query_string" : { "query" : "some query string here" } }, "filter" : { "term" : { "user" : "kimchy" } } } }}‘

 

#動態集群設定,自適應分區選擇參數為true,預設為false
curl -XPUT ‘localhost:9200/_cluster/settings?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "transient": { "cluster.routing.use_adaptive_replica_selection": true }}‘
#與統計資訊組做關聯
curl -XPOST ‘localhost:9200/_search?pretty‘ -H ‘Content-Type: application/json‘ -d‘{ "query" : { "match_all" : {} }, "stats" : ["group1", "group2"]}‘

 

 

 

  • 通過查詢API更新
  • Reindex API

ES Document API之多文檔API

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.