SQL Server 全文搜索使用 CONTAINS、FORMSOF、NEAR 进行多个搜索词

发布于 2024-10-12 14:45:17 字数 678 浏览 1 评论 0原文

我是 SQL Server 全文搜索的新手,并且正在尝试找出使用屈折引擎搜索多个单词的最佳方法,以便搜索使用所有单词的各种形式。

据我所知,FREETEXT 在与多个单词一起使用时使用隐式 OR。我想要一个 AND 以便搜索结果包含所有单词,因此我选择使用 CONTAINS。

我正在尝试执行类似于下面的查询的操作,该查询使用 FORMSOF 和邻近关键字 NEAR 来表示多个单词。请注意,这不是有效的语法并返回错误:

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model NEAR airplane)')

但是,下面的查询有效,但我不知道它是否给出了预期的结果。 SQL 全文搜索的“AND”和“NEAR”有区别吗?

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model) AND FORMSOF(INFLECTIONAL, airplane)')

我想我要问的是,有没有办法将 CONTAINS、FORMSOF 和 NEAR 与多个搜索词一起使用?或者我应该只使用上面使用“AND”的第二个查询?

I am new to SQL Server Full Text Searching, and am trying to figure out the best way to search on multiple words using the inflectional engine so the search uses the various forms of all of the words.

From what I read, FREETEXT uses an implicit OR when used with multiple words. I want an AND so that the search results contain all of the words, so because of this I am choosing to use CONTAINS.

I am trying to do something like the query below, which uses FORMSOF with the proximity keyword NEAR for multiple words. Note that this is not valid syntax and returns an error:

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model NEAR airplane)')

However, the query below works, but I don't know if it gives the intended results. Is there a difference between "AND" and "NEAR" with SQL Full Text Search?

select top 5 *
from content
WHERE CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, model) AND FORMSOF(INFLECTIONAL, airplane)')

I guess what I am asking is, is there a way to use CONTAINS, FORMSOF, and NEAR with multiple search words? Or should I just use the second query above that uses "AND"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2024-10-19 14:45:17

从文档中:

<proximity_term> ::= 
     { <simple_term> | <prefix_term> } 
     { { NEAR | ~ }
     { <simple_term> | <prefix_term> } 
     } [ ...n ] 

这意味着您可以对(可能有前缀的)单词、短语及其组合使用 NEAR 谓词。

由于您的搜索词使用非常简单的规则进行变形,因此您可以仅使用前缀:

SELECT  *
FROM    content
WHERE   CONTAINS((Title,Subtitle,Body), 'model* NEAR airplane*')

或使用 AND 并在客户端进行精细过滤

SELECT  *
FROM    ft
WHERE   CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, "model") AND FORMSOF(INFLECTIONAL, "airplane")')

From the docs:

<proximity_term> ::= 
     { <simple_term> | <prefix_term> } 
     { { NEAR | ~ }
     { <simple_term> | <prefix_term> } 
     } [ ...n ] 

This means you can use NEAR predicate for (possible prefixed) words, phrases and their combinations.

Since your search terms are inflected using quite simple rules, you can just use prefixes:

SELECT  *
FROM    content
WHERE   CONTAINS((Title,Subtitle,Body), 'model* NEAR airplane*')

or use AND and do fine filtering on the client side

SELECT  *
FROM    ft
WHERE   CONTAINS((Title,Subtitle,Body), 'FORMSOF(INFLECTIONAL, "model") AND FORMSOF(INFLECTIONAL, "airplane")')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文