Sitecore 中的 Lucene 搜索;没有返回结果

Lucene Search in Sitecore; no results returned

我有一个索引生成得很好(可以通过 Luke 浏览所有创建的项目)。在 Luke 中进行了查询,甚至设法返回 return 结果 - 在下面的 C# 代码中实现,但没有返回 return。有什么明显的我遗漏的东西吗?

            totalResults = 0;
        using (var context = ContentSearchManager.GetIndex("custom_search_index").CreateSearchContext())
        {
            var filterPredicate = PredicateBuilder.True<SearchItem>();
            var termPredicate = PredicateBuilder.False<SearchItem>();

            termPredicate = termPredicate
                                .Or(p => p.Name.Like(keyword, 0.75f)).Boost(2.0f)
                                .Or(p => p.Excerpt.Like(keyword))
                                .Or(p => p.SearchTags.Like(keyword))
                                .Or(p => p.HtmlContent.Like(keyword));

            var predicate = filterPredicate.And(termPredicate);
            var query = context.GetQueryable<SearchItem>().Where(predicate);

            var results = query.Page(page, itemsPerPage).GetResults();

            totalResults = results.TotalSearchResults;
            var result = results.Hits.Select(h => GetPage(h.Document)).ToArray();

            return result;
        }

在 Search.Log 我获得了以下点击率

ExecuteQueryAgainstLucene (custom_search_index): _name:1980s~0.75 excerpt:1980s searchtags:1980s htmlcontent:1980s - Filter :

如果我在 Luke 中 运行 '_name:1980s~0.75 excerpt:1980s searchtags:1980s htmlcontent:1980s' 我确实得到了结果!

大多数时候,这表示索引为 out-of-date。例如,结果指向的项目已被删除或尚未发布。重建索引应该导致 Luke 和 Sitecore return 相同。

另外,请检查您的分页是否不排除结果。也许先试试不分页。

因此,如果我使用以下代码:

var results = query.Page(page, itemsPerPage).GetResults();

其中 page 为 1,itemsPerPage 为 5,但我的筛选结果 returns 只有一个值(或小于 itemsPerPage)GetResults() returns 没有结果!

从其他评论看来,您似乎正在使用 page = 1 获取第一页结果。

但是page参数是zero-based,意思是如果你想要第一页就必须使用0.

// This will return the first 5 results (page 1)
query.Page(0, 5).GetResults();

// This will return the next 5 results (page 2)
query.Page(1, 5).GetResults();

这可以通过查看 Page(..) 扩展方法的代码来验证:

return Queryable.Take<TSource>(Queryable.Skip<TSource>(source, page * pageSize), pageSize);