0

I have implemented full-Text search in my application for searching a keyword from a table in my database.Now i want to extend the search for multiple words.I googled for it but didnt get the answer i wanted.Can anyone tell how to extend the search for multiple keywords.

Query:

select ProductCode,CategorySequenceNumber from tblProductMaster where Contains(*,@stringToSearch)

Any suggessions will be appriciated..

coder
  • 1,980
  • 3
  • 21
  • 32
  • I don't understand you question. Where do you need help ? On making a field in a form to enter multiple keywords ? – Luke Marlin Apr 08 '13 at 08:08
  • @LukeMarlin i want the query to implement it. – coder Apr 08 '13 at 08:08
  • 1
    Duplicate of http://stackoverflow.com/questions/3441775/use-multiple-words-in-fulltext-search-input-string – Seph Apr 08 '13 at 08:26
  • @LukeMarlin your answer is k.but tell how can i modify the above query .. – coder Apr 08 '13 at 09:15
  • You don't have to modify it. You simply need to change the parameter you put in your query. – Luke Marlin Apr 08 '13 at 09:16
  • I tried to modify as this select ProductCode,CategorySequenceNumber from tblProductMaster where Contains(*, '@stringToSearch OR @stringToSearch1' ) but it gives me an error "Incorrect syntax near keyword OR" – coder Apr 08 '13 at 09:20
  • As I said, you don't need to modify your query. You just need to modify the `@stringToSearch` parameter. So `@stringToSearch` must contain an aggregate of your words – Luke Marlin Apr 08 '13 at 09:36

3 Answers3

2

Pass in your keywords separated by "AND" or "OR" depending on the behavior you want :

SET @stringtosearch = '"this" AND "word" OR "test"'
Luke Marlin
  • 1,398
  • 9
  • 26
  • This is the correct answer. Also don't forget to watch out for stop words such as someone looking for "this is a test" if you just `Replace " " with " AND "` you're going to have `"this AND is AND a AND test"` which will return 0 results due to `stop words`. To solve that there's plenty of answers on SO and in search engines. – Seph Apr 08 '13 at 08:25
  • @Seph I know Luke's answer is correct.But i need to modify the above query(given in question) so that if i enter multiple woprds in a textbox it ca search and give me some results.And my question is in sqlserver and not in tsql. – coder Apr 08 '13 at 09:34
  • Wether it is simple SQL or TSQL, you just have to change the parameter by giving it your words, separated by a boolean operator. – Luke Marlin Apr 08 '13 at 09:39
0

You can split the search text at spaces to a string array and then search for each of the elements.

jalgames
  • 781
  • 4
  • 23
0

Use FREETEXT function intead of CONTAINS. seperate your keywords with spaces.

jaczjill
  • 334
  • 9
  • 25