You are here

function fuzzysearch_index_insert in Fuzzy Search 6

Insert the words into the database as they are indexed.

Parameters

$word: Word to insert into the index.

$nid: The node id that is to be associated with this word.

$word_score: Score given to the word based on the tag it is in.

$node_score: Score modifier given to the node from hook_search_score.

1 call to fuzzysearch_index_insert()
fuzzysearch_index in ./fuzzysearch.module
Index the node data in the fuzzy index table.

File

./fuzzysearch.module, line 398
Module file for fuzzysearch module.

Code

function fuzzysearch_index_insert($word, $word_id, $nid, $word_score, $node_score) {

  // Cleanse and remove spaces.
  $word = str_replace(' ', '', fuzzysearch_cleanse($word));
  $length = drupal_strlen($word);
  $nlength = variable_get('fuzzysearch_ngram_length', 3);

  //  Ensure that having all score modifiers set to 0 will not affect our natural scoring
  if ($node_score > 0) {
    $score = $word_score * $node_score;
  }
  else {
    $score = $word_score;
  }
  if ($length > $nlength) {

    //  Calculate how complete the ngram is compared to the length of the word
    $completeness = number_format(100 / ($length - $nlength + 1), 3);

    //  Create ngrams and index them
    for ($i = 0; $i < $length - $nlength + 1; $i++) {
      db_query("INSERT INTO {fuzzysearch_index} (nid, word_id, ngram, completeness, score) VALUES (%d, %d, '%s', %f, %f)", $nid, $word_id, drupal_substr($word, $i, $nlength), $completeness, $score);
    }
  }
  elseif ($length == $nlength) {

    //  The ngram is the same length as the actual word so it is complete
    $completeness = 100;

    //  Index the ngram
    db_query("INSERT INTO {fuzzysearch_index} (nid, word_id, ngram, completeness, score) VALUES (%d, %d, '%s', %f, %f)", $nid, $word_id, $word, $completeness, $score);
  }
}