siva
siva

Reputation: 556

Get unique values of a field in elasticsearch and get all records

I am using elasticsearch 2x version

I want distinct values in a field. I am getting only 10 values in the query. How do I change this to view all distinct records?

Following is the query I am using:

GET messages-2017.04*/_search
{
    "fields": ["_index"],
    "query": {
        "bool": {
            "must":{
                "bool":{
                "should": [
                        {
                            "match": {
                                "RouteData": {
                                    "query": "Q25B",
                                    "type": "phrase"
                                }
                           }
                        }
                    ]
               }
            }
        }
    }
}

I need to get all distinct _index names from the DB.

Upvotes: 1

Views: 2892

Answers (1)

Val
Val

Reputation: 217514

You need to use a terms aggregation instead, like this:

POST messages-2017.04*/_search
{
    "size": 0,
    "query": {
        "bool": {
            "must":{
                "bool":{
                "should": [
                        {
                            "match": {
                                "RouteData": {
                                    "query": "Q25B",
                                    "type": "phrase"
                                }
                           }
                        }
                    ]
               }
            }
        }
    },
    "aggs": {
       "all_indexes": {
          "terms": {
             "field": "_index",
             "size": 100
          }
       }
    }
}

Upvotes: 1

Related Questions