Search code examples
solrfaceted-search

SOLR query with raw data and union multiple facet values


I am writing a SOLR query for faceted searching, and my fields can possibly have quotes or other bad characters in them, so I am using the raw operator to construct the query. However, if the user selects multiple facet values, I can't see how to use an OR in the query.

For example, the following returns results for a Manufacturer of Nike:

{!raw f=Manufacturer}Nike

The following returns results only for a Manufacturer of Adidas:

{!raw f=Manufacturer}Nike OR {!raw f=Manufacturer}Adidas

And the following returns no results:

{!raw f=Manufacturer}Nike OR Adidas

Is there a way to do this?


Solution

  • Have you considered escaping the special characters, instead of using the raw operator? http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Escaping%20Special%20Characters

    Escaping is pretty trivial to implement and the OR operator should work as expected. Here's an example of how to escape the special characters in PHP:

    static public function escapeSolrValue($string)
    {
        $match = array('\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '*', '?', ':', '"', ';', ' ');
        $replace = array('\\\\', '\\+', '\\-', '\\&', '\\|', '\\!', '\\(', '\\)', '\\{', '\\}', '\\[', '\\]', '\\^', '\\~', '\\*', '\\?', '\\:', '\\"', '\\;', '\\ ');
        $string = str_replace($match, $replace, $string);
    
        return $string;
    }
    

    Source: http://e-mats.org/2010/01/escaping-characters-in-a-solr-query-solr-url/