Solved

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

Posted on 2016-09-13
17
36 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 39

Expert Comment

by:lcohan
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:lcohan
Comment Utility
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
 

Author Comment

by:shah36
Comment Utility
Thanks again but i am already using Freetexttable. containable also doesn not return the result.
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
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 39

Accepted Solution

by:
lcohan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Actually i already tried directly without double quotes before posting and it did not work. Same result.
Sorry i am being pain
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 39

Assisted Solution

by:lcohan
lcohan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:lcohan
Comment Utility
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
Comment Utility
Thanks again for your help and support. Its been really a great learning curve for me on the Full Text Search
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now