Sitecore 通过Lucene.Net获取最新文章
我想根据一个字段获取最近的文章:pubdate。目前我有以下方法:Sitecore 通过Lucene.Net获取最新文章,sitecore,lucene.net,sitecore6,lucene,Sitecore,Lucene.net,Sitecore6,Lucene,我想根据一个字段获取最近的文章:pubdate。目前我有以下方法: private void GetLastIndexId() { string indexLocation = @"C:\\inetpub\wwwroot\MyWebsite\Data\indexes\newsArticle"; Directory dir = FSDirectory.GetDirectory(indexLocation);
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();
}
}
}
private void GetLastIndexId()
{
string indexLocation=@“C:\\inetpub\wwwroot\MyWebsite\Data\index\newarticle”;
Directory dir=FSDirectory.GetDirectory(indexLocation);
IndexReader IndexReader=IndexReader.Open(dir);
IndexSearcher indexSearch=新的IndexSearcher(indexReader);
Analyzer Analyzer=新的StandardAnalyzer();
QueryParser qp=新的QueryParser(“id”,分析器);
Query Query=qp.Parse(“发布日期:[2012-01-01T00:00:000-00:00 3012-01-01T00:00:000-00:00]”;
点击次数=索引搜索(查询);
List myHits=新列表();
对于(int i=0;i
编辑:我这样做是从内容项中获取长度为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();
}
}
var sortField=new sortField(“pubdate”,sortField.STRING,true);
对于(int i=0;i0)
{
lastPubDate=doc.GetValues(“pubdate”).First();
}
}