Reputation: 821
I am trying to get unique values from elasticsearch using this dsl query
{
"aggs": {
"distinct": {
"terms": {
"field": "model",
"size": 0
}
}
}
}
The problem with this is that if I have two models i.e. moto g
and htc one
, I get four results, moto
, g
, htc
, one
. How can I configure it to return two results?
Upvotes: 2
Views: 1231
Reputation: 217304
You need to modify the mapping of your model
field to add a not_analyzed
sub-field instead and run the aggregation on that sub-field instead, like this:
curl -XPUT localhost:9200/your_index/_mapping/your_type -d '{
"properties": {
"model": {
"type": "string",
"fields": { <-- add this section
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
Once your mapping is updated, you need to reindex your data and then you'll be able to run your aggregation like this:
{
"aggs": {
"distinct": {
"terms": {
"field": "model.raw", <--- use the new sub-field
"size": 0
}
}
}
}
Upvotes: 2