Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-10T15:57:28.130Z
Answers.LastActivityDate: 2018-05-10T15:57:28.130Z
Answers.UserDisplayName: Rhys Halsey
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T20:11:33.787Z
Comments.Text: Did you read the docs about how to exclude search terms? https://docs.craftcms.com/v2/searching.html `- recipeIngredients:`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T20:47:04.847Z
Comments.Text: That's the correct page, Rhys... have updated the link.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-09T21:09:04.117Z
Comments.Text: @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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-10T15:58:47.197Z
Comments.Text: 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!
Comments.UserDisplayName: Rhys Halsey
Tags: <search><plugin-elementapi>
Title: ElementAPI : Excluding terms from search criteria
ThreadText: # 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](https://github.com/craftcms/docs/blob/v3/en/element-queries.md) page mentionned in the [docs](https://github.com/craftcms/element-api#criteria) no longer exists. I assume, it's [this page](https://docs.craftcms.com/v3/element-queries.html), 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, username_0... have updated the link.  
  > **username_1**: @username_2 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 @username_1, 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 ."' ";
  }
  
  ```
  
  
  ---
  
  