查询Sitecore的Lucene的指数与日期时间范围ContentSearch-API [英] Query Sitecore Lucene-index with ContentSearch-API on DateTime-range

查看:276
本文介绍了查询Sitecore的Lucene的指数与日期时间范围ContentSearch-API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一刻我工作的一个项目,以实现Sitecore的7.0更新2

At this moment i'm working on a project to implement Sitecore 7.0 Update 2

在我的数据模板,我有这个字段名为开始日期和另外一个结束日期。这两个字段的类型'日期'(未日期时间)创建。因此,它显示了一个日期选择器,当我编辑和创建项目,我已经提交给美国虚拟内容和开始和结束日期从上月和本月一些项目。

In my data template i have this field called Begin Date and another one End Date. These two fields are created with the type 'Date' (not Datetime). So it shows a datepicker when i edit and create items and i've filed some items with dummy-content and with Begin and End date from last month and the current month.

我想做到的,是得到一个选择一个月内的所有项目。我的方法包含了一个月,一年整数作为参数。这应该控制的开始和结束日期,应该得到了Lucene sitecore_master_index的项目。 Sitecore的为日期字段的原始值是ISO日期时间串。

What i want to achieve is to get all the items within a selected month. My method contains a month and a year integer as parameter. This should control the items with the begin and end date it should get from the Lucene sitecore_master_index. The raw values of Sitecore for the Date-field are the ISO datetime-strings.

因此​​,这是我尝试从所选月份所有项目的查询。

So this is the query where i try to get all items from that selected month.

private void GetItems(int month, int year)
{
    using (
        IProviderSearchContext context =
                ContentSearchManager.
GetIndex("sitecore_master_index").CreateSearchContext())
    {
         List<EventSearchResultItem> allEvents =     context.GetQueryable<EventSearchResultItem>(new     CultureExecutionContext(Sitecore.Context.Language.CultureInfo))
         .Where(s =>
                s.TemplateId == this.EventTemplateID &&
                ((s.BeginDate.Month == month && s.BeginDate.Year == year) || (s.EndDate.Month == month && s.EndDate.Year == year))
                    )
        .ToList();
    }
}

通过这个当语句我希望返回的活动模板的地方,应包含该月内的日期的所有项目。但是,它返回一个空结果。缺点是,我不能调试兰巴-EX pression,所以很遗憾我不知道在该范围内的值。但是,第一年和9999 :)之间没有东西的IQueryable后已执行的查询和返回的东西,在列表中的对象包含正确的日期时间的属性。所以它映射从索引正确的属性的字段。此外,如果我删除的日期检查所以只有TemplateID检查是在WHERE子句,它返回的结果。但即使是比较BEGINDATE为DateTime.MinValue和DateTime.MaxValue好好尝试一下返回的东西。

With this Where-statement i expect to return all items of the Events-template somewhere that should contain a date within that month. But it returns an empty resultset. Downside is that i can't debug the Lamba-expression, so unfortunately i don't know the value in that scope. But not something between year 1 and 9999 :) After the IQueryable has executed the query and returned something, the objects in the list contains the correct DateTime for the properties. So it maps the fields from the index correctly to the properties. Also if i remove the date-check so only the TemplateID check is the Where-clause, it returns results. But even comparing BeginDate to DateTime.MinValue and DateTime.MaxValue doens't return something.

它使用我已经为此创建了POCO类EvenSearchResultItem。在这堂课我已经映射领域的财产,并且增加了类型转换器将其转换为日期时间。至少,它应该...

It uses the POCO-class EvenSearchResultItem which i've created for this. In this class i've mapped the field to the property and added the TypeConverter to convert it to DateTime. At least, it should...

public class EventSearchResultItem : SearchResultItem
{
    [TypeConverter(typeof(IndexFieldDateTimeValueConverter))]
    [IndexField("__begin_date")]
    public DateTime BeginDate { get; set; }

    [TypeConverter(typeof(IndexFieldDateTimeValueConverter))]
    [IndexField("__end_date")]
    public DateTime EndDate { get; set; }
}

而在Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config我已经在-tag中添加的字段(也试过-tag,但结果没有什么不同)。 参见:

And in the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config i've added the field within the -tag (also tried -tag, but not that different in result). See:

<field luceneName="__begin_date" storageType="yes" indexType="tokenized" format="yyyyMMdd">Begin Date</field>
<field luceneName="__end_date" storageType="yes" indexType="tokenized" format="yyyyMMdd">End Date</field>

因此​​,在卢克的重新索引字段出现,并包含给定日期的项目(以YYYYMMDD> 20140214)后(Java的应用程序来查看一个Lucene索引中的内容)。就像其他日期字段,如__smallCreatedDate。

So in Luke (the Java-application to view the contents of a Lucene-index) after an re-index the field appears and contains the given date for that item (in yyyyMMdd > 20140214). Just like other date-fields such as __smallCreatedDate.

我可以通过修改我的查询来解决这个

I can solve this by modifying my query to:

List<EventSearchResultItem> allEvents = context.GetQueryable<EventSearchResultItem>()
    .Where(s =>
        (s["Begin Date"].StartsWith(string.Concat(year, month.ToString("00"))) || s["End Date"].StartsWith(string.Concat(year, month.ToString("00"))))
    )
    .ToList();

这是在这里对​​堆栈溢出另外一个问题的解决方案。但我不认为是最佳做法和可靠的。谷歌并没有任何其他的替代品很遗憾。我想的东西,我想这是可能的吗?

This was a solution in another question here on Stack Overflow. But i don't consider that as best practice and reliable. Google didn't had any other alternatives unfortunately. I guess something as i imagine it would be possible right?

有没有人有经验的IQueryable过滤的日​​期时间Lucene的,结果如何?并能在正确的方向指向我?

Does anyone has experience filtering Lucene-results on DateTime from IQueryable? And can point me in the right direction?

推荐答案

试试这个,如果它的工作原理:

Try this if it works:

private void GetItems(int month, int year)
        {
            DateTime startDate = new DateTime(year,month,1);
            DateTime endDate = new DateTime(year,month, DateTime.DaysInMonth(year, month));
            using ( IProviderSearchContext context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                List<EventSearchResultItem> allEvents = context.GetQueryable<EventSearchResultItem>(new CultureExecutionContext(Sitecore.Context.Language.CultureInfo))
                .Where(s =>
                       s.TemplateId == this.EventTemplateID &&
                       ((s.BeginDate >= startDate) || (s.EndDate <= endDate))
                           )
               .ToList();
            }
        }

编辑:只是为了解释为什么你的做法没有奏效,当Lucene索引的任何日期字段,它就会建立索引号,格式为年月日,例如2014年2月18日被收录为20140218,这样你可以看到它得到存储为一个整数,和年,月,日都在同一个领域,所以你不能只用一年或一个月仅比较等。

Just to explain why you approach didn't work, when lucene index any date field, it gets indexed as number, the format will be 'yyyyMMdd', for example 18 Feb 2014 is indexed as 20140218, so as you can see it get stored as a whole number, and the year,month and day are in the same field, so you can't compare with year only or month only etc.

现在在Sitecore的LINQ,如果你想对一个日期字段进行查询,则必须用日期时间类型比较,Sitecore的人都知道DateTime对象必须通过这个Lucene的前转换为YYYYMMDD格式。

Now in Sitecore linq, if you want to query against a date field, you MUST compare with a 'DateTime' type, Sitecore knows that DateTime object must be converted to 'yyyyMMdd' format before passing this to Lucene.

这篇关于查询Sitecore的Lucene的指数与日期时间范围ContentSearch-API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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