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



我想根据一个字段获取最近的文章:pubdate。我现在有这种方法:

     private void GetLastIndexId()
        {
            string indexLocation = @"C:\inetpubwwwrootMyWebsiteDataindexesnewsArticle";
            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();
                   }
              }
        }

编辑:我这样做了,我从内容项目中获取长度-1项目。这是一种黑客攻击,因为如果文件夹结构发生变化,那么这可能会失败。

您是否尝试过接受排序参数的 IndexSearcher.Search 重载?

var sortField = new SortField("pubdate", SortField.STRING, /*reverse*/ true);
var hits = searcher.Search(query, /*filter*/ null, /*count*/ 1, new Sort(sortField));

我做了这样的事情:

  var sortField = new SortField("pubdate", SortField.STRING, true);
        for (int i = 0; i < hits.Length(); i++)
        {
            Document doc = hits.Doc(i);
            string getDate = doc.GetValues("pubdate").First();
            if (string.Compare(getDate, lastPubDate) > 0)
            {
                lastPubDate = doc.GetValues("pubdate").First();
            }
        }

最新更新