I need to make a query in the following form:

SELECT * FROM {index}
  (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?


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');

// Add this subfilter to the main filter.

$subfilter2 = $query->createFilter('AND');

// Now add the second subfilter

// Apply the filter to the query.

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.

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.