在開始學習lucene時,在網上找到很多的例子,全部是早期的版本,在lucene3.5時,好多方法已不推薦使用,所以就自己寫了個例子,方便大家學習
1,創建一個實體類
package com.yutel.lucene;
public class ContactInfo {
private int id;
private String FirstName;
private String LastName;
public ContactInfo() {
}
public ContactInfo(int id, String firstName, String lastName) {
this.id = id;
FirstName = firstName;
LastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
}
2,索引的操作類
package com.yutel.lucene;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class ContactInfoSearcher {
private static final File INDEX_DIR = new File("d:\\temp\\index");
private static final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
/**
* 在索引庫中進行查詢
* @param fieldName
* @param criteria
* @return
* @throws CorruptIndexException
* @throws IOException
* @throws ParseException
*/
public List<ContactInfo> search(String fieldName, String criteria)
throws CorruptIndexException, IOException, ParseException {
IndexReader reader = IndexReader.open(FSDirectory.open(INDEX_DIR));
IndexSearcher searcher = new IndexSearcher(reader);
List<ContactInfo> list = new ArrayList<ContactInfo>();
try {
Query query = buildQuery(fieldName, criteria);
TopDocs topDocs = searcher.search(query, 10);
int total = topDocs.totalHits;
System.out.println("total=" + total);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < scoreDocs.length; i++) {
Document doc = searcher.doc(scoreDocs[i].doc);
ContactInfo info = new ContactInfo();
info.setId(Integer.valueOf(doc.get("id")));
info.setFirstName(doc.get("firstName"));
info.setLastName(doc.get("lastName"));
list.add(info);
}
return list;
} finally {
searcher.close();
}
}
/**
* 將聯繫方式添加到索引庫中
* @param list
* @throws IOException
*/
public void index(List<ContactInfo> list) throws IOException {
IndexWriter writer = openIndexWriter();
try {
for (ContactInfo contact : list) {
Document document = builderDocument(contact);
writer.addDocument(document);
}
} finally {
writer.close();
}
}
private IndexWriter openIndexWriter() throws IOException {
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
analyzer);
iwc.setOpenMode(OpenMode.CREATE);
return new IndexWriter(FSDirectory.open(INDEX_DIR), iwc);
}
private Document builderDocument(ContactInfo contact) {
Document document = new Document();
Field id = new Field("id", String.valueOf(contact.getId()),
Field.Store.YES, Field.Index.ANALYZED);
Field firstName = new Field("firstName", contact.getFirstName(),
Field.Store.YES, Field.Index.ANALYZED);
Field lastName = new Field("lastName", contact.getLastName(),
Field.Store.YES, Field.Index.ANALYZED);
document.add(id);
document.add(firstName);
document.add(lastName);
return document;
}
private Query buildQuery(String fieldName, String criteria)
throws ParseException, CorruptIndexException, IOException {
QueryParser parser = new QueryParser(Version.LUCENE_35, fieldName,
analyzer);
return parser.parse(criteria);
}
}
3,測試類
package com.yutel.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
public class ContactTest {
public static void main(String[] args) {
ContactTest t = new ContactTest();
t.init();
t.search();
}
public void search() {
ContactInfoSearcher cis = new ContactInfoSearcher();
try {
List<ContactInfo> list = cis.search("lastName", "軍");
if (list.size() > 0) {
System.out.println("找到:" + list.size() + "個結果! ");
for (ContactInfo info : list) {
System.out.println("id=" + info.getId() + ",name="
+ info.getFirstName() + info.getLastName());
}
} else {
System.out.println("沒有找到結果");
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
public void init() {
List<ContactInfo> list = new ArrayList<ContactInfo>();
list.add(new ContactInfo(1, "孫", "定軍"));
list.add(new ContactInfo(2, "袁", "亞軍"));
list.add(new ContactInfo(3, "王", "明慧"));
list.add(new ContactInfo(4, "王", "磊軍"));
list.add(new ContactInfo(3, "陳", "曉稽"));
list.add(new ContactInfo(3, "烏", "紅毅"));
list.add(new ContactInfo(3, "李", "建英"));
ContactInfoSearcher cis = new ContactInfoSearcher();
try {
cis.index(list);
System.out.println("add complate");
} catch (IOException e) {
e.printStackTrace();
}
}
}