thomas
thomas

Reputation: 2642

SQL Server Contains Full Text Function Not Returning Expected Results

I can't quite figure out why I am getting unexpected results from the following query/statement. I have included code to replicate the issue (which may not be an issue at all but more a misunderstanding on my part about how contains works).

create table dbo.temp (id int identity, description nvarchar(max))
insert dbo.temp values ('this is a website.') --this record will be returned in the select query
insert dbo.temp values ('a website exists.') --this record will be returned in the select 
insert dbo.temp values ('go to mywebsite.net') --this record will NOT be returned in the select 
insert dbo.temp values ('go to mywebsite.net.') --this record will NOT be returned in the select 


create fulltext catalog temp
create unique index idx_dbo_temp_1 on dbo.temp (id)
create fulltext index on dbo.temp(description)
    key index idx_dbo_temp_1 on temp
    with change_tracking auto


declare @search_client nvarchar(100) = 'website'

select
    *
from
    dbo.temp
where
    contains ((description),@search_client)


drop fulltext index on dbo.temp
drop index idx_dbo_temp_1 on dbo.temp
drop fulltext catalog temp
drop table dbo.temp

The query will return records that have website in the description field but will not return the record which has mywebsite.net in the description field.

Any thoughts?

UPDATE: the @search_client variable will really be a parameter passed in via SSRS so declared the variable to simulate the parameter being passed in.

Upvotes: 2

Views: 5065

Answers (3)

Luka Milani
Luka Milani

Reputation: 1541

In Fulltext, all nonalphanumeric characters are removed in the indexes and replaced by blanks. So in your search, since you had "." in the string, you are searching for "website" and "net".

You can fix this in 2 ways.

You need to have a separate table or separate field that has the fulltext data, separated from the original table were you keep the original data.

In the fulltext table you can remove the "." and store "websitenet".

In that case, you need to remove all "." from the search string before you do the query. If you want to query with ".", you need to replace "." with a character string - for example "dot".

So in this case you would store "websitedotnet".

When you search this time, you replace all "." with "dot" in the query.

OK now your case with a new field where store the column to be search by FTS so:

    ID      DESCRIPTION               DESCFTS
    -----------------------------------------------------
    1   this is a website.        this is a websitedot
    2   a website exists.         a website existsdot
    3   go to mywebsite.net       go to mywebsitedotnet
    4   go to mywebsite.net.      go to mywebsitedotnetdot

then your query:

declare @search_client nvarchar(100) = 'website'

set @search_client = replace(@search_client, '.', 'dot')

select * from dbo.temp where contains ((DESCFTS), @search_client)

Upvotes: 2

user1166147
user1166147

Reputation: 1610

I think the problem you are having is that, unforutnately, it is not possible to search with a leading wildcard in FTS. If you are searching with the CONTAINS phrase, you cannot use leading, only the trailing functionally. Look for a workaround to leading wildcard searches. IT IS THE MY that is creating the problem in the mywebsite.net not showing. This in combination with the DOT issue already mentioned. There have been some workarounds but they seem hacky to me - and also research FREETEXT for performance gain possibility.

Upvotes: 0

Shaikh Farooque
Shaikh Farooque

Reputation: 2640

Try the following:

Updated:

select
    *
from
    dbo.temp
where
    contains ((description),'"website*"')

Upvotes: 1

Related Questions