Sitecore 如何:在项目桶中搜索具有特定值的项目
Sitecore HOWTO: Search item bucket for items with specific values
我有一个物品桶,里面有超过 30 000 件物品。我需要的是快速搜索将特定字段设置为特定值的项目,或者更好的是制作类似 SELECT WHERE fieldValue IN (1,2,3,4) 的语句。有现成的解决方案吗?
我在网上搜索,唯一找到的是“项目开发人员指南”
Buckets and Search”,但没有代码示例。
使用 Sitecore 内容编辑器:
转到存储桶项目,然后在搜索选项卡中,开始键入以下内容(将字段名和值替换为实际字段名和值):
custom:fieldname|value
然后回车,就可以看到查询的结果,如果需要可以一次查询多个。
使用 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:
- http://www.fusionworkshop.co.uk/news-and-insight/tech-lab/sitecore-7-search-a-quickstart-guide#.VPw8AC4kWnI
- https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/06/sitecore-7-poco-explained.aspx
- https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/05/sitecore-7-predicate-builder.aspx
希望对您有所帮助!
你需要这样的东西。 Bucket 项是一个 IIndexable,因此可以使用 Sitecore 7 搜索 API 对其进行搜索。
下面的代码片段可以很容易地进行调整以满足您的需求,这只是一个修改位置的问题 clause.if 您需要有关 sitecore 7 语法的任何进一步帮助,只需在 QuickStart 博客上写评论 post 下面,我会尽快回复您。
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();
}
}
进一步阅读我的博客 post 此处:
http://coreblimey.azurewebsites.net/sitecore-7-search-quick-start-guide/
我有一个物品桶,里面有超过 30 000 件物品。我需要的是快速搜索将特定字段设置为特定值的项目,或者更好的是制作类似 SELECT WHERE fieldValue IN (1,2,3,4) 的语句。有现成的解决方案吗? 我在网上搜索,唯一找到的是“项目开发人员指南” Buckets and Search”,但没有代码示例。
使用 Sitecore 内容编辑器:
转到存储桶项目,然后在搜索选项卡中,开始键入以下内容(将字段名和值替换为实际字段名和值):
custom:fieldname|value
然后回车,就可以看到查询的结果,如果需要可以一次查询多个。
使用 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:
- http://www.fusionworkshop.co.uk/news-and-insight/tech-lab/sitecore-7-search-a-quickstart-guide#.VPw8AC4kWnI
- https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/06/sitecore-7-poco-explained.aspx
- https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-7-development-team/posts/2013/05/sitecore-7-predicate-builder.aspx
希望对您有所帮助!
你需要这样的东西。 Bucket 项是一个 IIndexable,因此可以使用 Sitecore 7 搜索 API 对其进行搜索。
下面的代码片段可以很容易地进行调整以满足您的需求,这只是一个修改位置的问题 clause.if 您需要有关 sitecore 7 语法的任何进一步帮助,只需在 QuickStart 博客上写评论 post 下面,我会尽快回复您。
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();
}
}
进一步阅读我的博客 post 此处:
http://coreblimey.azurewebsites.net/sitecore-7-search-quick-start-guide/