Lucene五(添加日期和數字類型索引)

  日期和數字類型索引可以使用NumericField對象來添加,建立索引、搜索、測試的代碼如下:

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 {
setDates();
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();
}
}

private void setDates() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
dates = new Date[ids.length];
dates[0] = sdf.parse("2010-02-19");
dates[1] = sdf.parse("2012-01-11");
dates[2] = sdf.parse("2011-09-19");
dates[3] = sdf.parse("2010-12-22");
dates[4] = sdf.parse("2012-01-01");
dates[5] = sdf.parse("2011-05-19");
} catch (ParseException 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));
//存儲數字   第三個參數表示是否索引
doc.add(new NumericField("attach",Field.Store.YES,true).setIntValue(attachs[i]));
//存儲日期
doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(dates[i].getTime()));

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();
}
}
}


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);
//這裏doc.getBoost獲取的加權之所以是1.0是因爲該Document是一個新的對象,和建立索引時並沒有關係
System.out.println("("+sd.doc+"-"+doc.getBoost()+"-"+sd.score+")"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach")+","+doc.get("date"));
}
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}


單元測試:

@Test
public void testIndex(){
IndexUtil iu = new IndexUtil();
iu.index();
}

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

運行testIndex後再運行testSearch的結果如下:

(5-1.0-0.84584934)jake[[email protected]]-->6,5,1305734400000
(0-1.0-0.634387)zhangsan[[email protected]]-->1,2,1266508800000
(1-1.0-0.634387)lisi[[email protected]]-->2,3,1326211200000
(4-1.0-0.5981058)mike[[email protected]]-->5,5,1325347200000
(3-1.0-0.21146233)jetty[[email protected]]-->4,4,1292947200000
(2-1.0-0.15859675)john[[email protected]]-->3,1,1316361600000

發佈了30 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章