【问题标题】:Filter results to remove documents with the same field value based on another field value (without aggregation)过滤结果以根据另一个字段值删除具有相同字段值的文档(无聚合)
【发布时间】:2016-06-09 17:37:45
【问题描述】:

给定弹性搜索索引中的以下 4 个对象:

"hits": [
  {
    "_id": "0:0",
    "_source": {
      "id": 0,
      "version": 0,
      "published": true
    }
  },
  {
    "_id": "0:1",
    "_source": {
      "id": 0,
      "version": 1,
      "published": false,
      "latest": true
    }
  },
  {
    "_id": "1:0",
    "_source": {
      "id": 1,
      "version": 0,
      "published": true
    }
  },
  {
    "_id": "1:1",
    "_source": {
      "id": 1,
      "version": 1,
      "published": true,
      "latest": true
    }
  }
]

我想使用这些规则查找文档:

  • published:true
  • 没有重复的id
  • 对于具有相同id 的文档,应返回最高的version

因此,对于上述内容,我想获得0:01:1

"hits": [
  {
    "_id": "0:0",
    "_source": {
      "id": 0,
      "version": 0,
      "published": true
    }
  },
  {
    "_id": "1:1",
    "_source": {
      "id": 1,
      "version": 1,
      "published": true,
      "latest": true
    }
  }
]

我知道我可以使用top_hits,但我想知道如果没有它是否可行,这样主hits.hits 数组将包含这些结果。

我可能会按如下方式折叠:

{ 
  query  : {...},
  aggs : {
    ids: {
      terms: {
          field: "id"
      },
      aggs:{
          dedup:{
            top_hits:{ size:1, sort: {version : 'desc'} }
          }
        }    
    }
  }
}

我希望避免使用top_hits 的原因是我需要在我们的应用程序中更新结果解析器。如果我这样做,size 字段也将无法正常工作。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    要回答我自己的问题based on this answer,不使用top_hits 聚合是不可能的。我认为我试图实现的并不是聚合的最佳使用。相反,我将通过将latestPublishedtrue 添加到相关模型来调整索引模型,从而允许查询为{ term: { latestPublished: true}}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多