How do I get only node IDs from the search 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;