如何做一个Lucene搜索与Sitecore的项目的具体日期?具体、日期、项目、如何做一个

2023-09-05 04:00:25 作者:等风初吻

我有一个内容产品Sitecore的名为日期字段'EVENTDATE。我想搜索这些项目使用Lucene.Net具体的日期。

下面是code我做过尝试,但我没有得到的结果是:

  VAR指数= SearchManager.GetIndex(event_search_index);
VAR项目=新的名单,其中,EventDetailItem>();
变种eventDateString = eventDate.Year.ToString(D4)+ eventDate.Month.ToString(D2)+ eventDate.Date.Day.ToString(D2);

使用(VAR上下文=新IndexSearchContext(指数))
{
    VAR搜索关键词=新Lucene.Net.Index.Term(EVENTDATE,eventDateString);
    VAR的查询=新Lucene.Net.Search.TermQuery(搜索关键词);
    变种topFieldDocs = context.Searcher.Search(查询,1);
    如果(topFieldDocs!= NULL)
    {
        VAR scoreDocs = topFieldDocs.ScoreDocs;
        的foreach(在scoreDocs VAR scoreDoc)
        {
            VAR DOC = context.Searcher.Doc(scoreDoc.doc);
            VAR uriField = doc.GetField(_网址);
            VAR itemUri =新Sitecore.Data.ItemUri(uriField.StringValue());
            变种项= Sitecore.Context.Database.GetItem(itemUri.ToDataUri());

            如果(项目== NULL)继续;

            VAR eventItem =新EventDetailItem(项目);
            items.Add(eventItem);
         }
     }
}
 

解决方案

您使用的是标准的 Sitecore的日期字段(切换到原始值查看),所以日期存储为 YYYYMMDDTHHMMSS 字符串,如:

  20130418T140122
 
Lucene

然后使用 TermQuery 与查询的第一部分唯一的,例如:

  20130418
 

所以,你看不到任何结果,为 TermQuery 精确匹配字段值。

您应该可尝试 WildcardQuery 20130418 * 或使用 RangeQuery 20130418T000000 包括这个值和 20130819T000000 不包括此值。

在这里你可以找到有关疑难解答Sitecore的更多信息和Lucene的问题。

I've a Content item is Sitecore with the date field named 'EventDate'. I want to search those items with specific date using Lucene.Net.

Below is the code I've tried but i am not getting the result:

var index = SearchManager.GetIndex("event_search_index");   
var items = new List<EventDetailItem>();   
var eventDateString = eventDate.Year.ToString("D4") + eventDate.Month.ToString("D2") +  eventDate.Date.Day.ToString("D2");

using (var context = new IndexSearchContext(index))
{
    var searchTerm = new Lucene.Net.Index.Term("EventDate", eventDateString);    
    var query = new Lucene.Net.Search.TermQuery(searchTerm);   
    var topFieldDocs = context.Searcher.Search(query, 1);    
    if (topFieldDocs != null) 
    {
        var scoreDocs = topFieldDocs.ScoreDocs;
        foreach (var scoreDoc in scoreDocs)
        {
            var doc = context.Searcher.Doc(scoreDoc.doc);
            var uriField = doc.GetField("_url");
            var itemUri = new Sitecore.Data.ItemUri(uriField.StringValue());
            var item = Sitecore.Context.Database.GetItem(itemUri.ToDataUri());

            if (item == null) continue;

            var eventItem = new EventDetailItem(item);
            items.Add(eventItem);
         }
     }
}

解决方案

You're using standard Sitecore date field (switch to raw values view) so the date is stored as yyyyMMddTHHmmss string, e.g.:

20130418T140122

And then you use TermQuery with the first part of the query only, e.g.:

20130418

So you don't see any result as TermQuery matches exact field value.

You should either try WildcardQuery with 20130418* or use RangeQuery starting from 20130418T000000 including this value and ending on 20130819T000000 excluding this value.

Here you can find more information about troubleshooting Sitecore and Lucene problems.