2

i am trying to get the unique values from my elasticsearch database.

So i want the unique names from my elasticsearch database.

So i am aggregation like so ---

   $paramss = [
            'index' => 'myIndex',
            'type' => 'myType',
            'ignore_unavailable' => true,
            'ignore' => [404, 500]
        ];


    $paramss['body'] = <<<JSON
{
"size": 0,
"aggs" : {
    "langs" : {
        "terms" : { "field" : "name" }
    }
}}
JSON;

        $results = $client->search($paramss);
        print_r(json_encode($results));

i get the result like so---

{
took: 3,
timed_out: false,
_shards: {
    total: 5,
    successful: 5,
    failed: 0
},
hits: {
    total: 1852,
    max_score: 0,
    hits: [

    ]
},
aggregations: {
    langs: {
        buckets: [
            {
                key: "aaaa.se",
                doc_count: 430
            },
            {
                key: "bbbb.se",
                doc_count: 358
            },
            {
                key: "cccc.se",
                doc_count: 49
            },
            {
                key: "eeee.com",
                doc_count: 46
            }
        ]
    }
}
}

But the problem is i am not getting all the unique values, I am getting only 10 values, which is default value for elasticsearch query.

So how can i change the query size !!!

i tried like so---

   $paramss = [
            'index' => 'myIndex',
            'type' => 'myType',
            'size' => 1000,
            'ignore_unavailable' => true,
            'ignore' => [404, 500]
        ];

which returns me some weird documents.

So do anyone knows the solution of this problem.

How can i get all the unique names from my elasticsearch database, can someone help me to fix this problem.

2 Answers 2

2

You are also doing everuthing right, except you the size.

The "size": 0 should come after the targeted field's name.

$client = new Elasticsearch\Client($params);
    $query['body'] = '{
        "aggs" : {
            "all_sources" : {
                "terms" : {
                    "field" : "source",
                    "order" : { "_term" : "asc" },
                    "size": 0
                }
            }
        }
    }';
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put size parameter inside terms:

{
"aggs" : {
    "langs" : {
        "terms" : { 
            "field" : "name",
            "size": 0
        }
    }
}}

Link to documentation where you can find more info: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

2 Comments

No, you didn't. You have size parameter on the same level as aggs.
why this answer is down voted. This is exactly correct answer. You have to define a size = 0 inside your aggregations otherwise ES will aggregate only top 20 of result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.