search - 如何仅对在两个索引中都出现的值汇总在Elasticsearch上?

标签 search elasticsearch

如何在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/

相关文章:

firebase - 如何将 Firestore 日期对象映射到 elasticsearch 中的日期

algorithm - 函数按预期执行,但在循环结束时返回 nil 值

android - 通过 ArrayList 自定义搜索数组名称

algorithm - 为什么迭代加深A星不需要测试重复状态?

elasticsearch - 计数查询所用的时间 - Elasticsearch

java - Hibernate搜索lucene : Updating index document by removing a child entity makes children entity as orphan in index document

elasticsearch - 弹性在压力下给出不一致的结果

php - 使用ElasticSearch匹配多个文档

c - 为什么我的二分搜索只能按此顺序处理 if 语句?

elasticsearch - 在Elasticsearch中创建动态构面