Apache Solr OR Syntax

Rolf van de Krol - October 8, 2009 - 10:25

I try to query Apache Solr to generate some search results.

<?php
$results
= apachesolr_search_execute('some keywords','tid:1','','my/base/path',0,'mymodule');
?>

this works perfectly well. The problems arise when i try to do this:

<?php
$results
= apachesolr_search_execute('some keywords','tid:1 (tid:2 OR tid:3)','','my/base/path',0,'mymodule');
?>

Somewhere I probably missed something, but to me, this should mean: give me all the nodes matching:

  • some keywords
  • assigned to term with tid = 1
  • assigned to term with tid = 2 or assigned to term with tid = 3

It doesn't work like this. My question is: how does it work? It it possible to query solr with this? And if yes: how?

Thanks in advance for your help.

this doesn't work either

jersu - October 15, 2009 - 01:43

I'm trying to solve something similar, I thought this would work, but does not, nor does char encoding the plus (&43;)

<?php
$results
= apachesolr_search_execute('some keywords','tid:1 tid:[2+OR+3]','','my/base/path',0,'mymodule');
?>

would require some extra work

jersu - October 15, 2009 - 17:58

After some more digging, found that solr is capable of searching with a param like fq=(tid:2+OR+tid:3), but at present, the Solr_Base_Query class doesn't like a filter passed in like that. It only allows range queries through, otherwise it wraps it in quotes. It also wants to set the filter like name:value, but we want to set it like (name:value+OR+name:value) ... a solution that would require some extra work.

From the Solr_Base_Query (in the apachesolr mod directory)

<?php
 
/**
   * Takes an array $field and combines the #name and #value in a way
   * suitable for use in a Solr query.
   */
 
public function make_filter(array $filter) {
   
// If the field value has spaces, or : in it, wrap it in double quotes.
    // unless it is a range query.
   
if (preg_match('/[ :]/', $filter['#value']) && !isset($filter['#start']) && !preg_match('/[\[\{]\S+ TO \S+[\]\}]/', $filter['#value'])) {
     
$filter['#value'] = '"'. $filter['#value']. '"';
    }
   
$prefix = empty($filter['#exclude']) ? '' : '-';
    return
$prefix . $filter['#name'] . ':' . $filter['#value'];
  }
?>

See the apachesolr_nodeacess

pwolanin - November 16, 2009 - 00:52

See the apachesolr_nodeacess module under the contrib/ dir for its construction of an OR fq parameter.

$query->add_subquery($subquery, 'OR');

---
Work: Acquia

 
 

Drupal is a registered trademark of Dries Buytaert.