2012-06-03 54 views
1

我正在尝试确定所有可能的单词旁边有一个全文索引的列中的单词。如何获取单词旁边的单词

我使用sys.dm_fts_index_keywords来获取所有可能的关键字,忽略所有在停止列表中的单词。

我试图弄清楚的一个例子: 如果我有字符串:“我喜欢我的办公室的咖啡厅”,我正在看“咖啡馆”一词,我想知道下一个字它是。

当我在下一步搜索时,是否包含停止列表单词并不重要。我只是认为利用已经存在的全文索引会更容易。

回答

2

您可以使用FT索引查找匹配,但是您必须解析短语并找到相邻的单词。不幸的是,FTS不会给你比赛afaik的位置,否则这可能会更容易。

下面是一个例子设置:

declare @find varchar(100) = 'cafe'; 

declare @phrase varchar(100) = 'I like the cafe at my office'; 

;with 
x(x) 
as ( select cast('<w>'+replace(@phrase, ' ', '</w><w>')+'</w>' as xml) 
    ), 
words(pos, word)  
as ( select dense_rank() over (order by n), 
       p.n.value('.', 'varchar(100)') 
     from x 
     cross 
     apply x.nodes('/w')p(n) 
    ) 
select d.* 
from words w 
cross 
apply ( select * 
      from words 
      where pos in (w.pos-1, w.pos+1) 
     )d(pos, word) 
where [email protected]