Lunence的一个helloworld

package com.bjfu.lunence.test;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
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.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class TestLunence {
	
	/**
	 * 添加索引
	 * <p>Title: addIndex</p>  
	 * <p>Description: </p>  
	 * @author TianYuXiao  
	 * @date 2018年11月11日
	 */
	@Test
	public void addIndex() throws Exception {
		//指定索引存放的位置
		Directory directory = FSDirectory.open(new File("F:\\index"));
		
		//指定分词器
		Analyzer analyzer = new IKAnalyzer();
		//获取indexWriterConfig
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
		//获取indexWriter
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
		
		//获取文件夹
		File searchDir = new File("F:\\searchsource");
		//获取文件对象数组
		File[] files = searchDir.listFiles();
		for (File file : files) {
			//文件名称
			String fileName = file.getName();
			//文件内容
			String fileContent = FileUtils.readFileToString(file);
			//文件大小
			long fileSize = FileUtils.sizeOf(file);
			//文件路径
			String filePath = file.getPath();
			
			//添加Field
			Field fileNameField = new TextField("fileNameField", fileName,Store.YES);
			Field fileContentField = new TextField("fileContentField", fileContent, Store.YES);
			Field fileSizeField = new LongField("fileSizeField", fileSize, Store.YES);
			Field filePathField = new StoredField("filePathField", filePath);
			
			//将field添加到document中
			Document document = new Document();
			document.add(fileNameField);
			document.add(fileContentField);
			document.add(fileSizeField);
			document.add(filePathField);
			
			//将document添加到indexWriter中
			indexWriter.addDocument(document);
		}
		//关流
		indexWriter.close();
	}
	
	@Test
	public void readIndex() throws Exception {
		//获取索引路径
		Directory directory = FSDirectory.open(new File("F:\\index"));
		
		
		//获取indexReader
		IndexReader indexReader = DirectoryReader.open(directory);
		//创建indexSearcher
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		//创建Query对象
		Query query = new TermQuery(new Term("fileContentField", "apache"));
		//执行查询
		TopDocs topDocs = indexSearcher.search(query, 20);
		//topDocs.scoreDocs存储了document对象的id
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc scoreDoc : scoreDocs) {
			//document的id
			int id = scoreDoc.doc;
			//根据id查询document
			Document document = indexSearcher.doc(id);
			//获取文件信息
			String fileNameField = document.get("fileNameField");
			String filePathField = document.get("filePathField");
			
			System.out.println(filePathField + "  :  " + fileNameField);
		}
		//关流
		indexReader.close();
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值