zoukankan      html  css  js  c++  java
  • 第一个LUCENE程序

    1. package com.fpi.lucene.studying.myfirstlucene;  
    2. import java.io.File;  
    3. import java.io.FileReader;  
    4. import java.io.IOException;  
    5. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
    6. import org.apache.lucene.document.Document;  
    7. import org.apache.lucene.document.Field;  
    8. import org.apache.lucene.index.CorruptIndexException;  
    9. import org.apache.lucene.index.IndexWriter;  
    10. import org.apache.lucene.store.FSDirectory;  
    11. import org.apache.lucene.store.LockObtainFailedException;  
    12. import org.apache.lucene.util.Version;  
    13. public class Indexer {  
    14.       
    15.     public static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException{  
    16.           
    17.         Document doc = null;  
    18.           
    19.         //创建索引。定义索引生成位置  
    20.         IndexWriter writer = new IndexWriter(FSDirectory.open(new File("d://test//myindex")),  
    21.                 new StandardAnalyzer(Version.LUCENE_30),  
    22.                 true,  
    23.                 IndexWriter.MaxFieldLength.LIMITED);  
    24.         File dir = new File("d://test//mysmall");  
    25.         File[] fileList = dir.listFiles();  
    26.         for (int i = 0; i < fileList.length; i++) {  
    27.             //对于每一个要查找的文件都要新建一个document,理解为一个文件对应着一个document  
    28.             doc = new Document();  
    29.             //前边的contents随便起 只是一个代号而已。后边的代表把这篇文章做成索引,以供后续查找...  
    30.             doc.add(new Field("contents",new FileReader(fileList[i])));  
    31.             doc.add(new Field("filename",fileList[i].getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));  
    32.               
    33.             //将文档写入索引  
    34.             writer.addDocument(doc);  
    35.            
    36.         }     
    37.           
    38.     
    39.         // .optimize() Requests an "optimize" operation on an index, priming the index for the fastest available search.  
    40.         // 请求一个“optimize”上的索引操作,启动了最快的搜索索引。  
    41.         writer.optimize();  
    42.           
    43.         writer.close();  
    44.     }  
    45.       
    46.     public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException {  
    47.         createIndex();  
    48.     }  
    49. }  

    建立索引。。。。^

    [java] view plaincopy
     
    1. package com.fpi.lucene.studying.myfirstlucene;  
    2. import java.io.File;  
    3. import java.io.IOException;  
    4. import org.apache.lucene.analysis.Analyzer;  
    5. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
    6. import org.apache.lucene.document.Document;  
    7. import org.apache.lucene.index.CorruptIndexException;  
    8. import org.apache.lucene.queryParser.ParseException;  
    9. import org.apache.lucene.queryParser.QueryParser;  
    10. import org.apache.lucene.search.IndexSearcher;  
    11. import org.apache.lucene.search.Query;  
    12. import org.apache.lucene.search.ScoreDoc;  
    13. import org.apache.lucene.search.TopScoreDocCollector;  
    14. import org.apache.lucene.store.FSDirectory;  
    15. import org.apache.lucene.util.Version;  
    16. public class Searcher {  
    17.     // 关键字,要搜查的对象  
    18.     public static String key_word = "all";  
    19.       
    20.     public static String field = "contents";  
    21.     public static void search() throws CorruptIndexException, IOException, ParseException{  
    22.         //打开索引所在地  
    23.         IndexSearcher sr = new IndexSearcher(FSDirectory.open(new File("d://test//myindex")),true);  
    24.           
    25.         //词法分析器  
    26.         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  
    27.           
    28.         //解析器  
    29.         QueryParser parser = new QueryParser(Version.LUCENE_30,field,analyzer);  
    30.           
    31.         //根据关键字查询  
    32.         Query query = parser.parse(key_word);  
    33.           
    34.         TopScoreDocCollector collector = TopScoreDocCollector.create(100false);  
    35.           
    36.         //将所搜寻出来的结果以特定的形式放在collector中  
    37.         sr.search(query, collector);  
    38.         /** 
    39.          * topDocs():Returns the top docs that were collected by this collector.     
    40.          *            返回的是由这个collector收集的顶级文档。 
    41.          * .scoreDocs():The top hits for the query.  
    42.          *              用于查询的最高命中。 
    43.          */  
    44.         ScoreDoc[] hits = collector.topDocs().scoreDocs;  
    45.         System.out.println("搜索到符合标准的文档数目:"+hits.length);  
    46.           
    47.         for (int i = 0; i < hits.length; i++) {  
    48.            
    49.             Document doc = sr.doc(hits[i].doc);      //依此遍历符合标准的文档  
    50.             System.out.println(doc.getField("filename")+" ----  "+hits[i].toString()+" ---- ");  
    51.         }  
    52.         System.out.println("you have " + collector.getTotalHits() +  
    53.                   " file matched query '" + key_word + "':");  
    54.     }  
    55.       
    56.       
    57.     public static void main(String[] args) {  
    58.         try {  
    59.             search();  
    60.         } catch (CorruptIndexException e) {  
    61.             // TODO Auto-generated catch block  
    62.             e.printStackTrace();  
    63.         } catch (IOException e) {  
    64.             // TODO Auto-generated catch block  
    65.             e.printStackTrace();  
    66.         } catch (ParseException e) {  
    67.             // TODO Auto-generated catch block  
    68.             e.printStackTrace();  
    69.         }  
    70.     }  
    71. }  

    查找部分。^  

  • 相关阅读:
    ssh框架整合之登录以及增删改查
    jsp+servlet实现模糊查询和分页效果
    面向对象高级部分
    个人第二次作业:熟悉使用工具
    个人第1次作业:准备与阅读
    如何计算团队成员贡献分
    Jira软件使用体验报告
    软件测试第四组第一周作业第三天
    软件测试第四组第一周作业第二天
    软件测试第四组第一周作业第一天
  • 原文地址:https://www.cnblogs.com/zwb7926/p/3115562.html
Copyright © 2011-2022 走看看