Facets not working as per docs #50

Closed
designermonkey opened this Issue Mar 5, 2014 · 4 comments

Projects

None yet

2 participants

@designermonkey

If I use:

{
    "query" : {
        "match_all" : {  }
    },
    "facets" : {
        "tag" : {
            "terms" : {
                "field" : "tag",
                "all_terms" : true
            }
        }
    }
}

from the documentation, as a PHP array converted to json by the php-api, the all_terms part doesn't work.

@polyfractal
Member

Do you mean you are receiving an exception from Elasticsearch, a PHP parse error or just not returning the results that you expect?

Everything appears to be working fine on my end:

$client = new Elasticsearch\Client();

$query = array(
    'query' => array(
        'match_all' => array()
    ),
    'facets' => array(
        'tag' => array(
            'terms' => array(
                'field' => 'tag',
                'all_terms' => true
            )
        )
    )
);
$params['body'] = $query;
$params['index'] = 't';
$params['type'] = 't';
$ret = $client->search($params);

print_r($ret);
Array
(
    [took] => 19
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 3
            [max_score] => 1
            [hits] => Array
            ( ... )
        )
    [facets] => Array
        (
            [tag] => Array
                (
                    [_type] => terms
                    [missing] => 1
                    [total] => 6
                    [other] => 0
                    [terms] => Array
                        (
                            [0] => Array
                                (
                                    [term] => z
                                    [count] => 1
                                )
                            [1] => Array
                                (
                                    [term] => y
                                    [count] => 1
                                )
                            [2] => Array
                                (
                                    [term] => x
                                    [count] => 1
                                )
                            [3] => Array
                                (
                                    [term] => c
                                    [count] => 1
                                )
                            [4] => Array
                                (
                                    [term] => b
                                    [count] => 1
                                )
                            [5] => Array
                                (
                                    [term] => a
                                    [count] => 1
                                )
                        )
                )
        )
)
@designermonkey

Should have been clearer, was in a rush, sorry.

It still truncates at 10 facet results for me which is the default, when I have 13 possible results.

I'm on ES v1.0

@polyfractal
Member

Ah, that's just Elasticsearch's behavior.

The all_terms parameter just controls if terms with zero counts show up in the results. The results that are actually returned are still subject to the facet size parameter, which is 10 by default. You need to increase the facet size to get more results back (regardless of their count):

$query = array(
    'query' => array(
        'match_all' => array()
    ),
    'facets' => array(
        'tag' => array(
            'terms' => array(
                'field' => 'tag',
                'all_terms' => true,
                'size' => 200
            )
        )
    )
);
@designermonkey

Ahhh, ok, no problem. I have switched it to the size parameter anyway, thanks for clearing that up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment