views:

44

answers:

1

Hello,

Say I have a string.

Then I have a number of unique tokens or keywords, potentially a large number in a database.

I want to search and find out which of these database strings are inside the string I provide (and get the IDs of them).

Is there a way of using a query to search the provided string or must it be taken to application space?

Am I right in thinking that this is not a 'full text search'? Would the best method be to insert it into the database to make it a full text search?

+1  A: 

look at the

  • position(token in LongString) function for postgresql
  • instr(oken,LongString) function for mysql.

Each returns the position of the token in the LongString. When that position is > 0 you have a match.

postgresql:

select st.string, tt.token 
  from tokentable as tt 
     , stringtable as st
 where position(tt.token in st.string) >0

mysql:

select st.string, tt.token 
  from tokentable as tt 
     , stringtable as st
 where instrr(tt.token ,st.string) >0

Please be aware that this is going to be slow and ressource hungry. If you need to do that kind of matching often, you might want to redesign your DB.

Why is this marked mysql and postgresql?

lexu
I wanted to know the strategy for both of them. Would it be more efficent to insert the string into the database and then do a full text search? Say you have a string, it contains various cities, you want to find out which cities are in the string.
Improfane
If you preprocess your data (identify all cities and insert accordingly) your insert performance will be lower. If you search the string for the substrings in the DB then your search will be slower. You will have to decide which use-case must be faster.
lexu
Thanks lexu :-)
Improfane