the company decided not to use a ranked result in the end, but i thought i'd have another crack at it to see if i could do it, and i've managed to come up with a reasonable, relatively fast solution (using asp.net)
Code:
SQL =
SELECT DISTINCT(field1), field2 FROM mytable WHERE field1 LIKE %searchterm% or field2 LIKE %searchterm%
then dump the results into a dataset and work through the results like so:
Code:
Public Function SearchRank(ByVal ds As DataSet, ByVal str As String) As DataView
Dim dc As New DataColumn("rank", GetType(Integer))
ds.Tables(0).Columns.Add(dc)
str &= " "
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
Dim search As String = ds.Tables(0).Rows(i)("prod_short")
Dim occur As Integer = 0
occur += 3 * ((Len(search) - Len(LCase(search).Replace(LCase(str), "*"))) / Len(str))
search = ds.Tables(0).Rows(i)("prod_long")
occur += (Len(search) - Len(LCase(search).Replace(LCase(str), "*"))) / Len(str)
ds.Tables(0).Rows(i)("rank") = occur
Next
Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = "rank DESC, prod_id"
Return dv
End Function
it works through each field and works out how many times the searchterm is present and then multipilies the result to give a weighting (ie it get's more points if the searchterm is found in the title) and then orders the results according to the weighting
i've just tried it out on a page that returns over 2,500 records, and it returned the sorted results and output to the page in 4 seconds and returned a paged version of 20 records in 1.1 seconds (this was running from a testing server, not a local machine)
i didn't think that was too bad a result in the end. obviously sql would have been faster and preferable, but consindering the size of the database i'm quite pleased with it
Social Networking Bookmarks