1

I'm using Search API Solr with custom searches in code, but I want to find a way to limit the return results to only the nid to save on bandwidth with our third-party solr server. I've tried $query->fields(array('nid')) but I receive the following error.

SearchApiException: Trying to search on field nid which is no indexed fulltext field.

I am using the following code (simplified).

$index = search_api_index_load('my_index');
$query = $index->query();
$query->keys($search_terms);
$query->fields(array('nid')); // doesn't work
$results = $query->execute();
return $results;

How do I get only node IDs from $results?

0

This is because the fields() method of the SearchAPIQuery class makes the search in fields with datatype fulltext.

In order to get the nid in the result, you have to ensure that its value is being indexed by search_api. You can check that at /admin/config/search/search_api/index/my_index/fields (my_index is the name of the index in this case).

To filter using the nid field you might have to use the condition method instead. The following should work:

$index = search_api_index_load('my_index');
$query = $index->query();
$query->keys($search_terms);
$query->condition('nid', $val); // The third parameter "operator" defaults to "="
$results = $query->execute();
return $results;
  • To clarify I don't want to filter by nid (I'm actually using lots of filters already), I only want to return the nids - I don't need all the extra fields as I'm doing a node_load on them afterwards. – Chris Jul 2 '17 at 23:44
  • @Chris nids are returned by the search_api when they are indexed (first part of the answer). Can you make sure that it is being indexed? – AjitS Jul 3 '17 at 4:54

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.