Ich habe ein Problem festgestellt, dass elasticsearch die Anzahl eindeutiger Dokumente nicht zurückgeben konnte, indem nur die Aggregation von Begriffen in einem verschachtelten Feld verwendet wurde. Hierwie die Anzahl der eindeutigen Dokumente mithilfe von elasticsearch aggregation zurückgegeben wird
ist ein Beispiel für unser Modell:
{
...,
"location" : [
{"city" : "new york", "state" : "ny"},
{"city" : "woodbury", "state" : "ny"},
...
],
...
}
I Aggregation auf dem Statusfeld tun will, aber dieses Dokument wird zweimal in dem ‚ny‘ bucket gezählt werden, da ‚ny‘ zweimal in der erscheint Dokument.
Also frage ich mich, ob wo ist eine Möglichkeit, die Anzahl der verschiedenen Dokumente zu greifen.
Mapping:
people = {
:properties => {
:location => {
:type => 'nested',
:properties => {
:city => {
:type => 'string',
:index => 'not_analyzed',
},
:state => {
:type => 'string',
:index => 'not_analyzed',
},
}
},
:last_name => {
:type => 'string',
:index => 'not_analyzed'
}
}
}
die Abfrage ist ziemlich einfach:
curl -XGET 'http://localhost:9200/people/_search?pretty&search_type=count' -d '{
"query" : {
"bool" : {
"must" : [
{"term" : {"last_name" : "smith"}}
]
}
},
"aggs" : {
"location" : {
"nested" : {
"path" : "location"
},
"aggs" : {
"state" : {
"terms" : {"field" : "location.state", "size" : 10}
}
}
}
}
}'
Die Antwort:
{
"took" : 104,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1248513,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"location" : {
"doc_count" : 2107012,
"state" : {
"buckets" : [ {
"key" : 6,
"key_as_string" : "6",
"doc_count" : 214754
}, {
"key" : 12,
"key_as_string" : "12",
"doc_count" : 168887
}, {
"key" : 48,
"key_as_string" : "48",
"doc_count" : 101333
} ]
}
}
}
}
Die doc_count ist viel größer als die Gesamttreffer. Also muss es Duplikate geben.
Danke!
Veröffentlichen Sie Ihre Zuordnung dieses Indexes und der Abfrage, die Sie verwenden, sonst kann ich Ihnen nicht helfen. –
@AndreiStefan Ich habe das Mapping und die Abfrage aktualisiert. Vielen Dank! – milodky