I am currently trying to build an internal search engine database using asp. I am no expert and have only been briefly introduced to this style of coding. The trouble I have been having is this:
(e.g) I have two documents that are relatively the same and that have the same keywords to describe them. However, the keywords in the table are not written in the same order. All keywords written in the table are separated by spaces.
Item 1 keywords: spare parts engine
Item 2 keywords: spare engine parts
Now the code that I have would allow me to display items 1 and 2 if I was to input only ONE(1) of those key words:
sql = "Select title, descript, url, uid FROM searchMachine WHERE keywords LIKE '%" & Replace(keywords, "'", "''") & "%' ORDER BY uid;"
However, if I was to type in the search box for "spare engine" then only item 2 will show and not item 1. Also, if I was to input "parts engine" or "engine parts" none of them will show. It seems to partake in only the first 2 (or pairs of 2) keywords listed. The goal I would like to accomplish is to allow grouped keywords that the user inputs (i.e. spare engine parts) and allow it to still display all relative info that has any of the keywords listed and not have to worry about writing pairs or groups of keywords over and over.
I know there is a feature out there for full text-search, but I've researched that there have been instances of security breaches having that on, so I would like to avoid risking that. I have a feeling this may relate more to an AND or OR statement, but I'm unsure of the placement if it needs it. Does anyone have any expertise regarding this situation and would be willing to help?
So looking at what HackyStack suggested in the recent post, a counter system is need to be recorded for the breaks in the spaces of words that the user puts in. For each space that the user makes, a "OR keywords LIKE" statement is repeated. It should look something like this:
Dim strSQL, tmpSQL
strSQL = "SELECT title, descript, url, uid FROM searchMachine WHERE "
tmpSQL = "(keywords LIKE "
Dim Pos
Pos = 1
While Pos > 0
Pos = InStr(1, strSearch, " ")
If Pos = 0 Then
'We have it the end
tmpSQL = tmpSQL & "'%" & strSearch & "%')"
Else
tmpSQL = tmpSQL & "'%" & Mid(strSearch, 1, Pos) & "%' " & DefaultBoolean & " keywords LIKE "
strSearch = Mid(strSearch, Pos + 1, Len(strSearch))
End If
End While
So to break down what I have here, we first set up the beginning set of our sql line. From there we need to specify the counter and the conditions it needs to separate multiple keywords. First, set up a marked value for Pos and give it the int value of 1. Next we create a InStr that (sets the starting value of the item being searched, the item that is being searched (in this case strSearch is equal to that of the txtBox name/value) and what we are checking within that section (in this case any spaces in between searched words). If the InStr does not find a space in the search, then it will act as just 1 searched word; otherwise, it will parse the two words and make them separate like the following below.
e.g. user type in searchbar "Spare Engine"
results come back:
keywords LIKE '%spare%' OR keywords LIKE '%engine%'
I'd like to thank my co-worker, HackyStack and 4guysfromrolla.com on help with this. I hope I explained myself ok.