PDA

View Full Version : Search feature for essay site (and advice from Chris)...



LittleGuy
04-08-2004, 10:58 PM
I'm having a little problem...

I have a couple of thousand essays in a mysql database. How can I allow users to efficiently search for them? Chris, would you mind sharing any advice on how you did it on your literature site?

PS. I want users to be able to search by the content of the essay (not just the name of the essay) -- i.e. search within the essay.

Thanks in advance.

-The Little Guy

Chris
04-09-2004, 05:41 AM
The simplest way to do this is a MySQL query like this

select * from essays where essay_content like '%$searchterm%'

% is the SQL wildcard.

This will return essays where the exact (and I do mean exact) search phrase is found.

For more complex searches you can use a FULLTEXT index. That is what forums like this one use. I don't use a FULLTEXT index on my literature site as people are often looking for exact phrases. On an essay site where people are looking for general topics, I would use one.

http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html

Chris
04-09-2004, 05:44 AM
http://www.zend.com/zend/tut/tutorial-ferrara1.php

Another tutorial on fulltext

LittleGuy
04-09-2004, 06:07 AM
Chris,

Do you break up the search term and search for each individual one? ie LIKE '%keyword1%' OR '%keyword2%'?

Thanks for your advice

incka
04-09-2004, 09:20 AM
I think I will use this, Mike will too I'm sure...

chromate
04-09-2004, 09:52 AM
Originally posted by LittleGuy
Do you break up the search term and search for each individual one? ie LIKE '%keyword1%' OR '%keyword2%'?

It depends if you want an exact match for a search term or not. breaking it up will obviously match any word, where as if it's all one string then it will only match if it finds the exact phrase.

rani
10-22-2004, 02:55 AM
hi,
The search like
select * from table name where column like "%search-string%"
will result the one which the word is found anywhere in the string.

But is it possible to check in a string for each word for exact match or even starting with?

I mean
-> " This string should return if I query_for the term querystarting with" ;
select * from tablename where column like "query%";

-> "This string should return when i query for this term when i match for this anywhere";
select * from tablename where column like "%this%";

->"This string should return for exact match for a word";
select * from tablename where column like "exact"

AndyH
10-22-2004, 03:51 AM
I suggest giving an option to search just the essay titles and also the essay bodies. If the site is, or gets very popular, the searches could be very process consuming for the server.

Or you could just make the change when the time comes.

Chris
10-22-2004, 06:47 AM
Yes Rani.

For instance to get everyone who has a name that starts with A

select * from people where name like 'A%'

eMEraLdwPn
10-22-2004, 09:16 AM
i tried adding fulltext like that article said... after about 3 minutes i got this message:

MySQL said:

#3 - Error writing file '/tmp/STPALyTf' (Errcode: 28)

rani
10-26-2004, 11:28 PM
Yes Rani.

For instance to get everyone who has a name that starts with A

select * from people where name like 'A%'

This works fine if the word start with the A.
For eg:
1) Andrew_smith was at the door.
2) I think Andrew_smith is at the door.
3) Andrew is right at the door
if we query like
select * from tablename where column like "Andrew%"; the first and third will return and the second will not.
what one can do for such thing.

regards,
rani

AndyH
10-27-2004, 02:51 AM
SELECT * FROM `names` WHERE `name` LIKE '%A%';

Todd W
11-06-2004, 05:08 PM
Is this


SELECT * FROM `names` WHERE `name` LIKE '%A%';

Just as fast as using a new column for only the first letter of their name (or last name).. you just grab the first char. when you add the names to the database and write it to a different column. Then search with


SELECT * FROM names WHERE name= "A"

Just curious.

AndyH
11-06-2004, 08:35 PM
Well, if you want to make it as fast as possible then make another column that holds a value from 1-26 (TINYINT(2) UNSIGNED) and index it. Then use:


SELECT * FROM names WHERE first_letter= "7" If the first letter is a G.

Todd W
11-07-2004, 10:55 AM
Well, if you want to make it as fast as possible then make another column that holds a value from 1-26 (TINYINT(2) UNSIGNED) and index it. Then use:


SELECT * FROM names WHERE first_letter= "7" If the first letter is a G.

Well thought idea :)