4

Has anyone succeeded in filtering array values using Apache solr? I have nids as an array, programmatically I want to use the array to filter the solr based on the 'entity_id'.

I tried something like

  $solr = apachesolr_get_solr();
  $query = apachesolr_drupal_query("custom", array('q' => ''),'sort_label asc', 'search/site');
  $query->addFilter('entity_id', '[10915 11013]');
  $response = $query->search();

But I don't get the desired results, somewhere the apachesolr_search or apachesolr module seems to be adding quotes to the filter values something like

webapp=/solr path=/select params={sort=sort_label+asc&start=0&q=&json.nl=map&wt=json&fq=entity_id:"[10915+11013]"&rows=10} status=500

2 Answers 2

4

I finally figured it out. Just a small change to what Clive suggested will solve the issue.

$query->addParam('fq', "entity_id:(10915 OR 11013)");

The above code gives me the expected response, i.e., result of node 10915 and 11013.

Hope this helps :)

1

addFilter can't guess what your intentions are, so it assumes you're passing a string value for the filter - which is then properly escaped so Solr doesn't misinterpret special chars as part of it.

If you want a more complex condition, you need to go a bit more manual:

$query->addParam('fq', "entity_id:[10915 11013]");

(that's assuming field:[x,y] is valid Solr query syntax for an IN operation, I don't know that it is; you might have to use a basic OR instead).

1
  • Thanks for your response @Clive, but I still get the same response webapp=/solr path=/select params={sort=sort_label+asc&start=0&q=&json.nl=map&wt=json&fq=entity_id:"[10915+11013]"&rows=10} status=500 in solr. FYI, I am using solr 3.5
    – Suresh R
    Commented Dec 11, 2015 at 12:14

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.