0

I am trying to exclude terms in my search when using the element api. I have successfully managed to include terms and search them by fields in my entries. I would now like to be able to exclude different terms in those fields.

In this case 'recipeIngredients' is a matrix field.

Here is my criteria search so far :

//Get search query text
$searchQuery = Craft::$app->request->getQueryParam('searchQuery');
//Search in either title or ingredients
if ($searchQuery) $criteria['search'] = "title:'".$searchQuery."' OR recipeIngredients:'".$searchQuery."'";

//Get excluded ingredients query
$excludedQuery = Craft::$app->request->getQueryParam('xq');
if ($excludedQuery) {
    if (!isset($criteria['search'])) {
        $criteria['search'] = '';
    } else {
        $criteria['search'] = $criteria['search'].' AND ';
    }
    $criteria['search'] = $criteria['search']."recipeIngredients: not '".$excludedQuery."'";
}

//Get result count
$count = Craft::$app->request->getQueryParam( 'count' );
$count = $count ? $count : 10;//Default to ten if no parameter

return [
    'elementType'     => Entry::class,
    'criteria'        => $criteria,
    'elementsPerPage' => $count,
    'transformer'     => function ( Entry $entry ) {
        return [
            'name'      => $entry->title,
            'url'        => $entry->url,
            //'json_url'    => UrlHelper::url( "recipe/{$entry->id}.json" ),
            'picture_url' => $entry->recipeImage->one()->getUrl(),
            'portions'   => (float)$entry->recipePortions,
            'time'       => [
                'total' => (float)$entry->recipeTotalTime
            ]
        ];
    },
];

I can't figure out if I should be trying to append the excluded terms to the search query, and my syntax is wrong, or if there is another criteria property available to exclude results.

Also, the ElementQuery page mentionned in the docs no longer exists. I assume, it's this page, but I'm not certain.

4
  • Did you read the docs about how to exclude search terms? docs.craftcms.com/v2/searching.html - recipeIngredients: Commented May 9, 2018 at 20:11
  • That's the correct page, Rhys... have updated the link.
    – Brad Bell
    Commented May 9, 2018 at 20:47
  • 1
    @BradBell thanks for including the parseParam function and custom fields to the documentation. Now I can link to this page in about every third answer :-P Commented May 9, 2018 at 21:09
  • Thanks @RobinSchambach, that was the doc I was looking for. It wasn't mentioned in the ElementAPI docs, so I was a little lost to which search syntax I should be using. A reference in the API docs to that page could be a nice addition. I've answered my question with the solution. Thanks to the both of you! Commented May 10, 2018 at 15:58

1 Answer 1

1

Here is what I ended up changing my code to exclude search items.

//Get excluded ingredients query
$excludedQuery = Craft::$app->request->getQueryParam('xq');
if ($excludedQuery) {
    if (!isset($criteria['search'])) {
        $criteria['search'] = '';
    } else {
        $criteria['search'] = $criteria['search'].' ';
    }
    $criteria['search'] = $criteria['search']."-title:'".$excludedQuery ."' -recipeIngredients:'".$excludedQuery ."' ";
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.