You are here

function search_service_nodes in Services 6.2

Same name and namespace in other branches
  1. 6 services/search_service/search_service.inc \search_service_nodes()
  2. 7 services/search_service/search_service.inc \search_service_nodes()

Return search results for nodes.

Parameters

$keys: The keywords to search for within the nodes.

$simple: When set to TRUE, only the fields indicated in $stdkeys will be returned. This can be helpful to limit the size of the search results.

$fields: An array of the node properties that should be returned. When $simple is not set, a full node object is returned with each result. You can limit the properties of these objects to only the ones you need by specifying them in this array. Again, this gives the opportunity to limit your result set.

Return value

An array of search results. If $simple is TRUE, this array will contain only results and no node objects. If $simple is FALSE the array will contain both results and full node objects, possibly limited by the properties indicated in $fields.

See also

hook_search()

node_search()

1 string reference to 'search_service_nodes'
search_service_service in services/search_service/search_service.module
Implementation of hook_service().

File

services/search_service/search_service.inc, line 31
Link general search functionalities to services module.

Code

function search_service_nodes($keys, $simple = FALSE, $fields = array()) {

  // define standard keys for simple set
  $stdkeys = array(
    'node',
    'link',
    'type',
    'title',
    'user',
    'date',
    'snippet',
  );

  // invoke the search hook to generate results
  $results = module_invoke('node', 'search', 'search', $keys);
  if ($results && is_array($results) && count($results)) {

    // if simple results requested, remove extra data
    if ($simple) {
      $num = count($results);
      for ($i = 0; $i < $num; $i++) {
        $keys = array_keys($results[$i]);
        foreach ($keys as $key) {
          if ($key == 'node') {
            $results[$i][$key] = $results[$i]['node']->nid;
          }
          if (!in_array($key, $stdkeys)) {
            unset($results[$i][$key]);
          }
        }
      }
    }
    else {
      foreach ($results as $key => $result) {
        $result['node'] = services_node_load($result['node'], $fields);
        $results[$key] = $result;
      }
    }
    return $results;
  }
  return services_error(t('Search returned no results.'), 404);
}