views:

119

answers:

1

I have simple tbl_posts table with three columns (id, name, content)

I created fulltext index for name and content columns. When I query it like:

SELECT  *
FROM    dbo.tbl_posts
WHERE   FREETEXT ( *, 'search word' )

I want that the order for results will be ordered first by rank in column name and then my content

A: 
    CREATE FUNCTION PostFreeTextSearch
    (
     @SearchTerms nvarchar(100)
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
    SELECT
    CASE WHEN fttName.[Key] IS NULL THEN fttContent.[Key] ELSE fttName.[Key] END as id,
    fttName.[RANK] as NameScore,
    fttContent.[RANK] as ContentScore

    FROM 
    FREETEXTTABLE(tbl_Posts, (Name), @SearchTerms) fttName
    FULL OUTER JOIN
    FREETEXTTABLE(tbl_Posts, (Content), @SearchTerms) fttContent ON fttName.[Key] = fttContent.[Key]

    WHERE fttName.RANK > 0 OR fttContent.RANK > 0
    )
    GO
    SELECT * FROM PostFreeTextSearch('sql server')
SteadyEddi