Lucene.net 没有将关键字与搜索字符串分开

Lucene.net is not separating keywords from search string

我在基于 Umbraco CMS 的网站上实现了网站搜索功能。对于站点搜索,我们使用 Lucene.net,它可以轻松与 Umbraco 集成。

如果我使用带有单个关键字的搜索字符串,搜索将非常有效。例如,有一个页面(在网站中)有一个名为 "Domestic use video licence" 的 header。现在,如果我使用搜索字符串 "domestic" 或 "video",它就会起作用。但是,如果我使用 "domestic video" 或 "domestic licence",则不会 returned.

我想构建一个查询,不仅 return 匹配整个 "domestic video",而且匹配包含文本 "domestic whatever video"、"domestic" 和 "video" 在其中。在所有结果 returned 中,完全匹配的页面应该有更高的分数,以便它们在搜索结果中占据显着位置。

有人有什么建议吗?我当前的代码如下:

var criteria = ExamineManager.Instance
    .SearchProviderCollection["WebSearcher"]
    .CreateSearchCriteria(IndexTypes.Content);
var filter =
    criteria.GroupedOr(
        new[]
        {
            "nodeName", "heading", "content", "metaKeywords", "title", "umbracoNaviHide", "umbracoUrlName",
            "umbracoUrlAlias", "metaCategory", "metaDescription", "metaTags", "heading", "subHeading",
            "quote", "author", "socialCopy", "socialTitle", "socialTitle2", "thumbTitle", "thumbTitle2",
            "thumbCopy", "thumbQuote", "url", "location", "question", "answer"
        }, query)
        .Compile();

var searchResults =
    ExamineManager.Instance.SearchProviderCollection["WebSearcher"].Search(filter)
        .OrderByDescending(x => x.Score).ThenByDescending(d => d.Fields["createDate"]);

我认为您需要使用搜索条件的 GroupedOr 方法来生成查询。

GroupedOr 方法需要两个列表,一个是字段名称,另一个是搜索关键字。例如

var query = provider.CreateSearchCriteria(BooleanOperation.And)
    .GroupedOr(siteSearchFields,
    searchTerms).Compile();

这个 Gist 中有一个完整的例子:https://gist.github.com/tarnacious/1399392