Searching and ordering results by relevance
Hi all,
I am building a search function and would like some advice.
I am planning to allow people to search in a single field and am looping through each word in their search terms and returning a struct of IDs, then selecting the records relating to those ID's to output.
If the user searches for John Smith London, I want to search Forename, Surname and City. This should return any instances of John, Smith and London from the db. If the db contains a John Smith from London, he will be returned 3 times. A Dave Jones from London will be returned once and so on.
I have it working up until this point. What I want to be able to do next is to group the results and count the number of instances of each result, and order by count DESC, to list the record with the most results from the search term at the top of the results.
My code is below. Any advice greatly appreciated.
Thanks,
Paul
<cfset results = structNew()> <cfset x = 0> <cfloop list="#FORM.q#" delimiters=" " index="q"> <cfset x = x+1> <cfquery name="searchResults" datasource="#REQUEST.ds#"> SELECT userId FROM users WHERE surname LIKE <cfqueryparam value="%#q#%"> OR forename LIKE <cfqueryparam value="%#q#%"> OR town LIKE <cfqueryparam value="%#q#%"> </cfquery> <cfoutput> <p>searchResults.regId = #searchResults.regId#</p> </cfoutput> <cfset structInsert(results, x, searchResults.regId)> </cfloop> <cfdump var="#results#">
