在DateTime-range上使用ContentSearch-API查询Sitecore Lucene-index

Thom Puiman |

目前,我正在一个项目中实施Sitecore 7.0 Update 2

在我的数据模板中,我有一个名为“开始日期”和另一个“结束日期”的字段。创建这两个字段的类型为“日期”(不是日期时间)。因此,当我编辑和创建项目时,它显示了一个日期选择器,并且我提交了一些内容为虚拟内容,并且上个月和当月的开始日期和结束日期为日期的日期。

我要实现的是在选定的月份内获取所有项目。我的方法包含一个月份和年份整数作为参数。这应该控制项目的开始和结束日期,该日期应从Lucene sitecore_master_index获取。Sitecore的Date字段的原始值是ISO datetime字符串。

因此,这是我尝试从该选定月份中获取所有项目的查询。

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();
    }
}

有了这个Where语句,我希望将Events-template的所有项目返回应该包含该月内日期的某个位置。但是它返回一个空的结果集。缺点是我无法调试Lamba表达式,因此不幸的是我不知道该范围的值。但是在1年和9999年之间不是:) IQueryable执行查询并返回了某些内容后,列表中的对象包含属性的正确DateTime。因此,它将字段正确地从索引映射到属性。另外,如果我删除了日期检查,那么只有TemplateID检查才是where-子句,它将返回结果。但是,即使将BeginDate与DateTime.MinValue和DateTime.MaxValue进行比较也不会返回任何内容。

它使用了我为此创建的POCO类EvenSearchResultItem。在此类中,我已将字段映射到属性,并添加了TypeConverter将其转换为DateTime。至少应该...

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,但结果没有太大不同)。看到:

<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>

因此,在Luke(用于查看Lucene索引内容的Java应用程序)中,在重新索引后,该字段出现并包含该项目的给定日期(yyyyMMdd> 20140214)。就像其他日期字段一样,例如__smallCreatedDate。

我可以通过将查询修改为:

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();

这是Stack Overflow另一个问题的解决方案。但我不认为这是最佳实践和可靠的方法。不幸的是,谷歌没有其他选择。我猜像我想像的那样可能吗?

有没有人有过从IQueryable过滤DateTime上的Lucene结果的经验?可以指出正确的方向吗?

艾哈迈德·奥库(Ahmed Okour)

尝试以下方法:

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索引任何日期字段时,它被索引为数字,格式将为“ yyyyMMdd”,例如2014年2月18日被索引为20140218,因此您可以看到它被存储为一个整数,并且年,月和日位于同一字段中,因此您无法与仅年或仅月等进行比较。

现在在Sitecore linq中,如果要查询日期字段,则必须与“ DateTime”类型进行比较,Sitecore知道在将日期时间对象传递给Lucene之前,必须将其转换为“ yyyyMMdd”格式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Sitecore Lucene Index队列在Prod服务器中落后

来自分类Dev

在位上应用Lucene查询

来自分类Dev

如何在Sitecore中使用ContentSearch API查询多个字段

来自分类Dev

Lucene Index:缺少文件

来自分类Dev

Lucene Index:缺少文件

来自分类Dev

如何使用Stratio Cassandra Lucene Index进行小写前缀过滤

来自分类Dev

使用 apache lucene 索引中的 Field.index

来自分类Dev

在@indexedEmbedded对象ID上的Lucene查询

来自分类Dev

Lucene或使用布尔查询进行搜索

来自分类Dev

使用Lucene查询相关文档字段

来自分类Dev

使用 Lucene 7 进行日期范围查询

来自分类Dev

如何在Orchard CMS上使用Lucene查询语法

来自分类Dev

搜索结果是否使用Sitecore 7 ContentSearch API排序?

来自分类Dev

如何使用Lucene索引Sitecore中的子内容?

来自分类Dev

Sitecore和Lucene搜索使用NGram自动完成

来自分类Dev

如何使用Lucene索引Sitecore中的子内容?

来自分类Dev

指定在 sitecore lucene 中使用哪个索引

来自分类Dev

Cassandra Lucene Index布尔语法

来自分类Dev

何时在Sitecore 7版本中绝对在Lucene上使用SOLR?

来自分类Dev

如何根据lucene搜索结果查询lucene?

来自分类Dev

如何通过Item Web API访问Sitecore Lucene搜索?

来自分类Dev

查询扩展lucene

来自分类Dev

解释Lucene的查询

来自分类Dev

短语查询的Lucene评分

来自分类Dev

如何使用Lucene查询界面在值列表中进行搜索

来自分类Dev

Lucene:前缀查询不能与WhitespaceAnalyzer一起使用

来自分类Dev

RavenDb。使用索引在Lucene查询中排序语法

来自分类Dev

使用 Apache Lucene 进行布尔查询的结果编号

来自分类Dev

在Lucene中向文档添加不可索引字段-不推荐使用Field.Index

Related 相关文章

热门标签

归档