0

我在下面写了 ES Query for fetching data Using filter and Must:我只能使用 ElasticSearch V5 来编写查询。

{
  "bool" : {
    "must" : [
      {
        "term" : {
          "Name" : {
            "value" : "Self",
            "boost" : 1.0
          }
        }
      }
    ],
    "filter" : [
      {
        "terms" : {
          "Number" : [
            "111",
            "222"
            
          ]
        }
      }
    ]
  }
}

我得到这样的输出,具有重复值。我想使用聚合查询。

 "hits": [
      { "Name": "Self","Number": 111},
      { "Name": "Self","Number": 222},
      { "Name": "Self","Number": 222},
      { "Name": "Self","Number": 111}
    ]

我只想为每个数字选择前 1 个值,所以我的结果会更少。
O/P 应该是:

"hits": [
          { "Name": "Self","Number": 222},
          { "Name": "Self","Number": 111}
        ]
4

1 回答 1

1

您可以使用字段折叠来删除重复的结果

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "Name": {
              "value": "Self",
              "boost": 1.0
            }
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "Number": [
              "111",
              "222"
            ]
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "Number"
  }
}

搜索结果将是

"hits": [
      {
        "_index": "68849562",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.10536051,
        "_source": {
          "Name": "Self",
          "Number": 111
        },
        "fields": {
          "Number": [
            111
          ]
        }
      },
      {
        "_index": "68849562",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.10536051,
        "_source": {
          "Name": "Self",
          "Number": 222
        },
        "fields": {
          "Number": [
            222
          ]
        }
      }
    ]
于 2021-08-19T14:28:36.367 回答