Lucene支持对搜索条件的排序,一个条件或者多个条件,以及是升序还是降序,部分代码如下:
string INDEX_STORE_PATH = Server.MapPath("index"); //INDEX_STORE_PATH 为索引存储目录
string keyword = TextBox2.Text; //搜索内容
Hits myhit = null;
IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);
QueryParser q = new QueryParser("indexcontent", new StandardAnalyzer());
Query query = q.Parse(keyword);
Sort sort = new Sort();
SortField f = new SortField("createdate", SortField.STRING, true);//按照createdate字段排序,true表示降序
sort.SetSort(f);
//多个条件排序
//Sort sort = new Sort();
//SortField f1 = new SortField("createdate", SortField.DOC, true);
//SortField f2 = new SortField("bookname", SortFiedl.INT, false);
//sort.setSort(new SortField[] { f1, f2 });
myhit = mysea.Search(query, sort);
Response.Write("关于:" + keyword + " 搜索到" + myhit.Length() + "个结果<br>");
string keyword = TextBox2.Text; //搜索内容
Hits myhit = null;
IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);
QueryParser q = new QueryParser("indexcontent", new StandardAnalyzer());
Query query = q.Parse(keyword);
Sort sort = new Sort();
SortField f = new SortField("createdate", SortField.STRING, true);//按照createdate字段排序,true表示降序
sort.SetSort(f);
//多个条件排序
//Sort sort = new Sort();
//SortField f1 = new SortField("createdate", SortField.DOC, true);
//SortField f2 = new SortField("bookname", SortFiedl.INT, false);
//sort.setSort(new SortField[] { f1, f2 });
myhit = mysea.Search(query, sort);
Response.Write("关于:" + keyword + " 搜索到" + myhit.Length() + "个结果<br>");
对于数据量大(索引文件大于50M)的索引,尽量不要用索引中的字段排序,要用索引ID排序(INDEXORDER);两者效率相差近10倍。
注:lucene.net版本为:2.0.0.4