需要一个 Elasticsearch 查询,它将结果限制为在一个字段中具有相同值但在另一个字段中具有不同值的结果

Need an Elasticsearch query that will restrict results to those that have the same value in one field but different values in another field

我猜这很可能是一个聚合,因为我不知道某个特定字段的值 - 需要与其他结果进行比较的字段。 我正在尝试创建一个结果列表,其中: Result1 中的 fieldA 与 Result2 中的 fieldA 具有完全相同的值(我不在乎它是什么) 和 其中 1 个 Result1 在 fieldB 中有“蓝色”,而 Result2 在 fieldB 中有“绿色”(我关心的是“蓝色存在于一个中,而“绿色”存在于另一个中。 我是否正确认为这只能通过聚合来实现? 如果是这样,聚合脚本会是什么样子(我假设它也是一个脚本)。 谢谢

所以,这里有一些示例数据:

[
    {
        "record_id": "1",
        "record_type": "typeA",
        "field_a": "1111111111",
        "field_b": "blue"
    },
    {
        "record_id": "2",
        "record_type": "typeA",
        "field_a": "1111111111",
        "field_b": "green"
    },
    {
        "record_id": "3",
        "record_type": "typeA",
        "field_a": "2222222222",
        "field_b": "blue"
    },
    {
        "record_id": "4",
        "record_type": "typeA",
        "field_a": "2222222222",
        "field_b": "yellow"
    }
]

我的查询只知道“record_type”的值。 我需要的是一个“field_a”值列表,这些值匹配至少有 2 个结果,其中 1 个在“field_b”中有“蓝色”,1 个在“[=28=”中有“绿色” ]".

所以,在这个例子中,我想知道“1111111111”是否符合该条件 - 有 1 个是“蓝色”,1 个是“绿色”,而“2222222222”不匹配,因为有 1 个是“蓝色”和 1 个带有“黄色”(即不是“绿色”)。

我知道我可以进行聚合,例如,为我的结果计算“field_a”中的值。

但是,“field_a”中可以有1-N个相同的值。我特别需要知道对于给定的“field_a”值,“field_b”中是否至少有 1 个带有“蓝色”,“field_b”中是否至少有 1 个带有“绿色” .

希望这说明了问题,但如果不是,这就是我“认为”我应该做的 - 聚合的聚合:

{
  "size": 0,
   "query": {
      "bool": {
        "must": [
        {
          "query_string": {
            "query": "*",
            "analyze_wildcard": true,
            "default_field": "*"
          }
        }
      ],
    "filter" : [
      {
        "terms" : {
          "record_type.keyword" : [
            "typeA"
          ],
          "boost" : 1.0
        }
      }
    ]
      }
   },
   "aggs": {
    "uniq_field_a_values": {
      "terms": {
        "field": "field_a.keyword",
        "size" : 10000
      }
    },
    "aggs": {
      [ what should this look like???? ]
    }
  }
}

我可以通过在上面写的“[这应该是什么样子????]”的地方执行以下操作来获得我想要的东西:

      "aggs": {
        "blue": {
          "filter": {
            "term": {
              "fieldB.keyword": "blue"
            }
          }
        },
        "green": {
          "filter": {
            "term": {
              "fieldB.keyword": "green"
            }
          }
        },
        "both": {
          "bucket_script": {
            "buckets_path": {
              "blue_count": "blue._count",
              "green_count": "green._count"
            },
            "script": "if ((params.blue_count > 0) && (params.green_count > 0)) { 1}"
          }
        }