You are here

function search_facetapi_search_execute in Faceted Navigation for Search 7

Implements hook_search_execute().

File

./search_facetapi.module, line 105
Integrates the core Search module with the Facet API project.

Code

function search_facetapi_search_execute($keys = NULL, $conditions = NULL) {

  // Build matching conditions.
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('node', 'n', 'n.nid = i.sid');
  $query
    ->condition('n.status', 1)
    ->addTag('node_access')
    ->searchExpression($keys, 'node');

  // Ensures the adapter is valid.
  if (!($adapter = facetapi_adapter_load('search'))) {
    return array();
  }

  // Sets search keys and adds active filters.
  $adapter
    ->setSearchKeys($keys);
  $adapter
    ->addActiveFilters($query);

  // Only continue if the first pass query matches.
  $adapter->hasMatches = $query
    ->executeFirstPass();
  if (!$adapter->hasMatches) {
    return array();
  }

  // Add the ranking expressions.
  _node_rankings($query);

  // Executes results, gets and stores the result count.
  global $pager_total_items;
  $element = PagerDefault::$maxElement;
  $find = $query
    ->limit(10)
    ->execute();
  $adapter
    ->setResultCount($pager_total_items[$element]);

  // Builds result rows.
  $results = array();
  foreach ($find as $item) {

    // Render the node.
    $node = node_load($item->sid);
    $build = node_view($node, 'search_result');
    unset($build['#theme']);
    $node->rendered = drupal_render($build);

    // Fetch comments for snippet.
    $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
    $extra = module_invoke_all('node_search_result', $node);
    $uri = entity_uri('node', $node);
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => check_plain(node_type_get_name($node)),
      'title' => $node->title,
      'user' => theme('username', array(
        'account' => $node,
      )),
      'date' => $node->changed,
      'node' => $node,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $node->rendered),
      'language' => $node->language,
    );
  }
  return $results;
}