3

I need to make a query in the following form:

SELECT * FROM {index}
...
AND (
  (field1 = value AND field2 = value) OR
  (field1 = other_value AND field2 = other_value)
)
...

But it doesn’t seem to be possible with the existing createFilter and condition methods, as there is no way to pass another filter to a condition for example. Am I missing something, or is this (basic functionality?) really not there?

1

If you mean from UX, then it not has been implemented yet as the code states (file search_api/includes/query.inc):

// @todo Add fourth mode for complicated expressions, e.g.:
// "vanilla ice" OR (love NOT hate)

See: Parse mode for complicated/complex expressions

This should be implemented soon in Drupal 8.

If you mean programatically, you can write your own filter, as suggested.

Or by installing Apache Solr and using Search API Solr instead.

4

The AND ((field1 = value AND field2 = value) OR (field1 = other_value AND field2 = other_Value)) IS possible with sub-filters. For anyone else who ends up here trying to sort this out (as I did before working out the correct solution) you can create the above like this:

$filter = $query->createFilter('OR');

$subfilter1 = $query->createFilter('AND');
$subfilter1->condition('field1',value);
$subfilter1->condition('field2',value);

// Add this subfilter to the main filter.
$filter->filter($subfilter1);

$subfilter2 = $query->createFilter('AND');
$subfilter2->condition('field1',other_value);
$subfilter2->condition('field2',other_value);

// Now add the second subfilter
$filter->filter($subfilter2);

// Apply the filter to the query.
$query->filter($filter);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.