SQL Server 2014 Full Text Search Returns more results with narrow search

Hi Guys,

I need help with the full text search. I want the full text to bring the data where all of the words are found in the table ideally more words should narrow the search result. however it is opposite. The more words i put in my search string it brings more records. So effectively it looks for the words in the column and if it finds any match it brings the record. Can you please help?

Here is my code
DECLARE @GlobalStringFraction NVARCHAR(100)  = 'BIRKBECK HOUSE COLLIERS WAY NG86AT'
ensures that full text index is up to date before running the following
EXEC [Core].[USP_Core_WaitForFullTextIndexing] N'LocationFullTextCatalog';

SET @GlobalStringFraction = REPLACE(REPLACE(REPLACE(@GlobalStringFraction,
                                                    N' ', N'<>'), N'><', N''),
                                    N'<>', N' ');
SET @GlobalStringFraction = N'"' + REPLACE(@GlobalStringFraction, N' ',
                                           N'" AND "') + N'"';

PRINT @GlobalStringFraction;


SELECT  LocationId ,
        Rank ,
        IsPoint
FROM    [Spaces].[CrossContext].[LocationView] AS t
        INNER JOIN FREETEXTTABLE([Spaces].[CrossContext].[LocationView], ConcatenatedAddressString, @GlobalStringFraction)
        AS k ON t.LocationId = k.[key]
                AND Iso3166MinusOneCountryId = 1
ORDER BY t.LocationId;

Open in new window


I have attached the sample data with the question.

regards
SampleData.xlsx
Ali ShahSQL DeveloperAsked:
Who is Participating?
 
lcohanDatabase AnalystCommented:
Can you try the actual two statements below:


SELECT  LocationId ,
        Rank ,
        IsPoint
FROM    [Spaces].[CrossContext].[LocationView] AS t
        INNER JOIN FREETEXTTABLE([Spaces].[CrossContext].[LocationView], ConcatenatedAddressString, 'BIRKBECK AND HOUSE AND COLLIERS AND WAY AND NG86AT')
        AS k ON t.LocationId = k.[key]
                AND Iso3166MinusOneCountryId = 1
ORDER BY t.LocationId;



SELECT d.LocationId ,
        f.[Rank] ,
        d.IsPoint
      FROM CONTAINSTABLE( [Spaces].[CrossContext].[LocationView] , ConcatenatedAddressString, N'BIRKBECK" AND HOUSE AND COLLIERS AND WAY AND NG86AT') f
      INNER JOIN [Spaces].[CrossContext].[LocationView] d
            ON f.[key]=d.LocationId
                AND d.Iso3166MinusOneCountryId = 1
--ORDER BY f.RANK
ORDER BY t.LocationId;


Maybe is just the double quotes buggering your query returned result set.
0
 
lcohanDatabase AnalystCommented:
I think it depends how the CONTAINS/FREETEXT search SQL code is written or in other words if OR is used instead of AND so the result set widens not narrows the search in that case. Also RANK can be used to add/remove rows to the returned record set and for more details please see:
https://msdn.microsoft.com/en-us/library/ms142583(v=sql.120).aspx
0
 
Ali ShahSQL DeveloperAuthor Commented:
Thanks for your reply. Well the string @GlobalStringFraction after formatting gives the following value
"BIRKBECK" AND "HOUSE" AND "COLLIERS" AND "WAY" AND "NG86AT"

Open in new window

So its definitely an AND.
Also i need the Rank column and if use the Contains function i can't have a Rank column. I already did a bit of reading on the link you sent but it did not help.

regards,
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
lcohanDatabase AnalystCommented:
I think you can actually have a RANK in contains and the difference between the two may be the answer you're looking for because:

CONTAINSTABLE - http://msdn.microsoft.com/en-us/library/ms189760(v=SQL.90).aspx 
<<Returns a table of zero, one, or more rows for those columns containing character-based data types for precise or fuzzy (less precise) matches to single words and phrases, the proximity of words within a certain distance of one another, or weighted matches>> - or if my understanding is correct that means more precise search in other words

VS.

FREETEXTTABLE - http://msdn.microsoft.com/en-us/library/ms177652(v=SQL.90).aspx 
<<Returns a table of zero, one, or more rows for those columns containing character-based data types for values that match the meaning, but not the exact wording, of the text in the specified freetext_string. >> - or less "accurate" search meaning more returns in the result set.

Please see more details and better explanation about the differences here:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/9e1095e5-2db1-484c-a23c-04c70872087a/difference-between-freetexttable-and-containstable?forum=transactsql


Limit/use CONTAINSTABLE with RANKing here:

https://msdn.microsoft.com/en-us/library/cc879245.aspx
0
 
Ali ShahSQL DeveloperAuthor Commented:
Thanks again but i am already using Freetexttable. containable also doesn not return the result.
0
 
lcohanDatabase AnalystCommented:
Well I suggested the oposite - use CONTAINSTABLE not FREETEXTTABLE.
What do you mean by "containable also doesn not return the result."?
Does not return any results OR it returns exactly the same number as FREETEXTTABLE(which is very unlikely)?
0
 
Ali ShahSQL DeveloperAuthor Commented:
Here is my code with Containstable and it does not return anything
DECLARE @GlobalStringFraction NVARCHAR(100)  = 'BIRKBECK HOUSE COLLIERS WAY NG86AT' ,
    @Wgs84Latitude FLOAT = 53.93022 ,
    @Wgs84Longitude FLOAT = -2.0324707;

