Skip to main content
1 of 2
Jason Gray
  • 762
  • 8
  • 14

Programmatical use of Search API

Problem: Need to run my own searches programmatically with Search API. Every google result I find is geared toward teaching non-developers how to use the module with Views with no results on doing it like I want.

Information:

  • Using local database service instead of Solr or any other.
  • Have data not stored in database that will appear alongside other results.
  • Content will be grouped in "type" tabs.
  • Not using Views for various reasons.
  • Have Facets API enabled but no clue how to use it either.

Why: Because I want to have the search page be at /site-search and without /node/keyword after the page argument. Also will be asked to customize this beyond the scope of other modules that may provide the same type of functionality. And I want to learn how to do this but have not found any tutorials or other insight on how to accomplish it.

The rest: I have created my search page and on it I am executing the search with:

$term = $_SESSION['my-search']['term'];
$server = search_api_server_load('database');
$index = search_api_index_load('default_node_index');
$query = new SearchApiQuery($index);
$query->keys($term); 
$query->fields(array('title','body:value')); 
$srv= new SearchApiDbService($server);
$result = $srv->search($query);

The session variable is set when a search form is submitted then redirects the user to /site-search to perform the search and show the results.

If I output $result I see it's an array with different information including a result set that is an array of node ids and their scores.

I could run through those, grabbing the node and displaying the results myself but I figure there's got to be some other method for rendering the results of the search. I haven't found it though and that's the problem.

I've been looking through the different classes in Search API and Facet API and can't find anything in them that seem like they would build result output.

Any help with this will be appreciated.

Jason Gray
  • 762
  • 8
  • 14