我想获取基于一个字段的最新文章:发布日期。我现在有这种方法:
private void GetLastIndexId()
{
string indexLocation = @"C:\\inetpub\wwwroot\MyWebsite\Data\indexes\newsArticle";
Directory dir = FSDirectory.GetDirectory(indexLocation);
IndexReader indexReader = IndexReader.Open(dir);
IndexSearcher indexSearch = new IndexSearcher(indexReader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser qp = new QueryParser("id", analyzer);
Query query = qp.Parse("pubdate: [2012-01-01T00:00:000-00:00 3012-01-01T00:00:000-00:00]");
Hits hits = indexSearch.Search(query);
List<Document> myHits = new List<Document>();
for (int i = 0; i < hits.Length(); i++)
{
if (i == hits.Length() - 1)
{
Document doc = hits.Doc(i);
lastPubDate = doc.GetValues("pubdate").First();
}
}
}
编辑:我这样做是从内容项中获取 length-1 项。这是一种黑客行为,因为如果文件夹结构发生更改,那么这可能会失败。
最佳答案
您是否尝试过 IndexSearcher.Search 重载接受排序参数?
var sortField = new SortField("pubdate", SortField.STRING, /*reverse*/ true);
var hits = searcher.Search(query, /*filter*/ null, /*count*/ 1, new Sort(sortField));
关于sitecore - 通过 Lucene.Net 获取最近的文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12521257/