【问题标题】:Sitecore HOWTO: Search item bucket for items with specific valuesSitecore HOW TO:在项目存储桶中搜索具有特定值的项目
【发布时间】:2015-05-09 01:24:42
【问题描述】:

我有一个物品桶,里面有超过 30 000 件物品。我需要的是快速搜索将特定字段设置为特定值的项目,或者更好的是制作类似 SELECT WHERE fieldValue IN (1,2,3,4) 语句的内容。有现成的解决方案吗? 我在网上搜索,唯一找到的是“开发者物品指南” Buckets and Search”,但没有代码示例。

【问题讨论】:

  • 如您所见,您可以通过下面的回答来使用 sitecore 7 api。快速入门指南链接是由我编写的,因此如果在标记答案时考虑到这一点,我将不胜感激。希望这能解决您的问题。

标签: search sitecore sitecore7 bucket sitecore7.5


【解决方案1】:

你需要这样的东西。 Bucket 项目是一个 IIndexable,因此可以使用 Sitecore 7 搜索 API 进行搜索。

下面的代码 sn-p 可以很容易地适应您的需求,这只是修改 where 子句的问题。如果您需要有关 sitecore 7 语法的任何进一步帮助,只需在下面的 QuickStart 博客文章中写下评论,然后我会回复你的。

var bucketItem = Sitecore.Context.Database.GetItem(bucketPath);
  if (bucketItem != null && BucketManager.IsBucket(bucketItem))
  {     
     using (var searchContext = ContentSearchManager.GetIndex(bucketItem as IIndexable).CreateSearchContext())
    {
        var result = searchContext.GetQueryable<SearchResultItem().Where(x => x.Name == itemName).FirstOrDefault();
        if(result != null)
            Context.Item = result.GetItem();
    }
  }

在此处进一步阅读我的博客文章:

http://coreblimey.azurewebsites.net/sitecore-7-search-quick-start-guide/

【讨论】:

  • 你知道为什么 bucketItem as IIndexable 会为空吗?
  • @Ernesto 我在这里参加聚会真的很晚了,但修复应该将您的项目传递给 SitecoreIndexableItem 值构造函数。 var indexableItem = new SitecoreIndexableItem(bucketItem);
【解决方案2】:

使用 Sitecore 内容编辑器

转到存储桶项目,然后在搜索选项卡中,开始输入以下内容(将字段名称和值替换为实际字段名称和值):

自定义:字段名|值

然后回车,你会看到查询的结果,如果你愿意,你可以一次查询多个。

使用 Sitecore 内容搜索 API:

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.Linq.Utilities

ID bucketItemID = "GUID of your bucket item";
ID templateID = "Guid of your item's template under bucket";
string values = "1,2,3,4,5";

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
    var predicate = PredicateBuilder.True<SearchResultItem>();
    predicate = PredicateBuilder.And(item => item.TemplateId == new ID(templateID) 
                                     && item.Paths.Contains(bucketItemID));
    var innerPredicate = PredicateBuilder.False<SearchResultItem>();
    foreach(string val in values.Split(','))
    {
         innerPredicate = PredicateBuilder.False<SearchResultItem>();
         innerPredicate = innerPredicate.Or(item => item["FIELDNAME"] == val);
    }
    predicate = predicate.And(innerPredicate);

    var result = predicate.GetResults();
    List<Item> ResultsItems = new List<Item>();
    foreach (var hit in result.Hits)
    {
       Item item = hit.Document.GetItem();
       if(item !=null)
       {
          ResultsItems .Add(item);
       }
    }
}

以下链接可以作为搜索 API 的良好开端:

  1. http://www.fusionworkshop.co.uk/news-and-insight/tech-lab/sitecore-7-search-a-quickstart-guide#.VPw8AC4kWnI
  2. https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/06/sitecore-7-poco-explained.aspx
  3. https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/05/sitecore-7-predicate-builder.aspx

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多