phpsymfonysolrsolarium

Symfony Nelmio Solarium Bundle FilterQuery


When I execute this code in solr API

....fq=title:(bionformatics OR scince)...

It works fine and return data that fit to Filter Query.

But when I try the same query in Solarium bundle

$client = $this->solr->getClient();
$query = $client->createSelect();
$query->setFilterQueries('title:("bionformatics" OR "scince")');

I get this error

Catchable Fatal Error: Argument 1 passed to Solarium\QueryType\Select\Query\Query::addFilterQueries() must be of the type array, string given, called in /var/www/html/conference/vendor/solarium/solarium/library/Solarium/QueryType/Select/Query/Query.php on line 737 and defined

I must notice that Solarium bundle works well with

$query->setQuery('title:"bionformatics" OR title:"scince"');


Solution

  • The error message is telling you exactly what the problem is.

    try:

    $query->setFilterQueries(array('titleFilter' => 'title:("bioinformatics" OR "science")'));
    

    Or since you only want to add a single Filterquery use the documented way from the solarium docs (http://solarium.readthedocs.io/en/stable/queries/select-query/building-a-select-query/adding-filterqueries/)

    // get a select query instance
    $query = $client->createSelect();
    
    // create a filterquery
    $query->createFilterQuery('maxprice')->setQuery('price:[1 TO 300]');
    
    // this executes the query and returns the result
    $resultset = $client->select($query);
    

    When you look at this example it is also clear why setFilterQueries uses the array syntax, since every filterquery needs its unique identifier.