elasticsearch - Elasticsearch 查询查找一个字段的重复值并返回另一个字段的值,如 GROUP BY
问题描述
ElasticSearch 6.4 - 给定一个文档索引,其中包含一个名为的字段CaptureId
和一个名为 的字段SourceId
:我们需要按CaptureId
值搜索重复记录。该SourceId
字段可以有许多具有相同值的记录,并且我们希望SourceId
每组找到的重复项只返回一个。因此,输出将是一个列表SourceIds
(每次仅列出一次),其中包含任意数量的重复CaptureId
值。
我将如何在 ElasticSearch 中创建此查询?
这是文档映射:
"mappings": {
"fla_doc": {
"_field_names": {
"enabled": false
},
"properties": {
"captureId": {
"type": "long"
},
"capturedDateTime": {
"type": "date"
},
"language": {
"type": "text"
},
"sourceId": {
"type": "long"
},
"sourceListType": {
"type": "text"
},
"region": {
"type": "text"
}
}
}
}
解决方案
假设这两个 ID 字段都是keyword
data type,您可以执行以下操作:
GET index_name/_search
{
"size": 0,
"aggs": {
"by_duplicate_capture": {
"terms": {
"field": "CaptureId",
"min_doc_count": 2
},
"aggs": {
"by_underlying_source_ids": {
"terms": {
"field": "SourceId",
"size": 1
}
}
}
}
}
}
如果您对 more 感兴趣SourceIDs
,请增加size
参数。
推荐阅读
- c++ - 避免共享基本状态的成员访问
- django - 在带有 postgresql 数据库的社交网络 django 网站上,我应该使用什么进行全文搜索和模糊搜索?
- c# - .NET Framework RsaCng 异常
- javascript - 如何对美元符号和逗号使用替换方法?
- javascript - 如何在 Vuejs 中访问外部 js 的方法或对象
- python - 在给定固定数量的移动的情况下,如何检查指针是否可以遍历可能的路径
- java - 如何使用 Java Immutables 设置默认参数?
- controller - GRBL控制器在位置设置步数并将位置设置为零
- bluetooth-lowenergy - BLE 自定义扫描响应
- typescript - Observable 创建新对象而不是更新