DECLARE @OriginPoint GEOGRAPHY;

SET @OriginPoint = Geography::STGeomFromText(N'POINT('
                                             + CAST(@Wgs84Longitude AS NVARCHAR(MAX))
                                             + N' '
                                             + CAST(@Wgs84Latitude AS NVARCHAR(MAX))
                                             + N')', 4326);

-- ensures that full text index is up to date before running the following
EXEC [Core].[USP_Core_WaitForFullTextIndexing] N'LocationFullTextCatalog';

SET @GlobalStringFraction = REPLACE(REPLACE(REPLACE(@GlobalStringFraction,
                                                    N' ', N'<>'), N'><', N''),
                                    N'<>', N' ');
									PRINT @GlobalStringFraction
SET @GlobalStringFraction = N'"' + REPLACE(@GlobalStringFraction, N' ',
                                           N'" AND "') + N'"';

PRINT @GlobalStringFraction;


SELECT  LocationId ,
        Rank ,
        IsPoint
FROM    [Spaces].[CrossContext].[LocationView] AS t
        INNER JOIN CONTAINSTABLE([Spaces].[CrossContext].[LocationView], ConcatenatedAddressString, @GlobalStringFraction)
        AS k ON t.LocationId = k.[key]
                AND Iso3166MinusOneCountryId = 1
ORDER BY t.LocationId; 

Open in new window

0
 
Ali ShahSQL DeveloperAuthor Commented:
Actually i already tried directly without double quotes before posting and it did not work. Same result.
Sorry i am being pain
0
 
lcohanDatabase AnalystCommented:
No way...that's why were here to help(and learn at the same time).
Also, did you run my queries? not dynamic sql just the ones I posted?


Can you please run statement below and post back results?

select @@version;
0
 
Ali ShahSQL DeveloperAuthor Commented:
I really appreciate your time and effort.
Here is the result of @@version
Microsoft SQL Server 2014 - 12.0.4213.0 (X64) 
	Jun  9 2015 12:06:16 
	Copyright (c) Microsoft Corporation
	Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

Open in new window

0
 
Ali ShahSQL DeveloperAuthor Commented:
Sorry but did you pick my answer that i did try your queries and they produce the same results with FreeTextTable all results and with Containstable no result at all
0
 
lcohanDatabase AnalystCommented:
Yes I got that and thinking about what may have cause the CONTAINSTABLE to not return anything. Interesting that the version you are running 12.0.4213.0 is not listed under "known" SQL 2014 build versions...

https://support.microsoft.com/en-ca/kb/2936603

found your version here... http://www.sqlservercentral.com/articles/SQL+Server+2014/101274/ related to fix " Partial results in a query of a clustered columnstore index in SQL Server 2014" https://support.microsoft.com/en-us/kb/3067257
0
 
Ali ShahSQL DeveloperAuthor Commented:
Hi lcohan,

Thanks a lot. we have now upgraded to the SP2 and CU and now the version of the server is
Microsoft SQL Server 2014 (SP2-CU1) (KB3178925) - 12.0.5511.0 (X64) 
	Aug 19 2016 14:32:30 
	Copyright (c) Microsoft Corporation
	Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

Open in new window


However still CONTAINSTABLE is not returning anything.

regards,
0
 
Ali ShahSQL DeveloperAuthor Commented:
Just found out another weird thing. I have also tested with the normal where clause
WHERE  ConcatenatedAddressString LIKE '%BIRKBECK%' AND  ConcatenatedAddressString LIKE '%HOUSE%' AND  ConcatenatedAddressString LIKE '%COLLIERS%'
  AND  ConcatenatedAddressString LIKE '%WAY%' AND  ConcatenatedAddressString LIKE '%NG86AT%'

Open in new window



It comes with the result as expected. However in my CONTAINSTABLE i have tested by putting direct string and until three words it brings back the results same as where clause
CONTAINSTABLE([Spaces].[CrossContext].[LocationView], ConcatenatedAddressString, '"BIRKBECK" AND "HOUSE" AND "COLLIERS"')

Open in new window

However as soon as i exceed the three words and put more to match it with where clause it does not return anything.

INNER JOIN CONTAINSTABLE([Spaces].[CrossContext].[LocationView], ConcatenatedAddressString, '"BIRKBECK" AND "HOUSE" AND "COLLIERS" AND "Way"')

Open in new window


Does CONTAINTABLE has the limit to look for max of three words?
0
 
Ali ShahSQL DeveloperAuthor Commented:
Well, Well, Well for some reasons the word "WAY" causes all the issues, I have removed the word "WAY" from the search string and it works.
Thanks a lot for all of your time and effort

regards,

Ali
0
 
lcohanDatabase AnalystCommented:
OMG...I am sooo bad...how come I didn't notice it and think to check that...I had one theought about noise words but that slipped away unfortunately and please see here the list with all of them and more about noise/stopwords as well. You could find/edit that list BTW and add/remove any words you want to search or not - I mena if for instance you remove "WAY" from the noise word list the search should work on it however....that may cause just too much "noise"

http://mssqltipsandtricks.blogspot.ca/2012/07/noisestop-words-in-sql-server.html

https://msdn.microsoft.com/en-us/library/ms142551.aspx
0
 
Ali ShahSQL DeveloperAuthor Commented:
Thanks again for your help and support. Its been really a great learning curve for me on the Full Text Search
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.