I am trying to create a module that executes a search -- I tried to utilize the documentation for calling a search in code but

$index = \Drupal\search_api\Entity\Index::load('INDEX_ID');

gives me $index == NULL so the very second line

$query = $index->query();

blows it up. I tried replacing 'INDEX_ID' with 'default_index' but that didn't help. Search API is definitely enabled. Any idea why \Drupal\search_api\Entity\Index::load would keep giving me NULL?

1 Answer 1


That is because you need to create an index first.

Go to


and create your index then replace the INDEX_ID in your code with the id you have set for the new index.

You might have to create a server too if you don't have any configured. I have used the default DB Search API. Enable Database Search.


Then create the server where you created the index:


Then configure the index to use the server and enable the index.

Don't forget to process the indexing for your new index. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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