lucene5.0建立索引并进行查找

说白了就是两个函数一个建立索引(写),另一个来查找(读),所以涉及到java IO的一些知识。

import java.io.*; 
import java.nio.file.Paths;
import java.util.Date; 
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.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*; 
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/** 
* This class demonstrate the process of creating index with Lucene 
* for text files 
*/ 
public class TxtFileIndexer { 
     public static void main(String[] args) throws Exception{ 
     //indexDir is the directory that hosts Lucene's index files 
     Directory indexDir = FSDirectory.open(Paths.get("G:\\luceneout"));
     //dataDir is the directory that hosts the text files that to be indexed 
     File   dataDir  = new File("G:\\downloads\\LJParser_release\\LJParser_Packet\\训练分类用文本\\交通"); 
     Analyzer luceneAnalyzer = new StandardAnalyzer(); //新建一个分词器实例
     IndexWriterConfig config = new IndexWriterConfig(luceneAnalyzer);
     File[] dataFiles  = dataDir.listFiles(); //所有训练样本文件
     IndexWriter indexWriter = new IndexWriter(indexDir,config);//构造一个索引写入器 
     long startTime = new Date().getTime(); 
     for(int i = 0; i < dataFiles.length; i++){ 
          if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){
               System.out.println("Indexing file " + dataFiles[i].getCanonicalPath()); //返回绝对路径
               Document document = new Document();//每一个文件都变成一个document对象 
               Reader txtReader = new FileReader(dataFiles[i]); 
               Field field1 = new StringField("path",dataFiles[i].getPath(),Store.YES);
               Field field2 = new TextField("content",txtReader);
               Field field3 = new LongField("fileSize", dataFiles[i].length(), Store.YES); 
               Field field4 = new TextField("filename",dataFiles[i].getName(),Store.YES);
               document.add(field1);
               document.add(field2);
               document.add(field3);
               document.add(field4);
               indexWriter.addDocument(document); //写进一个索引
          } 
     } 
     //indexWriter.optimize(); 
     indexWriter.close(); 
     long endTime = new Date().getTime(); 
        
     System.out.println("It takes " + (endTime - startTime) 
         + " milliseconds to create index for the files in directory "
         + dataDir.getPath());        
     } 
}

读取索引并查找

import java.io.File; 
import java.nio.file.Paths;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document; 
import org.apache.lucene.index.DirectoryReader;  
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*; 
import org.apache.lucene.store.*;
 /** 
 * This class is used to demonstrate the 
 * process of searching on an existing 
 * Lucene index 
 * 
 */ 
 public class TxtFileSearcher { 
	 public static void main(String[] args) throws Exception{ 
		 //存储了索引文件
		 Directory indexDir = FSDirectory.open(Paths.get("G:\\luceneout"));
		 //读取器读取索引文件
		 DirectoryReader ireader = DirectoryReader.open(indexDir);
		 //查找
		 IndexSearcher searcher = new IndexSearcher(ireader);
		 //目的查找字符串
		 String queryStr = "大数据挖掘";
		 //构造一个词法分析器,并将查询结果返回到一个队列
		 QueryParser parser = new QueryParser("content",new StandardAnalyzer());
		 Query query = parser.parse(queryStr);
		 TopDocs docs = searcher.search(query, 100);
		 System.out.print("一共搜索到结果:"+docs.totalHits+"条");
		 //输出查询结果信息
		 for(ScoreDoc scoreDoc:docs.scoreDocs){
			 System.out.print("序号为:"+scoreDoc.doc);
			 System.out.print("评分为:"+scoreDoc.score);
			 Document document = searcher.doc(scoreDoc.doc);
			 System.out.print("路径为:"+document.get("path"));
			 System.out.print("内容为"+document.get("content"));
			 System.out.print("文件大小为"+document.get("fileSize"));
			 System.out.print("文件名为"+document.get("filename"));
			 System.out.println();
		 }	 
	 } 
 }

运行结果

下面是文件目录

两个函数都需要用到分词器,前者是为了配置写入,后者则是为了配置词法分析器来查找

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值