amazon-web-servicesamazon-cloudsearch

AWS Amazon Cloud Search Expression Definition using PHP API


I ran into some issues doing Expression Definition via the official AWS Cloud Search API.

The code example in the docs is mangled and doesn't actually show what the API is actually expecting:

https://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.CloudSearchDomain.CloudSearchDomainClient.html#_search


Solution

  • After much experimentation I found the correct format for defining expressions via the PHP API.

    Hopefully this saves someone else some time.

    So the generic AWS Cloud Search docs for expression writing will show something like this:

    expr.score_by_date=_score*(_time - publish_date)
    

    Which you then use in a search like this:

    search?q=terminator& expr.score_by_date=_score*(_time - publish_date)&sort=score_by_date desc
    

    But in the API you'll want to define it as an argument that gets passed to the search method in the CloudSearchDomainClient class.

    $search_args being an array of the various arguments that the search method accepts.

    $search_args['expr'] = '{score_by_date: "_score*(_time - post_date)"}';
    

    You then modify the sort argument to match like this:

    $search_args['sort'] = 'score_by_date desc';