Just hoping you could point me in the right direction. What I want to do, is provide a list of nid's to exclude from a search api solr based view.

With a standard view, i was doing this by implementing hook_views_query_alter and adding a condition of NOT IN for the array of nids to exclude. Any way I can achieve the same result using SOLR index?

Thanks in advance.

Comments

drunken monkey’s picture

Status: Active » Fixed

There are hook_search_api_query_alter() and hook_search_api_solr_query_alter() – and even hook_views_query_alter() might work (can't really say due to lack of docs).
With the former two, $query->getOption('search id') helps identify the search.

PS: Oh, there just is no "NOT IN" operator, you'll have to use multiple "<>" conditions.

modstore’s picture

Thanks for the fast and helpful reply as always.

I implemented the hook as follows:

function mymodule_search_api_query_alter($query) {
  $filter = $query->createFilter('AND');
  foreach ($exclude_nids as $nid) {
    $filter->condition('nid', $nid, '<>');
  }
  $query->filter($filter);
}

This works great, if I have less than 90 nids to exclude, but over that and I get the following error:
'413' Status: Request Entity Too Large

I guess if there is no way to have more than this, I will have to create a new indexed field and base my filter on that.

romenov’s picture

drunken monkey’s picture

@ romenov: Please don't give tips if you don't understand the problem at hand.

@ modstore: This is actually a Solr-specific restriction, see #1276970: Large queries break solr search. Basically, this would be fine with the Search API no matter how many values you want to exclude.

modstore’s picture

I see, thanks for the update, I will follow the other issue and go from there. Thanks mate.

modstore’s picture

Made the update as per link in #4 and it fixed it for me. Thanks Drunken Monkey.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.