Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FREETEXT search - ordering results according to how close they match

I'm trying to implement a Full Text Search. I'm using FREETEXT and I get the correct results. The problem is the ordering of results. If try to search for "car park" the results that match both those words should be at the beginning and then those matching only one of them. How can I accomplish this?

Thank

like image 980
kubal5003 Avatar asked Jan 20 '23 05:01

kubal5003


1 Answers

Use FREETEXTTABLE instead of FREETEXT.

FREETEXTTABLE will return a table of keys with Rank information. You can sort on this Rank information to find the items that are the closest matches.

Microsoft FREETEXTTABLE documentation

The following example shows how this works:

SELECT
    t.TableID
    , t.TextData
    , ft.Rank
FROM
    Table t
    INNER JOIN FREETEXTTABLE ( Table , * , 'car park' ) ft ON ( t.TableID = ft.[Key] )
ORDER BY
    ft.Rank DESC
like image 154
Aaron Avatar answered Jan 31 '23 00:01

Aaron