Solved

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

Posted on 2016-09-13
17
47 Views
Last Modified: 2016-09-15
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
0
Comment
Question by:shah36
  • 10
  • 7
17 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 41796114
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
 

Author Comment

by:shah36
ID: 41796135
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
 
LVL 40

Expert Comment

by:lcohan
ID: 41796150
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:shah36
ID: 41796171
Thanks again but i am already using Freetexttable. containable also doesn not return the result.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 41796186
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
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 41796222
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
 

Author Comment

by:shah36
ID: 41796225
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
 

Author Comment

by:shah36
ID: 41796228
Actually i already tried directly without double quotes before posting and it did not work. Same result.
Sorry i am being pain
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 500 total points
ID: 41796250
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
 

Author Comment

by:shah36
ID: 41796255
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
 

Author Comment

by:shah36
ID: 41796324
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
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 500 total points
ID: 41796518
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
 

Author Comment

by:shah36
ID: 41798077
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
 

Author Comment

by:shah36
ID: 41798157
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
 

Author Closing Comment

by:shah36
ID: 41798229
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
 
LVL 40

Expert Comment

by:lcohan
ID: 41798426
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
 

Author Comment

by:shah36
ID: 41799467
Thanks again for your help and support. Its been really a great learning curve for me on the Full Text Search
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS GUID Variable 2 31
SQL Syntax 6 37
Related to SQL Query 5 19
SQL Server Compression Decision 5 39
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question