How do I get only node IDs from the search results? 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-&gt;query();
$query-&gt;keys($search_terms);
$query-&gt;fields(array('nid')); // doesn't work
$results = $query-&gt;execute();
return $results;


How do I get only node IDs from $results?
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-&gt;query();
$query-&gt;keys($search_terms);
$query-&gt;condition('nid', $val); // The third parameter "operator" defaults to "="
$results = $query-&gt;execute();
return $results;