do_search

Versions
4.6
do_search($keywords, $type, $join = '', $where = '1', $variation = true)
4.7 – 5
do_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC')
6
do_search($keywords, $type, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC')

Do a query on the full-text search index for a word or words.

This function is normally only called by each module that support the indexed search (and thus, implements hook_update_index()).

The final query is an SQL select on the search_index table. As a guide for writing the optional extra SQL fragments (see below), use this query:

SELECT i.type, i.sid, i.word, SUM(i.score/t.count) AS score FROM {search_index} i $join INNER JOIN {search_total} t ON i.word = t.word WHERE $where AND i.type = '%s' AND (i.word = '...' OR ...) GROUP BY i.type, i.sid ORDER BY score DESC";

Parameters

$keywords A search string as entered by the user.

$type A string identifying the calling module.

$join (optional) A string to be inserted into the JOIN part of the SQL query. For example "INNER JOIN {node} n ON n.nid = i.sid".

$where (optional) A string to be inserted into the WHERE part of the SQL query. For example "(n.status > 0)".

$variation Used internally. Must not be specified.

Return value

An array of SIDs for the search results.

Related topics

▾ 3 functions call do_search()

do_search in modules/search.module
Do a query on the full-text search index for a word or words.
hook_search in developer/hooks/core.php
Define a custom search routine.
node_search in modules/node.module
Implementation of hook_search().

Code

modules/search.module, line 470

<?php
function do_search($keywords, $type, $join = '', $where = '1', $variation = true) {
  // Note, we replace the wildcards with U+FFFD (Replacement character) to pass
  // through the keyword extractor. Multiple wildcards are collapsed into one.
  $keys = preg_replace('!\*+!', '�', $keywords);

  // Split into words
  $keys = search_keywords_split($keys);

  $words = array();
  $arguments = array($type);
  $refused = array();
  // Build WHERE clause
  foreach ($keys as $word) {
    if (string_length($word) < variable_get('remove_short', 3)) {
      if ($word != '') {
        $refused[] = str_replace('�', '*', $word);
      }
      continue;
    }
    if (strpos($word, '�') !== false) {
      // Note: strtolower can be used because the value is only used internally.
      $words[] = "i.word LIKE '%s'";
      $arguments[] = str_replace('�', '%', strtolower($word));
    }
    else {
      $words[] = "i.word = '%s'";
      $arguments[] = strtolower($word);
    }
  }
  // Tell the user which words were excluded
  if (count($refused) && $variation) {
    $message = format_plural(count($refused),
                             'The word %words was not included because it is too short.',
                             'The words %words were not included because they were too short.');
    drupal_set_message(strtr($message, array('%words' => theme('placeholder', implode(', ', $refused)))));
  }

  if (count($words) == 0) {
    return array();
  }
  $conditions = $where .' AND i.type = \'%s\' AND ('. implode(' OR ', $words) .')';

  // Get result count (for pager)
  $count = db_num_rows(db_query("SELECT DISTINCT i.sid, i.type FROM {search_index} i $join WHERE $conditions", $arguments));
  if ($count == 0) {
    // Try out a looser search query if nothing was found.
    if ($variation && $loose = search_keywords_variation($keywords)) {
      return do_search($loose, $type, $join, $where, false);
    }
    else {
      return array();
    }
  }
  $count_query = "SELECT $count";

  // Do pager query
  $query = "SELECT i.type, i.sid, SUM(i.score/t.count) AS score FROM {search_index} i $join INNER JOIN {search_total} t ON i.word = t.word WHERE $conditions GROUP BY i.type, i.sid ORDER BY score DESC";
  $result = pager_query($query, 15, 0, $count_query, $arguments);

  $results = array();
  while ($item = db_fetch_object($result)) {
    $results[] = $item->sid;
  }

  return $results;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.