首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在迁移到新版本的Lucene时转换使用RangeQuery的BooleanQuery?

如何在迁移到新版本的Lucene时转换使用RangeQuery的BooleanQuery?
EN

Stack Overflow用户
提问于 2015-01-30 23:05:54
回答 2查看 337关注 0票数 0

我有一个继承的应用程序(使用Sitecore CMS)。我们最近刚刚升级了Sitecore,这需要我们使用更新版本的Lucene.Net。我们的一些旧代码崩溃了。问题是,我不太明白这段代码想要做什么。我对Lucene查询一点也不熟悉。特别是,我知道我们的RangeQueries现在必须是TermRangeQueries,但是当涉及到重写这段代码时,我被卡住了,因为我找不到BooleanQuery的替代品,并且它不接受TermRangeQuery作为输入。

代码语言:javascript
运行
复制
     BooleanQuery lQuery = new BooleanQuery();
     lQuery.Add(new TermQuery(new Term("_shorttemplateid", string.Format("{0}", ShortID.Encode(templateId).ToLowerInvariant()))),
                Lucene.Net.Search.Occur.MUST);
     if (string.IsNullOrEmpty(endDateItemFieldName))
     {
         lQuery.Add(
            new RangeQuery(
               new Term(startDateItemFieldName, startDateTime),
               new Term(startDateItemFieldName, endDateTime), true),
               Lucene.Net.Search.Occur.MUST);
     }
     else
     {
         lQuery.Add(
            new RangeQuery(
               new Term(startDateItemFieldName, startDate.ToString(DATE_TIME_FORMAT)),
               new Term(startDateItemFieldName, string.Format("{0}{1}", endDate.ToString("yyyyMMdd"), endTimeStamp)), true),
               Lucene.Net.Search.Occur.SHOULD);
         lQuery.Add(
            new RangeQuery(
               new Term(endDateItemFieldName, startDate.ToString(DATE_TIME_FORMAT)),
               new Term(endDateItemFieldName, string.Format("{0}{1}", endDate.ToString("yyyyMMdd"), endTimeStamp)), true),
               Lucene.Net.Search.Occur.MUST);
     }
EN

Stack Overflow用户

发布于 2015-01-31 02:29:50

示例中的代码使用以下逻辑构建Lucene查询:

伪码:

代码语言:javascript
运行
复制
Match all documents 

     That have a specific template ID

     AND

         IF an endDateItemFieldName is present

             The start date must be between date X and Y

         ELSE

             The start date can be between date X and Y
             But the end date must be between date X and Y

在幕后,这将导致一个类似如下的Lucene查询:

代码语言:javascript
运行
复制
+_shorttemplateid:3f2504e04f8941d39a0c0305e82c3301 start:[20020101 TO 20030101] +end:[20020101 TO 20030101]

在Sitecore 7+中,许多"Luceneness“已经被抽象出来,并由LINQ搜索提供商为您生成。这允许您在搜索实现(例如,Solr)之间切换,而无需对代码进行任何实质性的重构。因为LINQ是如此广为人知,所以使用LINQ提供程序通常更容易让开发人员掌握。

下面是使用新的LINQ提供程序的等效搜索查询。

代码语言:javascript
运行
复制
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.ContentSearch.SearchTypes;
using System;
using System.ComponentModel;

namespace YourNamespace
{
    public class DateSearchResultItem : SearchResultItem
    {
        [IndexField("startdate"), TypeConverter(typeof(IndexFieldDateTimeValueConverter))]
        public DateTime StartDate { get; set; }

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

下面是一个用法示例:

代码语言:javascript
运行
复制
ISearchIndex index = ContentSearchManager.GetIndex("sitecore_web_index");
using (var context = index.CreateSearchContext())
{
    var results = context.GetQueryable<DateSearchResultItem>()
        .Where(item => item.TemplateId == new ID(templateId));

    if (String.IsNullOrEmpty(endDateItemFieldName))
    {
        results = results
            .Where(item => item.StartDate >= startDateTime)
            .Where(item => item.StartDate <= endDateTime);
    }
    else
    {
        results = results
            .Where(item => item.EndDate >= startDateTime)
            .Where(item => item.EndDate <= endDateTime);
    }

    var compiledQuery = results.GetResults();
    int totalMatches =  compiledQuery.TotalSearchResults;

    foreach (var hit in compiledQuery.Hits)
    {
        Item item = hit.Document.GetItem();
    }
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28238721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档