多文檔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 \ n
optional_source \ n
action_and_meta_data \ n
optional_source \ n
....
action_and_meta_data \ n
optional_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"] } '