问题
I have a schema that looks something like this:
{
"mappings": {
"entity": {
"properties": {
"a": {
"type": "text"
},
"b": {
"type": "text"
}
}
}
I want to find all the values of b which have a value of a which is shared by 2 or more entities:
Querying against:
[{"a": "a1", "b": "b1"},
{"a": "a1", "b": "b2"},
{"a": "a2", "b": "b3"}]
Should return b1
and b2
.
回答1:
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"
}
}
}
}
}
}
来源:https://stackoverflow.com/questions/51669448/search-for-documents-with-the-same-value-in-elasticsearch