Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - Return Unique Values

How would I get the values of all the languages from the records and make them unique.

Records

PUT items/1
{ "language" : 10 }

PUT items/2
{ "language" : 11 }

PUT items/3
{ "language" : 10 }

Query

GET items/_search
{ ... }

# => Expected Response
[10, 11]

Any help would be great.

like image 573
ChuckJHardy Avatar asked Oct 08 '22 05:10

ChuckJHardy


People also ask

How do I get distinct values in Elasticsearch?

Elasticsearch is a powerful search engine that can be used to get distinct values. To get started, you need to create an index and specify the mapping for the fields you want to search. Then, you can use the search API to query the index and get the distinct values for the fields you want.

How do I get unique values in KQL?

Re: kql query for distinct values If that is not an issue then after you get your host and your displayName, you can concatenate (using the strcat command) and then perform another distinct on the concatenated string. Hope this is what you are looking for.

What is cardinality aggregation?

A single-value metrics aggregation that calculates an approximate count of distinct values. Values can be extracted either from specific fields in the document or generated by a script.

How do you count unique values in Kibana?

You can use a Metric visualization and just use the "count" metric for this. There are many ways to do this, generally in most visualizations, you can: use "Unique Count" on the personId field as the metric. use a terms aggregation on the organizationId field for the X-Axis (or split rows in a table visualization).


1 Answers

You can use the terms aggregation.

{
"size": 0,
"aggs" : {
    "langs" : {
        "terms" : { "field" : "language",  "size" : 500 }
    }
}}

The size parameter within the aggregation specifies the maximum number of terms to include in the aggregation result. If you need all results, set this to a value that is larger than the number of unique terms in your data.

A search will return something like:

{
"took" : 16,
"timed_out" : false,
"_shards" : {
  "total" : 2,
  "successful" : 2,
  "failed" : 0
},
"hits" : {
"total" : 1000000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
  "langs" : {
    "buckets" : [ {
      "key" : "10",
      "doc_count" : 244812
    }, {
      "key" : "11",
      "doc_count" : 136794
 
    }, {
      "key" : "12",
      "doc_count" : 32312
       } ]
    }
  }
}
like image 224
Anton Avatar answered Oct 09 '22 18:10

Anton