0

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

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

提前致谢!

4

1 回答 1

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')
于 2012-12-04T21:40:39.683 回答