?? sortbymultifields.java
字號:
package chapter7;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.store.RAMDirectory;
public class SortByMultiFields {
static String[] ContentList = { "搜索 引擎","Lucene 使用 方便", "使用 Lucene","Lucene 功能 強大", "Lucene 開放 源碼" , "Lucene 源碼" };
static String[] NumberList = { "No.0", "No.1", "No.2", "No.3", "No.4","No.2"};
static String[] OrderList = { "0", "3", "2", "3","1","1"};
public static void main(String[] args) throws IOException{
searchIndex();
}
// 創(chuàng)建索引并通過Sort改變檢索結果排序
private static void searchIndex() throws IOException{
try{
RAMDirectory ramdirectory = new RAMDirectory(); // 內存目錄
IndexWriter writer = new IndexWriter(ramdirectory,new StandardAnalyzer(),true);
for (int i = 0; i < ContentList.length; i++)
{
Document document = new Document(); // 創(chuàng)建文檔對象
// 創(chuàng)建域對象
Field fieldContent = new Field("Content", ContentList[i], Field.Store.YES, Field.Index.TOKENIZED);
Field fieldNumber = new Field("Number" , NumberList[i] , Field.Store.YES, Field.Index.TOKENIZED);
Field fieldOrder = new Field("Order" , OrderList[i] , Field.Store.YES, Field.Index.TOKENIZED);
document.add(fieldContent); // 添加創(chuàng)建的文本域到當前文檔
document.add(fieldNumber);
document.add(fieldOrder);
writer.addDocument(document); // 完成的文檔添加到索引
}
writer.close(); // 關閉索引
IndexSearcher searcher = new IndexSearcher(ramdirectory); // 創(chuàng)建檢索器
QueryParser parser = new QueryParser("Content",new StandardAnalyzer()); // 創(chuàng)建查詢分析器
Query query = parser.parse("Lucene"); // 生成查詢對象
Hits rstDoc;
System.out.println("(a)Lucene默認相關性排序");
System.out.println("-----------------------------------");
rstDoc = searcher.search(query); // Lucene默認相關性排序
for (int i = 0; i < rstDoc.length(); i++) // 遍歷獲取文檔,并讀取相關參數(shù)
{
Document doc = rstDoc.doc(i);
System.out.println( doc.get("Order") + " " + doc.get("Number") + " " + doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
}
System.out.println("");
System.out.println("(b)Sort指定域Order文檔排序");
System.out.println("-----------------------------------");
rstDoc = searcher.search(query,new Sort("Order")); // Sort靜態(tài)常量INDEXORDER文檔序排序
for (int i = 0; i < rstDoc.length(); i++) // 遍歷獲取文檔,并讀取相關參數(shù)
{
Document doc = rstDoc.doc(i);
System.out.println( doc.get("Order") + " " + doc.get("Number") + " " + doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
}
System.out.println("");
System.out.println("(c)Sort指定聯(lián)合域Order,Number文檔排序");
System.out.println("-----------------------------------");
SortField SortArray1[];
SortArray1 = new SortField[]{new SortField("Order"),new SortField("Number")};
rstDoc = searcher.search(query,new Sort( SortArray1 )); // Sort靜態(tài)常量INDEXORDER文檔序排序
for (int i = 0; i < rstDoc.length(); i++) // 遍歷獲取文檔,并讀取相關參數(shù)
{
Document doc = rstDoc.doc(i);
System.out.println(doc.get("Order") + " " + doc.get("Number") + " " + doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
}
System.out.println("");
System.out.println("(d)Sort指定聯(lián)合域Number,Order文檔排序");
System.out.println("-----------------------------------");
SortField SortArray2[];
SortArray2 = new SortField[]{new SortField("Number"),new SortField("Order")};
rstDoc = searcher.search(query,new Sort( SortArray2 )); // Sort靜態(tài)常量INDEXORDER文檔序排序
for (int i = 0; i < rstDoc.length(); i++) // 遍歷獲取文檔,并讀取相關參數(shù)
{
Document doc = rstDoc.doc(i);
System.out.println( doc.get("Number") + " " + doc.get("Order") + " " + doc.get("Content") + " Boost: " + doc.getBoost() + ", score : " + rstDoc.score(i));
}
searcher.close();
} catch(ParseException e){
System.out.println("ParseException ");
} catch(IOException e){
System.out.println("IOException ");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -