<p>Here is what I ended up changing my code to exclude search items. </p>

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

CC BY-SA 4.0
2018-05-10T15:57:28.130Z
2018-05-10T15:57:28.130Z
Rhys Halsey
<p>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. </p>

<p>In this case 'recipeIngredients' is a matrix field. </p>

<p>Here is my criteria search so far : </p>

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

//Get excluded ingredients query
$excludedQuery = Craft::$app-&gt;request-&gt;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-&gt;request-&gt;getQueryParam( 'count' );
$count = $count ? $count : 10;//Default to ten if no parameter

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

<p>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. </p>

<p>Also, the <a href="https://github.com/craftcms/docs/blob/v3/en/element-queries.md" rel="nofollow noreferrer">ElementQuery</a> page mentionned in the <a href="https://github.com/craftcms/element-api#criteria" rel="nofollow noreferrer">docs</a> no longer exists. I assume, it's <a href="https://docs.craftcms.com/v3/element-queries.html" rel="nofollow noreferrer">this page</a>, but I'm not certain. </p>

CC BY-SA 4.0
2018-05-09T20:11:33.787Z
Did you read the docs about how to exclude search terms? https://docs.craftcms.com/v2/searching.html `- recipeIngredients:`
Robin Schambach
CC BY-SA 4.0
2018-05-09T20:47:04.847Z
That's the correct page, Rhys... have updated the link.
Brad Bell
CC BY-SA 4.0
2018-05-09T21:09:04.117Z
@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
Robin Schambach
CC BY-SA 4.0
2018-05-10T15:58:47.197Z
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!
Rhys Halsey
<search><plugin-elementapi>
ElementAPI : Excluding terms from search criteria
# ElementAPI : Excluding terms from search criteria

(Asked by: username_0 on 2018-05-09)

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.

**Tags:** search, plugin-elementapi
> **Comments:**
> **username_1**: Did you read the docs about how to exclude search terms? https://docs.craftcms.com/v2/searching.html `- recipeIngredients:`  
> **username_2**: That's the correct page, Rhys... have updated the link.  
> **username_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  
> **username_0**: 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!  

---

## 1 answers

### Answer by username_0 on 2018-05-10. Score: 1 | Accepted answer

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 ."' ";
}


---

