简体   繁体   中英

How to encode php array to Amazon CloudSearch filterquery

I have an array of fields & values that I wish to pass to the Amazon CloudSearchDomain as a filterQuery argument using one or both the 'and' or 'or' operators.

For example, I'd like to pass a single 'and' field (a category) and a second 'and' field which is comprised of several 'or' values:

$filters = array(
   'category' => 'cat name',
   'colour'   => array(
      'red',
      'green'
   )
);

I have tried:

$results = $cloudSearchClient->search(array(
   'filterQuery' => "(and(category:'cat name') (or (colour:'red') (colour:'green')))"
));

So the search would find items that match 'cat name' AND are red OR green

I can't seem to pass the correct syntax manually, and would then also like a good clean solution to convert the PHP array into the correct AWS syntax please.

You might want to check two things here. First is when you filter within the facet array you are using the 'OR' operator and if you are filtering between two facet arrays you are using 'AND'. Judging by your usage you want to display the results when a user selects either "red" or "green". Eg. filterQuery which you return should have the query --> "(or color:'red'(or color:'green'))"

Second is monitor the query that gets passed to the aws URL. Your filter query might be correct but the URL string needs to appended with html encoded space. For example http://your-search-domain/2013-01-01/search?&q.parser=structured&q.options=%7Bsomefield%27%2C+%27&sort=something%20 &fq=%28or+color%3A%27red%27%28or+color%3A%27green%27%29%29 ....."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM