Lucene的简单使用

Lucene的简单使用:在进行检索的时候是需要原来已经创建了索引才能检索到需要的内容的。所以在使用Lucene的时候大体就是两个方面,建立索引和检索,其次就是一些不能的策略了!

 

 

1.建立索引

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.IOException;  
  4.   
  5. import org.apache.lucene.analysis.Analyzer;  
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  7. import org.apache.lucene.document.Document;  
  8. import org.apache.lucene.document.Field;  
  9. import org.apache.lucene.index.IndexWriter;  
  10. import org.apache.lucene.index.IndexWriterConfig;  
  11. import org.apache.lucene.store.Directory;  
  12. import org.apache.lucene.store.FSDirectory;  
  13. import org.apache.lucene.util.Version;  
  14.   
  15. public class Writer {  
  16.       
  17.     public static void main(String args[]) throws IOException {  
  18.         String indexDir = "E:\\index\\test";//索引存放的路径  
  19.         String dataDir = "D:\\Backup\\Note";//这个是用来建立索引的数据源,此处是以一个一个的文件为例来建立索引的  
  20.         Directory dir = FSDirectory.open(new File(indexDir));  
  21.         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);//建立一个对应版本的分析器  
  22.         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_31, analyzer);//这是用于在建立索引时配置信息的,默认是默认信息  
  23.         IndexWriter indexWriter = new IndexWriter(dir, config);  
  24.         File files[] = new File(dataDir).listFiles();  
  25.         for (File file:files) {  
  26.             Document doc = new Document();  
  27.             Field field1 = new Field("fileName",file.getName(), Field.Store.YES, Field.Index.ANALYZED);  
  28.             FileInputStream fis = new FileInputStream(file);  
  29.             int len = fis.available();  
  30.             byte[] bytes = new byte[len];  
  31.             fis.read(bytes);  
  32.             fis.close();  
  33.             String content = new String(bytes);  
  34.             Field field2 = new Field("content", content, Field.Store.YES, Field.Index.ANALYZED);  
  35.             doc.add(field1);  
  36.             doc.add(field2);  
  37. //现在流行的检索工具都是把每一个信息源看作一个Document来进行处理的  
  38.             indexWriter.addDocument(doc);  
  39.             indexWriter.optimize();  
  40.         }  
  41.         int docs = indexWriter.numDocs();  
  42.         System.out.println("共索引了"+docs+"个文件!");  
  43.         indexWriter.close();  
  44.     }  
  45.       
  46. }  

 

 

2.进行检索

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  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.queryParser.MultiFieldQueryParser;  
  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.TopDocs;  
  14. import org.apache.lucene.search.TopScoreDocCollector;  
  15. import org.apache.lucene.store.Directory;  
  16. import org.apache.lucene.store.FSDirectory;  
  17. import org.apache.lucene.util.Version;  
  18.   
  19. public class Searcher {  
  20.   
  21.     public static void main(String args[]) throws IOException, ParseException {  
  22.           
  23.         String indexDir = "E:\\index\\test";  
  24.         Directory dir = FSDirectory.open(new File(indexDir));  
  25.         IndexSearcher indexSearcher = new IndexSearcher(dir);  
  26.         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);  
  27. //      QueryParser queryParser = new QueryParser(Version.LUCENE_31, "content", analyzer);  
  28.         QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_31, new String[] {"fileName","content"}, analyzer);  
  29.         Query query = queryParser.parse("内 的 值");  
  30.         TopScoreDocCollector collector = TopScoreDocCollector.create(3true);//前面一个参数表示一次最多检索多少,这里检索的是永远从第一条开始  
  31.         indexSearcher.search(query, collector);  
  32.         TopDocs topDocs = collector.topDocs(22);//第一个参数为从第多少个记录开始,第二个参数为每次取多少条记录  
  33. //      TopDocs topDocs = indexSearcher.search(query, 10);  
  34.         int totalHits = topDocs.totalHits;  
  35.         System.out.println("totalHits:"+totalHits);  
  36.         System.out.println();  
  37.         ScoreDoc scoreDocs[] = topDocs.scoreDocs;  
  38.         for (ScoreDoc scoreDoc:scoreDocs) {  
  39.             float score = scoreDoc.score;  
  40.             Document doc = indexSearcher.doc(scoreDoc.doc);  
  41.             System.out.println("fileName:"+doc.get("fileName"));  
  42. //          System.out.println("content:"+doc.get("content"));  
  43.             System.out.println("score:"+score);  
  44.             System.out.println();  
  45.         }  
  46.         indexSearcher.close();  
  47.           
  48.     }  
  49.       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值