sql server全文搜索排名

时间:2012-12-04 15:44:24

标签: sql-server full-text-search

我是全文搜索功能的新手。所以我需要一些帮助。

问题是是否可以在ms sql server 2008 r2的全文搜索中设置自定义排名? 我在自定义排名下的理解: 即我们在db中存储书籍。如果在Book1的书名中满足搜索词,并且在Book2的描述中,名称优先于描述,则Book1应显示为第一个记录。

提前致谢!

1 个答案:

答案 0 :(得分:1)

是的,可以自行滚动,但不能保证大规模的性能,因此请使用实际数据进行测试。我们的想法是单独查询字段,添加您自己的自定义排名,并合并结果。

 create table DoNotBlindlyRunMe.books 
 (
 id int not null identity primary key,
 Title varchar(100) not null,
 Author varchar(100) not null,
 [Description] varchar(500) not null
 )
 go
 CREATE FULLTEXT CATALOG [ft_books] WITH ACCENT_SENSITIVITY = ON
 GO
 --You need to edit this to match your PK index name!
 CREATE FULLTEXT INDEX ON [dbo].[books] KEY INDEX [PK__books__3213E83F3D5E1FD2] ON ([ft_books]) WITH (CHANGE_TRACKING AUTO)
 GO
 ALTER FULLTEXT INDEX ON [dbo].[books] ADD ([Author] LANGUAGE [English])
 GO
 ALTER FULLTEXT INDEX ON [dbo].[books] ADD ([Description] LANGUAGE [English])
 GO
 ALTER FULLTEXT INDEX ON [dbo].[books] ADD ([Title] LANGUAGE [English])
 GO
 ALTER FULLTEXT INDEX ON [dbo].[books] ENABLE
 GO

 insert into books (title, author, description)
 values ('The Cat in the Hat',     'Dr. Suess',     'An epic tale of a cat who teaches some valuable lessons.'),
 ('The Little Engine that Could',     'Watty Piper','A small train goes over a mountain.'),
 ('Favorite Nursery Rhymes by Mother Goose', 'Cat Stevens',     'Fairy tales from mother good, including peter piper.')

 --Rank Title highest, then Author, then description
 select [key], min([RANK]) [RANK] from (
      select [key], 100000000+ [RANK] RANK from containstable([books], [Title] , 'cat') union all
      select [key], 200000000+ [RANK] from containstable([books], [Author] , 'cat') union all
      select [key], 300000000+ [RANK] from containstable([books], [Description] , 'cat')
      ) ranked_containstable  GROUP by [key] order by MIN( [RANK] )

 --Or as a TVF
 go
 CREATE FUNCTION  DoNotBlindlyRunMe.RankedBookSearch (@ftQuery nvarchar(500))
 returns table return
 (
 select [key], min([RANK]) [RANK] from (
      select [key], 100000000+ [RANK] RANK from containstable([books], [Title] , @ftQuery) union all
      select [key], 200000000+ [RANK] from containstable([books], [Author] , @ftQuery) union all
      select [key], 300000000+ [RANK] from containstable([books], [Description] , @ftQuery)
      ) ranked_containstable  GROUP by [key]
 )
 go

 select * from dbo.RankedBookSearch(N'cat or stevens or piper')