![]() |
![]() |
#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):
So... for the above example the user would get:
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? |
![]() |
![]() |
#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).
|
![]() |
![]() |
#4 (permalink) |
Darth Papa
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'); |
![]() |
![]() |
#5 (permalink) |
Upright
|
So to do the actual query would you do seperate queries for each term?
PHP Code:
__________________
Nihilism is the organic, reasoned response to artificial chaos, the intentional chaos manufactured by government, religion and mass-media. |
![]() |
![]() |
#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. |
![]() |
![]() |
#7 (permalink) | |
Upright
|
Quote:
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);
__________________
Nihilism is the organic, reasoned response to artificial chaos, the intentional chaos manufactured by government, religion and mass-media. |
|
![]() |
Tags |
handling, php or mysqlsearch, queries |
|
|