Search code examples
amazon-cloudsearchaws-php-sdk

How to request facets from AWS CloudSearch via the PHP v2 SDK?


I am using v2 of the PHP SDK for interfacing requests to AWS CloudSearch. The documentation is here, however details and examples are somewhat lacking. I need to include facets in my search. It is clearly asking for a string but it is not clear exactly how the string should be formatted.

$cloudSearchDomainClient->serach([
    'filterQuery' => $filter,
    'query' => $query,
    'queryParser' => 'lucene',
    'facet' => '???'
]);

I have tried, for example:

'facet' => 'field1,field2,field3'

'facet' => 'facet.fieldname={sort:'count',size:5}'

'facet' => 'fieldname={sort:'count',size:5}'

(Some permutations based on examples in the non-sdk request descriptions here)


Solution

  • The appropriate syntax appears to be a string representing a javascript object.

    For the default sort/count:

    'facet' => '{fieldname:{}}'
    

    To specify sorting options:

    'faceet' => '{fieldname:{'sort':'count',size:5}}'
    

    To request multiple facets:

    'facet' => '{field1:{},field2"{}}"
    

    Etc.