嘿~ 今天天气不错嘛

Es怎么实现按多字段去重查询呢?

Elasticsearch | 作者 sandheart | 发布于2023年03月21日 | 阅读数:4621

Es怎么实现按多字段去重查询呢?不是统计,是查询结果,按多字段分组每组显示一条;
试了下collapse的折叠功能很好用,但只能折叠一个字段,
script_fields合并的字段又不能在collapse中使用,提示:
 
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "no mapping found for `group_field` in order to collapse on",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "no mapping found for `group_field` in order to collapse on"
}
}

 
大家有没有什么好办法实现多字段去重查询呢?
 
已邀请:

God_lockin

赞同来自:

copy_to? 或者把你需要折叠的内容拼在一起冗余进去

zhangcm - 90后

赞同来自:

以下是以authorId为基准(你可以多加几个去重基准),返回去重后的authorId对应的文档里面的bookId-其中bookId必须要有排序条件,并且只返回一条,另外,你需要为你每个field设置aggs
 {
  "explain": false,
  "size": 0,
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "authorId"
      },
      "aggs": {
        "book_id": {
          "terms": {
            "size": 1,
            "field": "bookId",
            "order": {
              "_key": "desc"
            }
          }
        }
      }
    },
      "aggs": {
        "other_field_xxx": {
          "terms": {
            "size": 1,
            "field": "other_field_xxx",
            "order": {
              "_key": "desc"
            }
          }
        }
      }
    }
  }
}

charlesfang

赞同来自:

Second level of collapsing,这个是不是可以看一下
https://www.elastic.co/guide/e ... psing

要回复问题请先登录注册