Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 09-07-2004, 07:34 PM   #1 (permalink)
Upright
 
[php/mysql]search queries and handling

I'm having some problems implementing my concept of searching on my blog [www.binaryelysium.com].

This is the way I am attempting to run a query. Searcher submits terms ie: "large red clowns."

What I want to do is give results in the following order (top with highest priority):
  1. results that include all terms
  2. results that include all terms minus one
  3. results that include all terms minus two
  4. and so on until one term remains this one is displayed at the very end

So... for the above example the user would get:
  • posts with the the striing "large red clowns"
  • posts with the string "large red"
  • posts with the string "large clowns"
  • posts with the string "red clowns"
  • posts with the string "large"
  • posts with the string "red"
  • posts with the string "clowns"

But the last three ordered in such a manner of being most recent.
Does anyone have any idea how I could go about doing it?
Outpour is offline  
Old 09-09-2004, 04:36 AM   #2 (permalink)
Upright
 
You could seperate the search string into words, and then generate a really bigass where clause with ANDs and ORs....

Or, seperate the searches into multiple queries and use mysql's UNION operation to concat the results together.
bacon is offline  
Old 09-09-2004, 11:26 AM   #3 (permalink)
Loser
 
Location: RPI, Troy, NY
When I was writing my first search engine, I thought of doing this. Then I realized that Google only includes results that have all terms. The difference is that they show results that are pages that link to pages that have your search terms. Basically, you don't need to do what you're saying, because you can just construct people to search more generally (less terms).
rukkyg is offline  
Old 09-09-2004, 04:20 PM   #4 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
There are really two issues here. Querying matches from the database is one. The other is ranking the responses by (let's call it) keyword density.

The first is easy--just split your query terms on spaces and do an OR query for each of them.

Then run a post-query ranking process where you test each record returned from your query for the presence of each of your search terms, and scores them. Then sort your responses by score, and bob's yer uncle.

Like this:

Code:
//Let's say that array $responses has strings of response values.

foreach ($responses as $record) {
   $result[$i++][record] = $record;
   foreach ($keyword as $word) {
       if (ereg($word, $record)) {
           ++$result[$i][score];
       }
    }
}

function user_sort ($a, $b) {
     if ($a[score] > $b[score]) {
         return 1;
      } elseif ($b[score] > $a[score]) {
         return -1;
      } else { return 0; }
}

sort($result, 'user_sort');
Note that I didn't actually run this code--this is strictly off-the-top-of-my-head. YMMV greatly.
ratbastid is offline  
Old 09-12-2004, 01:50 PM   #5 (permalink)
Upright
 
So to do the actual query would you do seperate queries for each term?

PHP Code:
//Let $_GET['q'] be the search terms
$search explode(" ",$_GET["q"]);
foreach (
$search as $term) {
   
$query "SELECT DISTINCT * FROM table WHERE field LIKE '%$term%' OR field2 LIKE '$term%' OR field3 LIKE '%$term%'";

Obviously that itself won't work.. $query would be over written each time the foreach loops. How would you do a query on all the terms and combine them into one large array?
__________________
Nihilism is the organic, reasoned response to artificial chaos, the intentional chaos manufactured by government, religion and mass-media.
Outpour is offline  
Old 09-12-2004, 02:06 PM   #6 (permalink)
Upright
 
egh ugly highlight colors.

Anyways i figured out how to combine all the queries:

Code:
$search = explode(" ",$_GET["q"]);
$query = '';
foreach ($search as $term) {
	$query .= "SELECT DISTINCT * FROM table WHERE field LIKE '%$term%' OR field2 LIKE '$term%' OR field3 LIKE '%$term%' UNION ";
} 
$query = substr($query, 0, -6)
$searchquery = mysql_query($query,$db);
__________________
Nihilism is the organic, reasoned response to artificial chaos, the intentional chaos manufactured by government, religion and mass-media.
Outpour is offline  
Old 09-12-2004, 03:34 PM   #7 (permalink)
Upright
 
Quote:
Originally Posted by ratbastid
--snip--
Then run a post-query ranking process where you test each record returned from your query for the presence of each of your search terms, and scores them. Then sort your responses by score, and bob's yer uncle.

Like this:

--snip--

Note that I didn't actually run this code--this is strictly off-the-top-of-my-head. YMMV greatly.
I am not sure exactly how to implement your example.. what is the purpose of returning 1, 0 or -1 on the sorting function.

Here is your code with some modifications for my situation:

Code:
$search = explode(" ",$_GET["q"]);
$query = 'SELECT * FROM posts WHERE 0=0 OR ';
foreach ($search as $term) {
	$query .= "'Body' LIKE '%$term%' OR 'intro' LIKE '%$term%' OR ";
} 
$query = substr($query, 0, -3);
$searchquery = mysql_query($query,$db);
while ($results = mysql_fetch_array($searchquery)) {
	foreach ($results as $i => $record) {
			 $result[$i++][record] = $record;
			   foreach ($search as $word) {
				  if (ereg($word, $record)) {
					  ++$result[$i][score];
				  }
			}
		}		
}

//The user_sort function here

$sorted = usort($result, 'user_sort');
print_r($sorted);
How do I go about outputting results in a sorted manner?
__________________
Nihilism is the organic, reasoned response to artificial chaos, the intentional chaos manufactured by government, religion and mass-media.
Outpour is offline  
 

Tags
handling, php or mysqlsearch, queries


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:56 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73