- Committed Gerhard's search module improvements.

4.1.x
Dries Buytaert 2002-11-18 19:10:44 +00:00
parent 15f1a61197
commit 57a9590a44
2 changed files with 180 additions and 66 deletions

View File

@ -9,7 +9,7 @@ function search_help() {
$output .= "<p>". t("You can also use wildcards, so 'walk*' will match 'walk', 'walking', 'walker', 'walkable' and 'walkability'... Alright you got me, I made the last ones up.") ."</p>";
$output .= "<p>". t("Searches are not case sensitive, regardless of how you type them all letters will be searched for in lower case") ."</p>";
$output .= "<b>". t("Words excluded from the search") ."</b>";
$output .= "<p>". t("Some words which commonly occur are filtered out by the searching process, these are commonly called 'noise words'. Examples are 'a, at, and, are, as, ask', and the list goes on. Words shorter than %number letters are also filtered from the search index.", array("number" => variable_get("minimum_word_size", 2)));
$output .= "<p>". t("Some words which commonly occur are filtered out by the searching process, these are commonly called 'noise words'. Examples are 'a, at, and, are, as, ask', and the list goes on. Words shorter than %number letters are also filtered from the search index.", array("%number" => variable_get("minimum_word_size", 2)));
$output .= "<p>". t("These words will never be matched when specified, even if they appear in the node you are searching for.");
return $output;
}
@ -51,7 +51,10 @@ function search_link($type) {
function search_admin() {
global $op, $id, $edit;
// Only allow people with sufficient access.
/*
** Only allow people with sufficient access.
*/
if (user_access("administer search")) {
switch ($op) {
case "Submit":
@ -103,21 +106,32 @@ function do_search($search_array) {
$type = $search_array["type"];
$select = $search_array["select"];
// Replace wildcards with mysql wildcards
/*
** Replace wildcards with mysql wildcards
*/
$keys = str_replace("*", "%", $keys);
// Split the words entered into an array
/*
** Split the words entered into an array
*/
$words = explode(" ", $keys);
foreach ($words as $word) {
// If the word is too short, and we've got it set to skip them,
// loop
/*
** If the word is too short, and we've got it set to skip them,
** loop
*/
if (strlen($word) < variable_get("remove_short", 0)) {
continue;
}
// If the word is proceeded by a "+", then this word is required, and
// pages that match other words, but not this one will be removed
/*
** If the word is proceeded by a "+", then this word is required, and
** pages that match other words, but not this one will be removed
*/
if (substr($word, 0, 1) == "+") {
$word = substr($word, 1);
$required = 1;
@ -128,15 +142,24 @@ function do_search($search_array) {
$required = 0;
}
// Put the next search word into the query and do the query
/*
** Put the next search word into the query and do the query
*/
$query = preg_replace("'\%'", $word, $select);
$result = db_query($query);
// If we got any results
/*
** If we got any results
*/
if (db_num_rows($result) != 0) {
$found = 1;
// Create an in memory array of the results,
/*
** Create an in memory array of the results,
*/
while ($row = db_fetch_array($result)) {
$lno = $row["lno"];
$nid = $row["nid"];
@ -146,7 +169,10 @@ function do_search($search_array) {
$name = $row["name"];
$count = $row["count"];
// If the just fetched row is not already in the table
/*
** If the just fetched row is not already in the table
*/
if ($results[$lno]["lno"] != $lno) {
$results[$lno]["count"] = $count;
@ -157,18 +183,28 @@ function do_search($search_array) {
$results[$lno]["uid"] = $uid;
$results[$lno]["name"] = $name;
// If this is a required word, set it to "valid"
/*
** If this is a required word, set it to "valid"
*/
if ($required == 1) {
$results[$lno]["valid"] = 1;
}
}
else {
// Different word, but existing "lno", increase the count of
// matches against this "lno" by the number of times this
// word appears in the text
/*
** Different word, but existing "lno", increase the count of
** matches against this "lno" by the number of times this
** word appears in the text
*/
$results[$lno]["count"] = $results[$lno]["count"] + $count;
// Another match on the a required word, increase valid
/*
** Another match on the a required word, increase valid
*/
if ($required == 1) {
$results[$lno]["valid"]++;
}
@ -178,10 +214,17 @@ function do_search($search_array) {
}
if ($found) {
// Black magic here to sort the results
/*
** Black magic here to sort the results
*/
array_multisort($results, SORT_DESC);
// OK, time to output the results.
/*
** OK, time to output the results.
*/
foreach ($results as $key => $value) {
$lno = $value["lno"];
$nid = $value["nid"];
@ -229,17 +272,25 @@ function update_index($search_array) {
$result = db_query($select);
if (db_num_rows($result)) {
// Wohoo, found some, look through the nodes we just selected
/*
** Wohoo, found some, look through the nodes we just selected
*/
while ($node = db_fetch_array ($result)) {
// Trash any existing entries in the search index for this node,
// in case its a modified node.
/*
** Trash any existing entries in the search index for this node,
** in case its a modified node.
*/
db_query("DELETE from search_index where lno = '". $node["lno"] ."' and type = '". $node_type ."'");
// Build the wordlist, teaser not included, as it then gives a
// false count of the number of hist, and doesn't show up
// when clicking on a node from the search interface anyway.
$wordlist = $node["text1"] . $node["text2"];
/*
** Build the wordlist, teaser not included, as it then gives a
** false count of the number of hits, and doesn't show up
** when clicking on a node from the search interface anyway.
*/
$wordlist = $node["text1"] ." ". $node["text2"];
// Strip heaps of stuff out of it
$wordlist = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $wordlist);
@ -263,8 +314,8 @@ function update_index($search_array) {
$wordlist = strtolower($wordlist);
// Remove "noisewords"
$noise = explode("|", $noisewords);
foreach ($noise as $word) {
$noise = explode("\r\n", variable_get("noisewords", ""));
foreach ($noise as $word) {
$wordlist = preg_replace("' $word '", " ", $wordlist);
}
@ -274,8 +325,11 @@ function update_index($search_array) {
// Make it an array
$eachword = explode(" ", $wordlist);
// walk through the array, giving a "weight" to each word, based on
// the number of times it appears in a page.
/*
** walk through the array, giving a "weight" to each word, based on
** the number of times it appears in a page.
*/
foreach ($eachword as $word) {
if (strlen($word) > $minimum_word_size) {
if ($newwords[$word]) {
@ -287,8 +341,11 @@ function update_index($search_array) {
}
}
// Walk through the weighted words array, inserting them into
// the search index
/*
** Walk through the weighted words array, inserting them into
** the search index
*/
foreach ($newwords as $key => $value) {
db_query("INSERT INTO search_index VALUES('$key', ". $node["lno"] .", '$node_type', $value)");
}
@ -313,7 +370,7 @@ function update_index($search_array) {
*/
function search_display($edit) {
$form = form_textfield(t("Minimum word length to index"), "minimum_word_size", $edit["minimum_word_size"], 10, 10, t("The number of characters a word has to be to be indexed. Words shorter than this will not be searchable."));
$form .= form_textfield(t("Minimum word length to search for"), "remove_short", $edit["remove_short"], 10, 10, t("The number of charachters a word has to be to be searched for."));
$form .= form_textfield(t("Minimum word length to search for"), "remove_short", $edit["remove_short"], 10, 10, t("The number of characters a word has to be to be searched for."));
$form .= form_textarea(t("Noise words"), "noisewords", $edit["noisewords"], 70, 10, t("These words will not be indexed, enter one word per line. Example: and, or, not, a, to, I, it, ..."));
$form .= form_select(t("Help text position"), "help_pos", $edit["help_pos"], array("1" => t("Above search form"), "2" => t("Below search form"), "3" => t("Link from above search form"), "4" => t("Link from below search form")), t("Where to show the help text for users on the search page."));
$form .= form_submit("Submit");

View File

@ -9,7 +9,7 @@ function search_help() {
$output .= "<p>". t("You can also use wildcards, so 'walk*' will match 'walk', 'walking', 'walker', 'walkable' and 'walkability'... Alright you got me, I made the last ones up.") ."</p>";
$output .= "<p>". t("Searches are not case sensitive, regardless of how you type them all letters will be searched for in lower case") ."</p>";
$output .= "<b>". t("Words excluded from the search") ."</b>";
$output .= "<p>". t("Some words which commonly occur are filtered out by the searching process, these are commonly called 'noise words'. Examples are 'a, at, and, are, as, ask', and the list goes on. Words shorter than %number letters are also filtered from the search index.", array("number" => variable_get("minimum_word_size", 2)));
$output .= "<p>". t("Some words which commonly occur are filtered out by the searching process, these are commonly called 'noise words'. Examples are 'a, at, and, are, as, ask', and the list goes on. Words shorter than %number letters are also filtered from the search index.", array("%number" => variable_get("minimum_word_size", 2)));
$output .= "<p>". t("These words will never be matched when specified, even if they appear in the node you are searching for.");
return $output;
}
@ -51,7 +51,10 @@ function search_link($type) {
function search_admin() {
global $op, $id, $edit;
// Only allow people with sufficient access.
/*
** Only allow people with sufficient access.
*/
if (user_access("administer search")) {
switch ($op) {
case "Submit":
@ -103,21 +106,32 @@ function do_search($search_array) {
$type = $search_array["type"];
$select = $search_array["select"];
// Replace wildcards with mysql wildcards
/*
** Replace wildcards with mysql wildcards
*/
$keys = str_replace("*", "%", $keys);
// Split the words entered into an array
/*
** Split the words entered into an array
*/
$words = explode(" ", $keys);
foreach ($words as $word) {
// If the word is too short, and we've got it set to skip them,
// loop
/*
** If the word is too short, and we've got it set to skip them,
** loop
*/
if (strlen($word) < variable_get("remove_short", 0)) {
continue;
}
// If the word is proceeded by a "+", then this word is required, and
// pages that match other words, but not this one will be removed
/*
** If the word is proceeded by a "+", then this word is required, and
** pages that match other words, but not this one will be removed
*/
if (substr($word, 0, 1) == "+") {
$word = substr($word, 1);
$required = 1;
@ -128,15 +142,24 @@ function do_search($search_array) {
$required = 0;
}
// Put the next search word into the query and do the query
/*
** Put the next search word into the query and do the query
*/
$query = preg_replace("'\%'", $word, $select);
$result = db_query($query);
// If we got any results
/*
** If we got any results
*/
if (db_num_rows($result) != 0) {
$found = 1;
// Create an in memory array of the results,
/*
** Create an in memory array of the results,
*/
while ($row = db_fetch_array($result)) {
$lno = $row["lno"];
$nid = $row["nid"];
@ -146,7 +169,10 @@ function do_search($search_array) {
$name = $row["name"];
$count = $row["count"];
// If the just fetched row is not already in the table
/*
** If the just fetched row is not already in the table
*/
if ($results[$lno]["lno"] != $lno) {
$results[$lno]["count"] = $count;
@ -157,18 +183,28 @@ function do_search($search_array) {
$results[$lno]["uid"] = $uid;
$results[$lno]["name"] = $name;
// If this is a required word, set it to "valid"
/*
** If this is a required word, set it to "valid"
*/
if ($required == 1) {
$results[$lno]["valid"] = 1;
}
}
else {
// Different word, but existing "lno", increase the count of
// matches against this "lno" by the number of times this
// word appears in the text
/*
** Different word, but existing "lno", increase the count of
** matches against this "lno" by the number of times this
** word appears in the text
*/
$results[$lno]["count"] = $results[$lno]["count"] + $count;
// Another match on the a required word, increase valid
/*
** Another match on the a required word, increase valid
*/
if ($required == 1) {
$results[$lno]["valid"]++;
}
@ -178,10 +214,17 @@ function do_search($search_array) {
}
if ($found) {
// Black magic here to sort the results
/*
** Black magic here to sort the results
*/
array_multisort($results, SORT_DESC);
// OK, time to output the results.
/*
** OK, time to output the results.
*/
foreach ($results as $key => $value) {
$lno = $value["lno"];
$nid = $value["nid"];
@ -229,17 +272,25 @@ function update_index($search_array) {
$result = db_query($select);
if (db_num_rows($result)) {
// Wohoo, found some, look through the nodes we just selected
/*
** Wohoo, found some, look through the nodes we just selected
*/
while ($node = db_fetch_array ($result)) {
// Trash any existing entries in the search index for this node,
// in case its a modified node.
/*
** Trash any existing entries in the search index for this node,
** in case its a modified node.
*/
db_query("DELETE from search_index where lno = '". $node["lno"] ."' and type = '". $node_type ."'");
// Build the wordlist, teaser not included, as it then gives a
// false count of the number of hist, and doesn't show up
// when clicking on a node from the search interface anyway.
$wordlist = $node["text1"] . $node["text2"];
/*
** Build the wordlist, teaser not included, as it then gives a
** false count of the number of hits, and doesn't show up
** when clicking on a node from the search interface anyway.
*/
$wordlist = $node["text1"] ." ". $node["text2"];
// Strip heaps of stuff out of it
$wordlist = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $wordlist);
@ -263,8 +314,8 @@ function update_index($search_array) {
$wordlist = strtolower($wordlist);
// Remove "noisewords"
$noise = explode("|", $noisewords);
foreach ($noise as $word) {
$noise = explode("\r\n", variable_get("noisewords", ""));
foreach ($noise as $word) {
$wordlist = preg_replace("' $word '", " ", $wordlist);
}
@ -274,8 +325,11 @@ function update_index($search_array) {
// Make it an array
$eachword = explode(" ", $wordlist);
// walk through the array, giving a "weight" to each word, based on
// the number of times it appears in a page.
/*
** walk through the array, giving a "weight" to each word, based on
** the number of times it appears in a page.
*/
foreach ($eachword as $word) {
if (strlen($word) > $minimum_word_size) {
if ($newwords[$word]) {
@ -287,8 +341,11 @@ function update_index($search_array) {
}
}
// Walk through the weighted words array, inserting them into
// the search index
/*
** Walk through the weighted words array, inserting them into
** the search index
*/
foreach ($newwords as $key => $value) {
db_query("INSERT INTO search_index VALUES('$key', ". $node["lno"] .", '$node_type', $value)");
}
@ -313,7 +370,7 @@ function update_index($search_array) {
*/
function search_display($edit) {
$form = form_textfield(t("Minimum word length to index"), "minimum_word_size", $edit["minimum_word_size"], 10, 10, t("The number of characters a word has to be to be indexed. Words shorter than this will not be searchable."));
$form .= form_textfield(t("Minimum word length to search for"), "remove_short", $edit["remove_short"], 10, 10, t("The number of charachters a word has to be to be searched for."));
$form .= form_textfield(t("Minimum word length to search for"), "remove_short", $edit["remove_short"], 10, 10, t("The number of characters a word has to be to be searched for."));
$form .= form_textarea(t("Noise words"), "noisewords", $edit["noisewords"], 70, 10, t("These words will not be indexed, enter one word per line. Example: and, or, not, a, to, I, it, ..."));
$form .= form_select(t("Help text position"), "help_pos", $edit["help_pos"], array("1" => t("Above search form"), "2" => t("Below search form"), "3" => t("Link from above search form"), "4" => t("Link from below search form")), t("Where to show the help text for users on the search page."));
$form .= form_submit("Submit");