我必须在弹性搜索索引中插入批量记录,其中_id是消息中多个字段的组合。我可以这样做吗如果可以做到那么请给我一个样本json相同。问候
一个示例_id字段我正在寻找下面的东西
{
"_index": "kpi_aggr",
"_type": "KPIBackChannel",
"_id": "<<<combination of name , period_type>>>",
"_score": 1,
"_source": {
"name": "kpi-v1",
"period_type": "w",
"country": "AL",
"pg_name": "DENTAL CARE",
"panel_type": "retail",
"number_of_records_with_proposal": 10000,
"number_of_proposals": 80000,
"overall_number_of_records": 2000,
"@timestamp": 1442162810
}
}
当然,您可以在调用Index API时指定自己的Elasticsearch文档ID:
PUT kpi_aggr/KPIBackChannel/kpi-v1,w
{
"name": "kpi-v1",
"period_type": "w",
"country": "AL",
"pg_name": "DENTAL CARE",
"panel_type": "retail",
"number_of_records_with_proposal": 10000,
"number_of_proposals": 80000,
"overall_number_of_records": 2000,
"@timestamp": 1442162810
}
你也可以在_bulk
API call期间这样做:
POST _bulk
{ "index" : { "_index" : "kpi_aggr", "_type" : "KPIBackChannel", "_id" : "kpi-v1,w" } }
{"name":"kpi-v1","period_type":"w","country":"AL","pg_name":"DENTAL CARE","panel_type":"retail","number_of_records_with_proposal":10000,"number_of_proposals":80000,"overall_number_of_records":2000,"@timestamp":1442162810}
请注意,Elasticsearch将使用新版本替换文档。
如果在空索引上执行这两个查询,则按文档ID查询:
GET kpi_aggr/KPIBackChannel/kpi-v1,w
会给你以下内容:
{
"_index": "kpi_aggr",
"_type": "KPIBackChannel",
"_id": "kpi-v1,w",
"_version": 2,
"found": true,
"_source": {
"name": "kpi-v1",
"period_type": "w",
"country": "AL",
"pg_name": "DENTAL CARE",
"panel_type": "retail",
"number_of_records_with_proposal": 10000,
"number_of_proposals": 80000,
"overall_number_of_records": 2000,
"@timestamp": 1442162810
}
}
请注意"_version": 2
,在我们的例子中表明文档已被索引两次,因此执行了“upsert”(但通常意味着用于Optimistic Concurrency Control)。
希望有所帮助!