lucene 3.5 例子

在开始学习lucene时,在网上找到很多的例子,全部是早期的版本,在lucene3.5时,好多方法已不推荐使用,所以就自己写了个例子,方便大家学习

1,创建一个实体类

  1. package com.yutel.lucene;  
  2.   
  3. public class ContactInfo {  
  4.     private int id;  
  5.     private String FirstName;  
  6.     private String LastName;  
  7.     public ContactInfo() {  
  8.     }  
  9.     public ContactInfo(int id, String firstName, String lastName) {  
  10.         this.id = id;  
  11.         FirstName = firstName;  
  12.         LastName = lastName;  
  13.     }  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getFirstName() {  
  21.         return FirstName;  
  22.     }  
  23.     public void setFirstName(String firstName) {  
  24.         FirstName = firstName;  
  25.     }  
  26.     public String getLastName() {  
  27.         return LastName;  
  28.     }  
  29.     public void setLastName(String lastName) {  
  30.         LastName = lastName;  
  31.     }  
  32. }  

2,索引的操作类
  1. package com.yutel.lucene;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.apache.lucene.analysis.Analyzer;  
  9. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  10. import org.apache.lucene.document.Document;  
  11. import org.apache.lucene.document.Field;  
  12. import org.apache.lucene.index.CorruptIndexException;  
  13. import org.apache.lucene.index.IndexReader;  
  14. import org.apache.lucene.index.IndexWriter;  
  15. import org.apache.lucene.index.IndexWriterConfig;  
  16. import org.apache.lucene.index.IndexWriterConfig.OpenMode;  
  17. import org.apache.lucene.queryParser.ParseException;  
  18. import org.apache.lucene.queryParser.QueryParser;  
  19. import org.apache.lucene.search.IndexSearcher;  
  20. import org.apache.lucene.search.Query;  
  21. import org.apache.lucene.search.ScoreDoc;  
  22. import org.apache.lucene.search.TopDocs;  
  23. import org.apache.lucene.store.FSDirectory;  
  24. import org.apache.lucene.util.Version;  
  25.   
  26. public class ContactInfoSearcher {  
  27.     private static final File INDEX_DIR = new File("d:\\temp\\index");  
  28.     private static final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);  
  29.     /** 
  30.      * 在索引库中进行查询 
  31.      * @param fieldName 
  32.      * @param criteria 
  33.      * @return 
  34.      * @throws CorruptIndexException 
  35.      * @throws IOException 
  36.      * @throws ParseException 
  37.      */  
  38.     public List<ContactInfo> search(String fieldName, String criteria)  
  39.             throws CorruptIndexException, IOException, ParseException {  
  40.         IndexReader reader = IndexReader.open(FSDirectory.open(INDEX_DIR));  
  41.         IndexSearcher searcher = new IndexSearcher(reader);  
  42.         List<ContactInfo> list = new ArrayList<ContactInfo>();  
  43.         try {  
  44.             Query query = buildQuery(fieldName, criteria);  
  45.             TopDocs topDocs = searcher.search(query, 10);  
  46.             int total = topDocs.totalHits;  
  47.             System.out.println("total=" + total);  
  48.             ScoreDoc[] scoreDocs = topDocs.scoreDocs;  
  49.             for (int i = 0; i < scoreDocs.length; i++) {  
  50.                 Document doc = searcher.doc(scoreDocs[i].doc);  
  51.                 ContactInfo info = new ContactInfo();  
  52.                 info.setId(Integer.valueOf(doc.get("id")));  
  53.                 info.setFirstName(doc.get("firstName"));  
  54.                 info.setLastName(doc.get("lastName"));  
  55.                 list.add(info);  
  56.             }  
  57.             return list;  
  58.         } finally {  
  59.             searcher.close();  
  60.         }  
  61.     }  
  62.      /** 
  63.       * 将联系方式添加到索引库中 
  64.       * @param list 
  65.       * @throws IOException 
  66.       */  
  67.     public void index(List<ContactInfo> list) throws IOException {  
  68.         IndexWriter writer = openIndexWriter();  
  69.         try {  
  70.             for (ContactInfo contact : list) {  
  71.                 Document document = builderDocument(contact);  
  72.                 writer.addDocument(document);  
  73.             }  
  74.         } finally {  
  75.             writer.close();  
  76.         }  
  77.     }  
  78.   
  79.     private IndexWriter openIndexWriter() throws IOException {  
  80.         IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,  
  81.                 analyzer);  
  82.         iwc.setOpenMode(OpenMode.CREATE);  
  83.         return new IndexWriter(FSDirectory.open(INDEX_DIR), iwc);  
  84.     }  
  85.   
  86.     private Document builderDocument(ContactInfo contact) {  
  87.         Document document = new Document();  
  88.         Field id = new Field("id", String.valueOf(contact.getId()),  
  89.                 Field.Store.YES, Field.Index.ANALYZED);  
  90.         Field firstName = new Field("firstName", contact.getFirstName(),  
  91.                 Field.Store.YES, Field.Index.ANALYZED);  
  92.         Field lastName = new Field("lastName", contact.getLastName(),  
  93.                 Field.Store.YES, Field.Index.ANALYZED);  
  94.         document.add(id);  
  95.         document.add(firstName);  
  96.         document.add(lastName);  
  97.         return document;  
  98.     }  
  99.   
  100.     private Query buildQuery(String fieldName, String criteria)  
  101.             throws ParseException, CorruptIndexException, IOException {  
  102.         QueryParser parser = new QueryParser(Version.LUCENE_35, fieldName,  
  103.                 analyzer);  
  104.         return parser.parse(criteria);  
  105.     }  
  106.   
  107. }  

3,测试类
  1. package com.yutel.lucene;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.lucene.index.CorruptIndexException;  
  8. import org.apache.lucene.queryParser.ParseException;  
  9.   
  10. public class ContactTest {  
  11.   
  12.     public static void main(String[] args) {  
  13.         ContactTest t = new ContactTest();  
  14.         t.init();  
  15.         t.search();  
  16.     }  
  17.   
  18.     public void search() {  
  19.         ContactInfoSearcher cis = new ContactInfoSearcher();  
  20.         try {  
  21.             List<ContactInfo> list = cis.search("lastName""军");  
  22.             if (list.size() > 0) {  
  23.                 System.out.println("找到:" + list.size() + "个结果! ");  
  24.                 for (ContactInfo info : list) {  
  25.                     System.out.println("id=" + info.getId() + ",name="  
  26.                             + info.getFirstName() + info.getLastName());  
  27.                 }  
  28.             } else {  
  29.                 System.out.println("没有找到结果");  
  30.             }  
  31.         } catch (CorruptIndexException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         } catch (ParseException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.   
  40.     public void init() {  
  41.         List<ContactInfo> list = new ArrayList<ContactInfo>();  
  42.         list.add(new ContactInfo(1"孙""定军"));  
  43.         list.add(new ContactInfo(2"袁""亚军"));  
  44.         list.add(new ContactInfo(3"王""明慧"));  
  45.         list.add(new ContactInfo(4"王""磊军"));  
  46.         list.add(new ContactInfo(3"陈""晓稽"));  
  47.         list.add(new ContactInfo(3"乌""红毅"));  
  48.         list.add(new ContactInfo(3"李""建英"));  
  49.         ContactInfoSearcher cis = new ContactInfoSearcher();  
  50.         try {  
  51.             cis.index(list);  
  52.             System.out.println("add complate");  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值