我有一个内容项是 Sitecore,其日期字段名为“EventDate”。我想使用 Lucene.Net 搜索具有特定日期的项目。
以下是我尝试过的代码,但我没有得到结果:
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);
}
}
}