packagech.lucene.app;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.lucene.document.Document;
importorg.apache.lucene.index.IndexWriter;
importorg.apache.lucene.queryParser.QueryParser;
importorg.apache.lucene.search.IndexSearcher;
importorg.apache.lucene.search.Query;
importorg.apache.lucene.search.ScoreDoc;
importorg.apache.lucene.search.Sort;
importorg.apache.lucene.search.SortField;
importorg.apache.lucene.search.TopDocs;
importorg.junit.Test;
importch.lucene.entity.Article;
importch.lucene.utils.LuceneUtils;
/**
* 排序問題
*@authorJayin
*/
publicclassSixApp {
/**
* 創建索引庫
*@throwsException
*/
@Test
publicvoidcreateIndexDB()
throws
Exception {
//創建javabean 對象
Articleart
=
newArticle(3,
"搜索排序問題"
, "搜索什麼好呢,反正就隨便咯,金木研說的。"
);
/*Article art1 = new Article(2, "搜索排序問題", "搜索什麼好呢,反正就隨便咯,金木研說的。。");
Article art2 = new Article(4, "搜索問題", "搜索什麼好呢金木研說的。。");
Article art3 = new Article(1, "搜索問題", "反正就隨便咯,金木研說的。");*/
//創建document對象,並將 javabean對象轉換爲document對象
Documentdoc
= LuceneUtils.javaBeanToDocument(art);
//創建IndexWrite對象
IndexWriteriw
=
newIndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength());
//將doc寫入到索引庫中
iw.addDocument(
doc);
//關閉
iw.close();
}
/**
* 根據關鍵字搜索
*@throwsException
*/
@Test
publicvoidfindByKeyword()
throws
Exception {
//關鍵字
Stringkeyword
=
"金木";
//創建數組
List<Article>artList
=
newArrayList<Article>();
//創建IndexSearcher字符流對象
IndexSearcherindexSearcher
=
newIndexSearcher(LuceneUtils.getDirectory());
//創建解析器
QueryParserqueryParser
=
newQueryParser(LuceneUtils.getVersion(),"content", LuceneUtils.getAnalyzer());
//封裝關鍵字
Queryquery
=
queryParser.parse(keyword);
//根據關鍵字,去索引庫中的詞彙表搜索
//TopDocs topDocs = indexSearcher.search(query, 100);
//按得分度高低排序
//TopDocs topDocs = indexSearcher.search(query,100);
//創建排序對象
//參數一:id表示依據document對象中的哪個字段排序,例如:id
//參數二:SortField.INT表示document對象中該字段的類型,以常量方式書寫
//參數三:true表示降序,類似於order by iddesc
//參數三:false表示升序,類似於order by idasc
//Sort sort = new Sort(new SortField("id",SortField.INT,false));
//按count字段的降序排列,如果count字段相同的話,再按id的升序排序
Sortsort
=
newSort(
new
SortField("count",SortField.INT,true),
new
SortField("id",SortField.INT,false));
//sort表示排序的條件
TopDocstopDocs
=
indexSearcher.search(query,null,100,sort);
for(
int
i = 0 ;
i<
topDocs.
scoreDocs.
length;
i++){
//獲取對象
ScoreDocscoreDoc
=
topDocs.scoreDocs[i];
//獲取編號
int
no
= scoreDoc.
doc;
//獲取屬性的值
Documentdoc
=
indexSearcher.doc(no);
//document轉換成
javabean
Articleart
= (Article) LuceneUtils.documentToJavaBean(doc, Article.class);
//將art添加到數組集合中
artList.add(art);
for(Articlearticle
:
artList){
System.out.println(article);
}
}
}
}
單字段排序:
Sort
sort =
new Sort(newSortField("id",SortField.INT,true));
TopDocs topDocs =
indexSearcher.search( query,null,1000000,sort);
多字段排序:
Sort sort = new Sort(new SortField("count",SortField.INT,true),new SortField("id",SortField.INT,false));
單字段搜索:
QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(),"content", LuceneUtils.getAnalyzer());
多字段搜索:(推薦)
QueryParser
queryParser =
new MultiFieldQueryParser(LuceneUtils.getVersion(),new
String[]{"centent","title"},
LuceneUtils.getAnalyzer ());