Lucene三(索引加權)

  在建立索引的時候,爲指定的Document對象加權會增加該文檔的評分,使其在搜索結果中靠前。使用Document對象的setBoost方法可以爲索引加權,代碼如下:

先來測試一下不加權時索引的搜索結果,需要一個search方法:

public void search() {
try {
IndexReader reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
TermQuery query = new TermQuery(new Term("content","like"));
TopDocs tds = searcher.search(query, 10);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println("("+sd.doc+")"+doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id"));
}
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

單元測試:

@Test
public void testSearch(){
IndexUtil iu = new IndexUtil();
iu.search();
}

測試輸出結果:

(4)mike[[email protected]]-->5
(3)jetty[[email protected]]-->4
(5)jake[[email protected]]-->6
(0)zhangsan[[email protected]]-->1
(1)lisi[[email protected]]-->2
(2)john[[email protected]]-->3

修改添加索引方法Index,爲指定索引加權,這裏修改前先使用了一個map來存儲權值信息,紅色部分爲加權的代碼:

public class IndexUtil {

private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"};
private String[] contents = {
"welcome to visited the space,I like book",
"hello boy, I like pingpeng ball",
"my name is cc I like game",
"I like football",
"I like football and I like basketball too",
"I like movie and swim"
};
private Date[] dates = null;
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","jetty","mike","jake"};
//用於存放加權信息
private Map<String,Float> scores = new HashMap<String,Float>();

private Directory directory = null;

public IndexUtil() {
try {
scores.put("itat.org",2.0f);
scores.put("zttc.edu", 1.5f);

directory = FSDirectory.open(new File("F:\\stady\\JAVA\\other\\Lucene\\test\\index02"));
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 建立索引
*/
public void index(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
//創建前先刪除索引
writer.deleteAll();
Document doc = null;
for(int i = 0; i < ids.length; i++){
doc = new Document();
doc.add(new Field("id", ids[i], Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",contents[i],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
String et = emails[i].substring(emails[i].lastIndexOf("@")+1);
//加權操作。默認爲1.0f
if(scores.containsKey(et)) {
doc.setBoost(scores.get(et));
} else {
doc.setBoost(0.5f);
}

writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(writer != null) writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

再次運行單元測試方法testSearch,運行結果爲:

(5)jake[[email protected]]-->6
(0)zhangsan[[email protected]]-->1
(1)lisi[[email protected]]-->2
(4)mike[[email protected]]-->5
(3)jetty[[email protected]]-->4
(2)john[[email protected]]-->3

明顯看到此時加權成功!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章