Sitecore7 LinqHelper.CreateQuery越野车? [英] Sitecore7 LinqHelper.CreateQuery Buggy?

查看:77
本文介绍了Sitecore7 LinqHelper.CreateQuery越野车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是一个澄清类型的问题,而不是关于LinqHelper.CreateQuery方法的实际问题.

This is more of a clarification type question rather than actual problem regarding LinqHelper.CreateQuery method.

所以,
此方法有3个重载.这里有问题的2个是: 1. LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel) 2. LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel, startLocationItem) [我这里没有使用任何其他上下文,所以使用默认的null]

So,
This method has 3 overloads. The 2 in question here, are: 1.LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel) 2.LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel, startLocationItem) [I haven't used any additional context here so used the default null]

现在,
为了搜索内容树中特定位置的项目(例如,在特定文件夹下,您有1000个项目),我可以使用以下方法1进行查询:

Now,
In order to search for items with in a specific location of the content tree ( for example under a particular folder you have 1000 items) I can use method 1 using the query:

query = "location:{FOLDER_GUID};+custom:my_filed_name|bla_bla"

哪个效果很好. 但是(据我从方法签名中了解到的),我也应该能够使用方法2,如下所示:

Which works perfectly. But (from what I understood from the method signature is that) I should also be able to use method 2 like the following:

SitecoreIndexableItem folderID = SitecoreIndexableItem)contextDatabase.GetItem({FOLDER_GUID});
var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));
using (var context = index.CreateSearchContext())
{
    List<SearchStringModel> searchStringModel = new List<SearchStringModel>();
    searchStringModel.Add(new SearchStringModel("my_field_name", "bla_bla"));
    List<Sitecore.Data.Items.Item> resultItems = LinqHelper.CreateQuery(context, searchStringModel, folderID).Select(toItem => toItem.GetItem()).ToList();                
}

问题在于上述方法(方法2),搜索工作正常,而"startLocationItem"(在这种情况下为folderID)不起作用.

Problem is for the above method (method 2) the searching works fine, what doesn't work is the "startLocationItem" (folderID in this case).

例如,
我的整个网站核心树中的 IF 中共有3个项目,其中包含"my_filed_name = bla_bla"
,文件夹中只有1个项目包含"my_filed_name = bla_bla"({FOLDER_GUID},在这种情况下为垂直文件夹")
之后
方法1返回1项(正确) 但是,方法2返回3个项目,尽管"startLocationItem = {FOLDER_GUID} ...(我不认为这是正确的)

FOR EXAMPLE,
IF in my entire sitecore tree has total 3 items containing "my_filed_name=bla_bla"
BUT, only 1 item contains "my_filed_name=bla_bla" in the Folder ({FOLDER_GUID}, "the perticular folder" in this case)
THEN,
Method 1 returns 1 item (WHICH IS CORRECT) BUT, Method 2 returns 3 items, despite "startLocationItem = {FOLDER_GUID} ... (WHICH I DONT THINK IS CORRECT)

问题是:
1.方法1中"startLocationItem"的确切目的是什么?
2.使用位置"过滤器或方法2的startLocationItem"有什么好处?

Question is :
1. What is the exact purpose of "startLocationItem" in Method 1 ?
2. And what's the benefit of using "location" filter or "startLocationItem for method 2" ?

推荐答案

LinqHelper是内部帮助程序类,在正常操作中不应使用.这是为了帮助Sitecore UI与搜索后端进行对话.它的语法可以随时更改,因此有可能破坏基于它的内容,并且也没有文档记录.

LinqHelper is an internal helper class and should not be used in normal operation. It is to help the Sitecore UI talk to the search back-end. Its syntax could be changed at any time so could potentially break things based on it and it is also not documented.

最好将查询转换为普通的Linq查询,即

You would be better to convert your query into a normal Linq query ie

using (var context = index.CreateSearchContext)
{
    context.GetQueryable<SearchResultItem>().Where(x =>x.Paths.Contains(ID.Parse("your GUID Here")))
}

LinqHelper字符串中的位置"等同于存储在索引中的路径"(或_path)字段.

The 'location' in the LinqHelper string is equivalent to the Paths (or _path) field stored in the index.

此字段包含一个项目的所有父项目的列表,作为GUID的列表保存.

This field contains a list of all the parent items of an item, held as a list of GUIDs.

通过_path过滤,您可以将查询限制在树的某个节点上,而不会影响得分,例如:

By filtering by _path you restrict the query to a certain node of the tree without effecting the score, for example:

/home (id:1234) 
   /animals (id:5678 = path:1234 / 5678
       /cats (id:1111) = path: 1234 / 5678 / 1111
       /dogs (id:2222) = path: 1234 / 5678 / 2222 
   /cars (id:4567) = path: 1234 / 4567
       /sports (id:3333) = path: 1234 / 4567 / 3333

如果您对动物进行过滤(即5678),则仅限制搜索该项目及其子项.

If you filter on animals (ie 5678) you restrict the search only that item and its children.

使用过滤器意味着您可以限制搜索的上下文,而不会影响主查询的评分,因此最终您将得到:

Using a filter means you can restrict the context of a search without that part effecting the scoring of the main query, so you would end up with:

using (var context = index.CreateSearchContext)
{
    context.GetQueryable<SearchResultItem>().Where(x =>Name.Contains("Exciting"))
           .Filter(y => y.Paths.Contains(ID.Parse("your GUID Here")
}

这只会在您过滤的那部分树中搜索名称包含令人兴奋"的地方.

This would search only inside the part of the tree you have filtered by for where name contains 'exciting'.

希望有帮助:)

这篇关于Sitecore7 LinqHelper.CreateQuery越野车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