SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


getting distinct result set from unioned fulltext search


getting distinct result set from unioned fulltext search

Author
Message
jsesch
jsesch
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 32
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.
patrickmcginnis59 10839
patrickmcginnis59 10839
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1770 Visits: 5520
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"

to properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search