Elasticsearch distinct filter values -
i have large document store in elasticsearch , retrieve distinct filter values display on html drop-downs.
an example
[ { "name": "john doe", "deparments": [ { "name": "accounts" }, { "name": "management" } ] }, { "name": "jane smith", "deparments": [ { "name": "it" }, { "name": "management" } ] } ]
the drop-down should have list of departments, i.e. it, account , management.
would kind person please point me in right direction retrieving distinct list of departments elasticsearch?
thanks
this job terms
aggregation (documentation).
you can have distinct departments
values :
post company/employee/_search { "size":0, "aggs": { "by_departments": { "terms": { "field": "departments.name", "size": 0 //see note 1 } } } }
which, in example, outputs :
{ ... "aggregations": { "by_departments": { "buckets": [ { "key": "management", //see note 2 "doc_count": 2 }, { "key": "accounts", "doc_count": 1 }, { "key": "it", "doc_count": 1 } ] } } }
two additional notes :
- setting
size
0 set maximum buckets number integer.max_value. don't use if there manydepartments
distinct values. - you can see keys
terms
resulting of analyzingdepartments
values. sure useterms
aggregation on field mappednot_analyzed
.
for example, our default mapping (departments.name
analyzed
string), adding employee:
{ "name": "bill gates", "departments": [ { "name": "it" }, { "name": "human resource" } ] }
will cause kind of result:
{ ... "aggregations": { "by_departments": { "buckets": [ { "key": "it", "doc_count": 2 }, { "key": "management", "doc_count": 2 }, { "key": "accounts", "doc_count": 1 }, { "key": "human", "doc_count": 1 }, { "key": "resource", "doc_count": 1 } ] } } }
with correct mapping :
post company { "mappings": { "employee": { "properties": { "name": { "type": "string" }, "departments": { "type": "object", "properties": { "name": { "type": "string", "index": "not_analyzed" } } } } } } }
the same request ends outputting :
{ ... "aggregations": { "by_departments": { "buckets": [ { "key": "it", "doc_count": 2 }, { "key": "management", "doc_count": 2 }, { "key": "accounts", "doc_count": 1 }, { "key": "human resource", "doc_count": 1 } ] } } }
hope helps!
Comments
Post a Comment