Jump to content

Creating a search engine for my website [solved]


Recommended Posts

I'm unsure of how to go about this. Currently using an java-based search, which is OK for a small site, but with mine slowly getting bigger, I want it to be quicker. The java file is getting large, so can take a while just to load the search page when you click it.

 

Therefore, figured I can do it with mysql, only my app has stopped working that I used, which used to generate it all for me.

 

So, I'm trying to see if anyone knows of how I could do this. I don't mind how it would work, whether it's html or php based, providing it can get the data from a mysql database.

 

Also, wondering how I can create the database in the first place. Any ideas appreciated :thanks:

Link to comment
Share on other sites

Just as an addon, I'm thinking of creating my mysql table like this:

 

id = Unique record number

keywords = where each keyword will exist

url = url to the page the keyword is assigned to

text = text from webpage to show in the search

 

I'm not entirely sure if this is the best way to go about it, but I had no other idea of how I can get the url pulled for each page otherwise. My whole site is mostly html, and I know this will have to be php for the search, which is perfectly fine.

 

I think I'm getting closer, but no idea on the php bit, or even if my table will work OK.

Link to comment
Share on other sites

OK, got a basic script, here it is, but what I find is, that when I open the page when a search hasn't been completed, I get all results. If anyone knows how I can stop it displaying the whole database automatically, that would be great.

 

<?php
// set your infomation.
$dbhost='localhost';
$dbusername='searchuser';
$dbuserpass='searchpass';
$dbname = 'buziaks';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');
if(isset($_GET['search']))
{
$search = $_GET['search'];
}

$keywords = explode(" ", $search);

$query = "SELECT keywords,url,text FROM search " .
"WHERE keywords LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." OR keywords LIKE '%".$keywords[$i]."%'";
}

$result = mysql_query($query) or die(mysql_error());
?>
<center>
<form method="GET" action="search.php">
<b>Search:</b> <input type="text" name="search" size="20" />
<input type="submit" value="Search!" />
</form>
<table width="50%" style="border:1px solid #000000;">
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='border-bottom: 1px solid #000000;'>";
echo "<a href=".$row['url'].">".$row['url']."</a>";
echo "<p>".$row['text']."</p>";
echo "</td>";
echo "</tr>";
}
?>
</td>
</tr>
</table>

 

When I do a search normally, it works. It's just when I open the page for the first time, or do a blank search, it displays all results possible.

Link to comment
Share on other sites

The code needs to distinguish between when the form is initially loaded versus the when the form has been submitted. Also, blank search criteria needs to be detected and communicated back to the user rather than processed (since LIKE "%" matches everything, you get all rows returned).

 

You could do something like the following. This uses POST rather than GET but the idea is the same.

 

//if the "submit" variable does not exist, the form has not been submitted - display initial page
if (!isset($_POST['submit'])) {
print '<h3>Search Criteria:</h3><br />
	<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
	Keyword1: <input name="searchkey1" size="25">
	<select name="bOption"><option>AND<option>OR</select> 
	Keyword2: <input name="searchkey2" size="25">
	<input type="submit" name="submit" value="Go">
	</form>';
}
else {
// if the "submit" variable exists, the form has been submitted - look for and process
// form data and display result
$searchfor1 = $_POST['searchkey1'];
$searchfor2 = $_POST['searchkey2'];
$bOption = $_POST['bOption'];
if ($searchfor1 == "" && $searchfor2 != "") {
	$searchfor1 = $searchfor2;
	$searchfor2 = "";
}	
$s = $searchfor1 . (($searchfor2 != "") ? " $bOption $searchfor2" : "");
print "<h3>Search Results</h3>\n";
if ( $s != "" ) {
	 	print "<p>Search Criteria: $s </p>\n";
				   //call a function to process the search criteria
		my_search_function($s);
} else {	
		print "<p>Cannot Proceed - Missing Search Criteria.</p>\n";
}
}

Link to comment
Share on other sites

Or make a text box to get google to search just the pages in your site:

http://www.google.com/searchcode.html

 

Of if you're set on creating, coding and maintaining your own database, have an input page and an output page - so you first go to the input page, type your query, and only then go to the results page where the results are displayed. And then the output page probably has an input box too to try a different search.

Link to comment
Share on other sites

why recreate the wheel?

http://search.mandrivausers.org/

 

Have you got the code for that?

 

I'm mainly interested in doing it, so I understand how to create one myself - you know, the learning process and all :P

 

Didn't really want to use any other search engines features, such as google, as I want it to be my own stuff, and not taking the easy option. Besides, googles feature doesn't open the results in a new window, and therefore ends up displaying a google page rather than my website. I wanted to keep it in my website rather than take people elsewhere.

 

I'll have a go at that tonight jboy, let you know how I get on.

Link to comment
Share on other sites

You could do something like the following. This uses POST rather than GET but the idea is the same.

 

...

 

Can't quite see how I can integrate this with my database to show the results.

Link to comment
Share on other sites

Ah, cool, this looks like the functionality I had in my Windows app that stopped working under wine. I'll have to have a play with this :woot:

 

The one I've created so far works OK, but it's not automatically updateable. So means I'm constantly adding entries to the database for keywords etc, but works a treat, and a lot faster than the java-based one that I had before.

 

Cheers guys! :beer:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...