Executing a search in code

Last updated on
24 March 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

If you want to call Search API from your own module you need to create a SearchApiQuery object. For example, to display the indexed data for all 'product' nodes with a category of 'book' or 'magazine' you might use:

// Replace "node_index" with you index's machine name.
$query = search_api_query('node_index');

$query->condition('type', 'product', '=');
$filter = $query->createFilter('OR');
$filter->condition('field_category', 'book', '=');
$filter->condition('field_category', 'magazine', '=');
$query->filter($filter); 

$data=$query->execute();
$results=$data['results'];
print_r($results);

Help improve this page

Page status: No known problems

You can: