How to do a Lucene search with Sitecore item with specific date?

纵然是瞬间 提交于 2019-12-24 01:45:10

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/16082256/how-to-do-a-lucene-search-with-sitecore-item-with-specific-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!