如何在Elasticsearch中搜索两个索引,以汇总两个索引中出现的值?
例如:
GET indexA,indexB/_search
{
"aggs": {
"myField": {
"terms": {
"field": "myField"
}
}
}
}
这样我就可以获取myField在两个索引(indexA和indexB)中都具有的所有值,但是如何更改此值,使其仅显示同时出现在indexA和indexB中的值?
澄清一下,myField在indexA中是否具有value1,value2和value3
但它在indexB中仅包含value1和value2,我的搜索将仅显示value1和value2。
最佳答案
您可以这样做(并且您需要Elasticsearch 2.x):
{
"size": 0,
"aggs": {
"myField": {
"terms": {
"field": "myField"
},
"aggs": {
"count_indices": {
"cardinality": {
"field": "_index"
}
},
"values_bucket_filter_by_index_count": {
"bucket_selector": {
"buckets_path": {
"count": "count_indices"
},
"script": "count >= 2"
}
}
}
}
}
}
使用
"terms": {"field": "myField"}
,您可以获得唯一的myField
值。然后,作为子聚合,您可以使用"cardinality": {"field": "_index"}
计算具有该值的索引的数量,使用最终聚合-values_bucket_filter_by_index_count
-可以保留至少包含两个索引的存储分区。最后,聚合结果如下所示:
"aggregations": {
"myField": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "value1",
"doc_count": 2,
"count_indices": {
"value": 2
}
},
{
"key": "value2",
"doc_count": 2,
"count_indices": {
"value": 2
}
}
]
}
}
正如我提到的,您需要Elasticsearch 2.x进行
bucket_selector
聚合。
关于search - 如何仅对在两个索引中都出现的值汇总在Elasticsearch上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709937/