【发布时间】:2021-03-12 05:50:37
【问题描述】:
我想知道如何(以及是否)可以使用多个搜索字符串以有效方式过滤多个字段。通过排序,您可以确保最重要的匹配项位于顶部。
如果我的用户键入“mexico maria futter”,我希望在某处包含所有三个搜索词的行。例如。 (“John Smith”、“Donnerfutter”、“Maria Mexicobay”、“Berlin”)会很受欢迎, 但是(“John Smith”、“Donnerfulter”、“Maria Mexicobay”、“Berlin”)不会。
编辑:如果我得到多个点击,我希望能够按点击的字段对它们进行排序,例如如果我将 CustomerName 指定为最重要的字段:
("Annie Futterson", "Maria's company", "Address field", "Mexico")
("John Smith", "Donnerfutter", "Maria Mexicobay", "Berlin")
因为在 CustomerName 中有匹配项,所以 Annie Futterson 位居榜首。
SELECT * FROM Customers where (CustomerName LIKE '%futter%' OR ContactName LIKE '%futter%' or Address LIKE '%futter%' or City LIKE '%futter%')
AND (CustomerName LIKE '%mexico%' OR ContactName LIKE '%mexico%' or Address LIKE '%mexico%' or City LIKE '%mexico%')
AND (CustomerName LIKE '%maria%' OR ContactName LIKE '%maria%' or Address LIKE '%bianco%' or City LIKE '%maria%');
查询需要在 Microsoft SQL Server 2012 及更高版本上运行。
对于实现此功能的任何帮助将不胜感激,即使我的问题被证明是重复的 :)
编辑:我编辑了查询以生成一个有效的查询并添加了更多信息。我不知道如何按优先级对字段进行排名,具体取决于点击的字段。
FullText search with CONTAINS on multiple columns and predicate - AND 不能解决我的问题,因为它没有按命中列排名。它也没有涉及不同的选项和这些选项的有效性。
【问题讨论】:
-
向我们展示一些示例表数据和预期结果 - 全部为格式化文本(无图像)。minimal reproducible example。
-
查看使用 SQL Server 全文索引的答案:stackoverflow.com/a/20475748/133665 您可以在此处阅读有关该主题的更多信息:docs.microsoft.com/en-us/sql/relational-databases/search/…
标签: sql sql-server filter