简体   繁体   中英

Query of distinct in elastic search

SQL Query : SELECT DISTINCT column FROM table_name WHERE [condition]

We want to apply same query in elastic search, where i can find distinct values of column in search result.

For example we have index of users (userindex) field is information where school name or company name of the user in indexed. let there are users with same school name. i want all the distinct school name from the index

As keety stated in the comments, one possibility is to use a terms aggregation like the following:

curl localhost:9200/users/_search?pretty=1 -d 
{
    "aggs": {
        "schools": {
            "terms": {
                "field": "schoolname"
            }
        }
    }
}

Depending on your use case this might already be enough. But you should keep in mind that the buckets ES returns for the aggregation are somewhat limited and possibly inaccurate concerning pagination and counts.

See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM