Lucene学习(1)

来源:互联网 发布:淘宝客做什么产品好 编辑:程序博客网 时间:2024/06/05 04:15

前段时间研究一段时间Lucene,后来因为工作忙,放下了,现在工作闲下来了,重新拿起,发现在Lucene新的版本里变化了不少,现在Lucene的版本是2.4.0,如果不清楚Lucene是什么,就google一下吧。闲话不说,先写个小程序看看。

 

  1. package lucene.study.test;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.Analyzer;
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.document.Field;
  7. import org.apache.lucene.index.CorruptIndexException;
  8. import org.apache.lucene.index.IndexWriter;
  9. import org.apache.lucene.index.IndexWriter.MaxFieldLength;
  10. import org.apache.lucene.queryParser.ParseException;
  11. import org.apache.lucene.queryParser.QueryParser;
  12. import org.apache.lucene.search.IndexSearcher;
  13. import org.apache.lucene.search.Query;
  14. import org.apache.lucene.search.ScoreDoc;
  15. import org.apache.lucene.search.TopDocs;
  16. import org.apache.lucene.store.Directory;
  17. import org.apache.lucene.store.LockObtainFailedException;
  18. import org.apache.lucene.store.RAMDirectory;
  19. public class IndexAndSearch {
  20.     
  21.     public static void main(String[] args) {
  22.         Analyzer analyzer = new StandardAnalyzer();
  23.         
  24.         //在内存中建立索引
  25.         //也可以使用将索引存储在硬盘上
  26.         //Directory directory = FSDirectory.getDirectory(indexDir);
  27.         Directory directory = new RAMDirectory(); 
  28.         try {
  29.             IndexWriter iwriter = new IndexWriter(directory,analyzer,true,new MaxFieldLength(2500));
  30.             
  31.             Document doc = new Document();
  32.             //要被索引的字符串
  33.             String text = "good good study day day up.";
  34.             doc.add(new Field("testIndex",text,Field.Store.YES,Field.Index.ANALYZED));
  35.             iwriter.addDocument(doc);
  36.             iwriter.optimize();
  37.             iwriter.close();
  38.         } catch (CorruptIndexException e) {
  39.             e.printStackTrace();
  40.         } catch (LockObtainFailedException e) {
  41.             e.printStackTrace();
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.         
  46.         //通过索引进行搜索
  47.         try {
  48.             IndexSearcher isearcher = new IndexSearcher(directory);
  49.             QueryParser parser = new QueryParser("testIndex",analyzer);
  50.             Query query = parser.parse("good");
  51.              //在Lucene现在的版本里,原来的返回值为Hits的search方法都置为不推荐方法了
  52.              //而Hits也被TopDocCollector和TopDocs取代了
  53.              //这里的2指的是查询结果中的前两个
  54.             TopDocs tdocs = isearcher.search(query,2);
  55.             ScoreDoc[] sdocs = tdocs.scoreDocs;
  56.             for(int i = 0;i < sdocs.length;i++ ) {
  57.                 ScoreDoc sdoc = sdocs[i];
  58.                 Document document = isearcher.doc(sdoc.doc);
  59.                 System.out.println(document.get("testIndex"));
  60.             }
  61.         } catch (CorruptIndexException e) {
  62.             e.printStackTrace();
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         } catch (ParseException e) {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69. }

 

以上是一个简单的索引与查询,建立起初步对Lucene的认识,接下来会体验Lucene强大的索引与查询功能

原创粉丝点击