1

This is the first time I am writing an actual search feature for my database.

The database consists of hotel names, hotel food items, hotel locations.

I would like the above three to show up during a search of a string.

Are there any common search algorithm or packages that can be used ?

EXPECTED RESULT SET:

id | name | description | table_name | rank

56 | KFC| Fried chicken | hotel | 1

12 | [food item name] | [food item description] | food_item | 2

19 | [hotel name] | [hotel description] | hotel | 3

....

1
  • 1
    depends how complex you want to get, and what database you are using. Commented Feb 3, 2012 at 21:23

4 Answers 4

2

Do you mean a relational database? If yes, your "search" algorithm is a WHERE clause.

Do you mean contextual search? Lucene is a great search engine implementation written in Java. This might help you marry it with Lucene:

http://www.cabotsolutions.com/2009/05/using-solr-lucene-for-full-text-search-with-mysql-db/

The answer is far more complicated if you're thinking about crawling web sites based on some criteria. Please clarify.

Sign up to request clarification or add additional context in comments.

4 Comments

I've not seen a better answer today. +1
No I do not mean the "where" clause. I would like my search results to be ranked depending on the search string. Not the simple select * from ...
I have three different tables to search, and I do not want to write three different queries and then compile my search.
@Prakash You should provide an example result-set for your search algorithm if this answer isn't what you were looking for.
1

If you are using Microsoft SQL Server, FreeText works very well:

http://msdn.microsoft.com/en-us/library/ms176078.aspx

2 Comments

thank you, I am looking into how to enable full text search for my mysql database.
The answer mentions "Free Text", which only exists in Microsoft SQL Server. Try the above link for fulltext search with MySQL
1

Let's consider you're using mysql.

Well your question is basically: how to write a query that will search hotel name, food items, and hotel location.

I guess theses 3 informations are stored in 3 different tables. The easiest way would be to simply query the 3 tables one after the other with query like theses:

SELECT * FROM hotel WHERE hotel_name LIKE "%foobar%";
SELECT * FROM hotel_food_item WHERE item_name LIKE "%foobar%";
SELECT * FROM hotel_location WHERE hotel_name LIKE "%foobar%" OR street_name LIKE "%foobar%" OR city LIKE "%foobar%";
  • Make sure your search term are safe from SQL injection
  • You may (or not) want to group the query into 1 bigger query

If your database is becoming large ( like < 100 000 line per table ), or if you have a lot or search query, you might be interested in creating a search index, or use a dedicated database intend for text search, like elastic search or something else.

Edit: If relevance is a matter, use MATCH AGAINST:

You'll have to create 3 subqueries that do MATCH AGAINST, and them compile them together. You can do AGAINST("foobar") as rank so you'll have the score you needed.

This should look like:

SELECT *
FROM
(
SELECT id, 'hotel' as table_name, MATCH (search_field1) AGAINST ("lorem") as rank FROM tableA
UNION 
SELECT id, 'food' as table_name, MATCH (search_field2) AGAINST ("lorem") as rank FROM tableB
) as res

ORDER BY res.rank DESC

4 Comments

:) I know I could do that. But I want to be able to rank my results depending on the search key.
@Prakash Raman Edited answer for detail about relevance search.
Yes, thanks a lot. Looking into that now. Also, I have added more detail to my question, to make it sound a little less stupid !
The hard part of your question is to fill the table_name field of the result you expect. Each subquery (see my edit) should return a static value in his select that is the table name.
1

if you are not using innodb table, and instead are using myisam, you can use mysql's built in full text search.

this works by first putting a full-text index on the columns you wish to search, and then creating a query that looks roughly like this:

SELECT *, MATCH(column_to_search) AGAINST($search_string) AS relevance
FROM your_table
WHERE MATCH(keywords) AGAINST($search_string IN BOOLEAN MODE)
ORDER BY relevance
LIMIT 20

1 Comment

not quite as advanced as the also suggested lucene, but easier to implement, and from what I've experienced, works quite well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.