在Elasticsearch中搜索具有相同值的文档 [英] Search for documents with the same value in Elasticsearch
本文介绍了在Elasticsearch中搜索具有相同值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的架构:
I have a schema that looks something like this:
{
"mappings": {
"entity": {
"properties": {
"a": {
"type": "text"
},
"b": {
"type": "text"
}
}
}
我想找到b的所有值,其中b的值由2个或更多实体共享:
I want to find all the values of b which have a value of a which is shared by 2 or more entities:
查询依据:
[{"a": "a1", "b": "b1"},
{"a": "a1", "b": "b2"},
{"a": "a2", "b": "b3"}]
应返回 b1
和 b2
.
推荐答案
您可以使用 min_doc_count
a 字段上进行 terms
聚合> 2,然后添加 top_hits
子聚合以找到匹配的 b
字段:
You can do a terms
aggregation on the a
field with a min_doc_count
of 2 and then add a top_hits
sub-aggregation to find the matching b
fields:
{
"size": 0,
"aggs": {
"dups": {
"terms": {
"field": "a",
"min_doc_count": 2
},
"aggs": {
"b_hits": {
"top_hits": {
"_source": "b"
}
}
}
}
}
}
这篇关于在Elasticsearch中搜索具有相同值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文