2

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

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

        }

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

4

2 回答 2

2

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

var sortField = new SortField("pubdate", SortField.STRING, /*reverse*/ true);
var hits = searcher.Search(query, /*filter*/ null, /*count*/ 1, new Sort(sortField));
于 2012-09-21T11:03:37.207 回答
0

我做了这样的事情:

  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();
            }
        }
于 2012-09-21T13:57:38.277 回答