gpt4 book ai didi

sitecore - 在 Sitecore 中进行简单搜索

转载 作者:行者123 更新时间:2023-12-02 21:44:14 29 4
gpt4 key购买 nike

将 Sitecore 8.2 与 MVC 结合使用。
我正在尝试在 MVC View 中实现搜索功能。 (带有文本框和提交按钮)

内容树中有一个名为 Books 的文件夹,其中包含项目列表。每个项目都有这些字段 - 标题、作者、价格

当用户搜索某个术语时,将检查该术语是否与该项目的 3 个字段中的任何一个匹配,并返回结果。

此方法不起作用,因为它返回 null Item。

public PartialViewResult GetSearchBooks(string txtSearch)
{

string index = string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name);
List<SearchResultItem> query;
List<Item> matches = new List<Item>();

using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
{
query = context.GetQueryable<SearchResultItem>()
.Where(p => p.Path.StartsWith("/sitecore/content/Book")).ToList();
}

foreach(SearchResultItem sritem in query)
{
Item item = sritem.GetItem(); //item is null here

if(item.Fields["Title"].Value.Contains(txtSearch) ||
item.Fields["Title"].Value.Contains(txtSearch) ||
item.Fields["Title"].Value.Contains(txtSearch))
matches.Add(item);
}

return(matches);
}

这是正确的方法吗?如果没有,请推荐一个。

最佳答案

路径

避免像这样查询路径:

context.GetQueryable<SearchResultItem>()
.Where(p => p.Path.StartsWith("/sitecore/content/Book"));

改为使用

context.GetQueryable<SearchResultItem>()
.Where(p => p.Paths.Contains(idOfBookFolderItem));

有关原因的更多信息,请参阅 http://blog.paulgeorge.co.uk/2015/05/29/sitecore-contentsearch-api-filtering-search-on-folder-path/

方法

您需要一次性将整个查询交给搜索 API。

        List<SearchResultItem> matches;

using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
var predicate = PredicateBuilder.True<SearchResultItem>();

// must have this (.and)
predicate = predicate.And(p => p.Paths.Contains(bookFolderItem.ID));

// must have this (.and)
predicate = predicate.And(p => p.Name == searchTerm);

matches = context.GetQueryable<SearchResultItem>().Where(predicate).ToList();
}

这将返回 SearchResultItems 而不是 Items。如果您需要该项目,只需调用 GetItem。

Matches[i].GetItem()

空项目

这可能表明您的索引与数据库不同步。尝试从控制面板重新索引,或者对于 Web 数据库,重新发布预期内容。

搜索模板字段

这只是搜索项目名称。您只能在 SearchResultItem 类中指定通用字段。如果您想搜索项目的特定字段,您可以继承SearchResultItem并添加这些字段。

public class BookSearchResultItem : SearchResultItem
{
[IndexField("Book Title")]
public string BookTitle { get; set; }

[IndexField("Book Author")]
public string BookAuthor { get; set; }
}

然后您可以将其传递到查询中并在这些字段上进行搜索

        List<BookSearchResultItem> matches;

using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
var predicate = PredicateBuilder.True<BookSearchResultItem>();

// must have this (.and)
predicate = predicate.And(p => p.Paths.Contains(bookFolderItem.ID));

// must have this (.and)
predicate = predicate.And(
PredicateBuilder.False<BookSearchResultItem>() // in any of these fields
.Or(p => p.BookTitle == searchTerm)
.Or(p => p.BookAuthor == searchTerm)
.Or(p => p.Name == searchTerm));

matches = context.GetQueryable<BookSearchResultItem>().Where(predicate).ToList();
}

搜索所有“内容”

如果您发现必须指定显式字段是一件不必要的麻烦,或者您正在使用不同字段跨不同模板执行搜索,则可以使用特殊的计算“内容”字段,它将项目中的所有文本数据组合成一个索引字段。因此,而不是执行此操作的原始查询

predicate = predicate.And(p => p.Name == searchTerm);

您可以只使用

predicate = predicate.And(p => p.Content == searchTerm);

它将查找项目的任何字段中存在搜索词的结果。

关于sitecore - 在 Sitecore 中进行简单搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090312/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com