sitecore - 通过 Lucene.Net 获取最近的文章

标签 sitecore lucene.net sitecore6 lucene

我想获取基于一个字段的最新文章:发布日期。我现在有这种方法:

     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/

相关文章:

sitecore - 如何从 Sitecore Analytics 或 DMS 获取推荐 URL 链接

c# - 在 Web 应用程序中使用 IndexSearcher/IndexWriter 的 Lucene.net

c# - 替换 Sitecore 媒体库链接中的空格

sitecore - 以编程方式在媒体库下创建图像

elasticsearch - 模糊查询不返回记录

search - 有没有办法防止使用 Sitecore 搜索和 Lucene 进行部分单词匹配?

lucene - Sitecore syncMaster 索引策略不起作用

javascript - 如何将外部 javascript 加载限制为一次。 Sitecore 实现

c# - 如何将自定义标记放在 sitecore sc 字段中?

Lucene.NET "OR"