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

标签 elasticsearch elasticsearch-aggregation

我猜这很可能是一个汇总,因为我不知道一个特定字段的值-需要将该字段与其他结果进行比较。
我正在尝试创建结果列表,其中:
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”中具有“blue”,而1个在“field_b”中具有“green”。
因此,在此示例中,我想知道“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}"
          }
        }
    

关于elasticsearch - 需要一个Elasticsearch查询,以将结果限制为在一个字段中具有相同值而在另一个字段中具有不同值的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64071341/

相关文章:

elasticsearch - Elasticsearch计数值在范围内的文档

c# - ElasticSearch:查询作为对象数组的字段

ruby-on-rails - SearchKick中的日期范围查询( Elasticsearch )

django - ElasticSearch-python中的完成建议的批量索引

mapping - Elasticsearch 如何在ouchdb的附件文件中找到单词?

elasticsearch - ElasticSearch查询带有聚合前缀

elasticsearch - Elasticsearch 按字段分组

elasticsearch - 使用 query_string Elasticsearch 进行模糊搜索

spring-boot - 文本字段未针对需要每个文档 : Elasticsearch 的操作进行优化

elasticsearch - 重构Elasticsearch模型以实现快速聚合