I have a search api view.

I have an exposed filter for full text search.

In the filter settings, I am using search keys and in Query settings I have selected multiple terms.

I used this settings so that I can search for test1 AND test2 and test1 OR test2.

I want now to search for test1 NOT test2. So that it gives me the results which contains test1 but does not contains test2.

How can I do this?

enter image description here

I used the following code to make it work but is not working.

function mymodule_search_api_views_query_alter(&$view, &$query) {
  if ($view->name == 'search_view') {
    $keys =& $query->getKeys();
    if (!empty($keys) && in_array('NOT', $keys)) {
      $keys['#conjunction'] = 'OR';
      $keys['#negation'] = TRUE;
      $keys = array_diff($keys, array('OR'));

When I print the $query variable, it gives me the following output.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.