在开始学习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();
- }
- }
- }