views:

43

answers:

2

Hi: Suppose I have a MySQL database named DB, and a table named ContactInfo. The following is its structure and some example data(Just one item):

Table----ContactInfo:


-----------------------------------------------------------
name  fullname                                       phone
-----------------------------------------------------------
NASA  National Aeronautics and Space Administration  00000
-----------------------------------------------------------

Now I want to search the data under the table "ContactInfo" using the following manner:

1) No matter I enter "NASA" or "National Aeronautics and Space Administration" or the phone number, the right line(in this exmaple, just the first line) should be returned to me.

2) When I enter "National Aeronautics Administration" or "National Space Administration" or "American Aeronautics and Space Administration" or "American NASA" which does not have a exact match in the database,but the first line should be returned to me since the content in the first line are related to the requested string.

The manner two can be seen as the obscure search.

I have considered to use the "Full context search" such as the "Hpyer estraier" and "Lucene". However I found that my requirement is different more or less.

When using the "Hyper estraier" or "Lucene", the indexes shoule be built first according to the parsed plain text. Then start to search.

So does that mean I should change the data in my databse to multiple plain text files(each record in the table to a single file), and then build the index according to these files?

BTW, I just found that the MySQL support full text search, however we have to handle some Chinese characters, but it seems that mysql does support chinese characters full text search perfectly.

So, anyone can give me a suggestion?

A: 

You could use something like this for that purpose:

SELECT *
  FROM `ContactInfo`
 WHERE `name` LIKE '%your-query%'
    OR `fullname` LIKE '%your-query%'
    OR `phone` LIKE '%your-query%'

In order to support your case #2, you need to replace all spaces in your-query with %-characters to allow any combination of characters between the entered words. Remember to escape the users input to prevent SQL injection.

And putting all your data into (separate) text files appears to be really ineficcient for storing data.

elusive
For case#2, your suggestion work for English character,but it seems not for Chinese characters. Since English words start and end with a space, but the Chinese word does not.
hguser
You might be able to build this using regular expressions with the word boundary shortcut (`\b`), as documented [here](http://unicode.org/reports/tr18/#Tailored_Word_Boundaries).
elusive
Thanks, I will have a try. I wonder which efficiency maybe better compared with the full text serach?
hguser
A: 

You can use sphinx in conjunction with mysql. sphinx is a standalone tool but it has a mysql proxy mode where it basically behaves like a mysql server and can be queried with any mysql connector out there. Or, as an alternative, there is a sphinx storage engine plugin for mysql which allows you to query sphinx with your current mysql connection and perform join's to other tables.

Definetly not a quich and dirty solution but it's worth a further look: http://sphinxsearch.com/

SchlaWiener
It seems that sphinx support php only,but our app is built under java.And I find another sphinx-4, what is the different?
hguser
Java does not need build in sphinx support. You can use SphinxSE `SE = Storage Engine` http://sphinxsearch.com/docs/current.html#sphinxse-using or SphinxQL http://sphinxsearch.com/docs/current.html#sphinxql which is a daemon that fakes a real MySQL server and let's you perform queries. Both methods require that the sphinx daemon in up and running. SphinxQL is easier to set up because SphinxSE requires you to recompile MySQL with sphinx support but SphinxSE is more convinient to query since you can query the product_id's from a sphinx search and directly join the product table in one step.
SchlaWiener