使用完整的 Lucene 语法匹配所有文档,但不包括某些术语

Match all documents excluding some terms using full Lucene syntax

我们服务的默认搜索网页使用 * 完整的 Lucene 查询来匹配所有文档。这是在用户提供任何搜索词之前。我们想从搜索结果中排除一些数据(在我们的例子中是测试数据)。

是否可以匹配所有文档但排除所有文档的子集?

例如,假设我们有一个“owners”字段并且我们想要排除所有者为“testA”和“testB”的文档。以下查询似乎不适用于全部匹配方法:

Query:  search=* -owners:testA -owners:testB&queryType=full&$orderby=created desc
Error:  "Failed to parse query string. See https://aka.ms/azure-search-full-query for supported syntax."

当搜索 * 以外的任何内容时,此方法效果很好。例如:

Query:  search=foo -owners:testA -owners:testB&queryType=full&$orderby=created desc
Result: (many documents matched)

我考虑过 $filter 并使用 $filter=filterableOwners/all(p: p ne 'testa' and p ne 'testb') 但这有以下缺点:

理想情况下,这可以仅使用带有 Lucene 查询文本的 search 查询参数来完成。

我找到了解决该问题的方法。如果您的文档中有一个始终有值的字段,您可以使用 .* 正则表达式来匹配该字段中的所有值,从而匹配所有文档。

例如,假设 packageId 字段对所有文档都有一个值。

不正确(如原问题中所述):

Query:  search=* -owners:testA -owners:testB&queryType=full&$orderby=created desc

正确:

Query:  search=packageId:/.*/ -owners:testA -owners:testB&queryType=full&$orderby=created desc