Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

getting distinct result set from unioned fulltext search Expand / Collapse
Author
Message
Posted Thursday, March 14, 2013 11:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 5:02 PM
Points: 17, Visits: 27
I am using fulltext search to get a search result but if I use containstable only, the result must have all the search terms. If I use freetexttable only, the result does not include predicts. So I am unioning the two results together and then ordering them by rank. My problem is that the union will not eliminate the duplicates because the rank value for each search is different.

Here is psuedo code of what I am currently doing.

@contains_srch_param = N'"Rand*" AND "Doe*"',
@freetext_srch_param = N'Rand Doe'

Select entityId,
name,
title,
address,
rank
From (
Select
Top(50)
e.entityId
e.name,
e.title,
e.address,
keyTable.Rank
From
entity e
inner join ContainsTable(srch_param, param_val, @contains_srch_param) as keyTable on e.entityId = keyTable.key
Order by keyTable.Rank desc
Union
Select
Top(50)
e.entityId
e.name,
e.title,
e.address,
keyTable.Rank
From
entity e
inner join FreeTextTable(srch_param, param_val, @freetext_srch_param) as keyTable on e.entityId = keyTable.key
Order by keyTable.Rank desc
) result
Order by rank desc, name;

Results in
1 Randy Doe, director, 123 main st, 262
2 Jane Doe, employee, 321 other st, 260
1 Randy Doe, director, 123 main st, 160

The first and third result are actually the same person, so I need to remove the one with the lower rank. How can I do this or can someone offer a better option of searching.
Post #1431156
Posted Friday, April 12, 2013 12:15 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 8:59 AM
Points: 389, Visits: 2,311
Pseudocode of my guess!

Select e.entityId,
a.name,
a.title,
a.address,
e.rank
From (
Select entityId, max(Rank) Rank
from (
Select
Top(50)
e.entityId
keyTable.Rank
From
entity e
inner join ContainsTable(srch_param, param_val, @contains_srch_param) as keyTable on e.entityId = keyTable.key
-- Order by keyTable.Rank desc (probably not needed)
Union
Select
Top(50)
e.entityId
keyTable.Rank
From
entity e
inner join FreeTextTable(srch_param, param_val, @freetext_srch_param) as keyTable on e.entityId = keyTable.key
-- Order by keyTable.Rank desc (probably not needed)
) ranking_subquery
Group by entityId
) e
join
entity a
on
e.entityId = a.entityId
Order by rank desc, name;


edit: Forgot a "group by"
Post #1441834
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse