ES Document API之多文档API

时间:2021-08-24 05:38:00

#获取一个类型的多个文档,,有多种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" } ] }

批量操纵在写法上需要注意的事项:换行\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}

#示例
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