3

I want to add an indexed field to a Search API query in code. My query currently looks like this:

$index = search_api_index_load("default_node_index");
$query=new SearchApiQuery($index);
$query->condition('type', 'answers_question', '=');
$filter = $query->createFilter('OR');
$filter->condition('title', $text, '=');
$filter->condition('body:value', $text, '=');
$query->sort('search_api_relevance', DESC);
$query->fields(array('title'));
$query->filter($filter);
$query->keys($text);
$data=$query->execute();

This returns the correct results, however the result set is composed of an array of arrays with an 'id' and a 'score'. I want the indexed node title to be returned as well. I cannot do a join to the node table as ->join is not available on SearchApiQuery.

The statement:

$query->fields(array('title'));

does not seem to do anything.

I have this same query set up in a view where it is working properly. When I inspect the query at hook_search_api_query_alter I cannot see where or how the title is added.

How do I add the indexed title as a field to be returned in the result set?

2

1 Answer 1

0
$query->fields(array('title'));

Doesn't do what you think it does/should, this tells Search API that this is one of your fulltext fields that you want querying against the string you passed to keys()

$query->keys($text);

What backend are you using for Search API, in my experience Solr returns all indexed non empty fields. Make sure the Title is indexed so that it is available to your Search API's backend.

Your Answer

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

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