?? boostfieldquery.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.store.RAMDirectory;
public class BoostFieldQuery {
static String[] ContentList = { "Lucene 使用 方便", "Lucene 功能 強大", "Lucene 開放 源碼" };
static String[] NumberList = { "No.1", "No.2", "No.3"};
public static void main(String[] args) throws IOException{
searchIndex();
}
// 創建索引并修改boost值,改變檢索結果排序
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(); // 創建文檔對象
// 創建域對象
Field fieldContent1 = new Field("Content1", ContentList[i], Field.Store.YES, Field.Index.TOKENIZED);
Field fieldContent2 = new Field("Content2", ContentList[i], Field.Store.YES, Field.Index.TOKENIZED);
Field fieldNumber = new Field("Number", NumberList[i], Field.Store.YES, Field.Index.TOKENIZED);
fieldContent1.setBoost((i+1)*2); // 不同域上指定不同Boost值
fieldContent2.setBoost(1.0f/(i+1));
document.add(fieldContent1); // 添加創建的文本域到當前文檔
document.add(fieldContent2); // 添加創建的內容相同文本域到當前文檔
document.add(fieldNumber);
writer.addDocument(document); // 完成的文檔添加到索引
}
writer.close(); // 關閉索引
IndexSearcher searcher = new IndexSearcher(ramdirectory); // 創建檢索器
System.out.println("Lucene Search Content1");
System.out.println("-------------------------------------------");
QueryParser parser1 = new QueryParser("Content1",new StandardAnalyzer()); // 創建查詢分析器
Query query1 = parser1.parse("Lucene"); // 生成查詢對象
Hits rstDoc1 = searcher.search(query1); // 檢索結果保存Hits集合
for (int i = 0; i < rstDoc1.length(); i++) // 遍歷獲取文檔,并讀取相關參數
{
Document doc = rstDoc1.doc(i);
System.out.println(doc.get("Number") + " " + doc.get("Content1") + " Boost: " + doc.getBoost() + ", score : " + rstDoc1.score(i));
}
System.out.println("");
System.out.println("Lucene Search Content2");
System.out.println("-------------------------------------------");
QueryParser parser2 = new QueryParser("Content2",new StandardAnalyzer()); // 創建查詢分析器
Query query2 = parser2.parse("Lucene"); // 生成查詢對象
Hits rstDoc2 = searcher.search(query2); // 檢索結果保存Hits集合
for (int i = 0; i < rstDoc2.length(); i++) // 遍歷獲取文檔,并讀取相關參數
{
Document doc = rstDoc2.doc(i);
System.out.println(doc.get("Number") + " " + doc.get("Content2") + " Boost: " + doc.getBoost() + ", score : " + rstDoc2.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 + -