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 {
	
	/**
	 * 添加索引
	 * 

Title: addIndex

*

Description:

* @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(); } }

 

你可能感兴趣的:(javaWeb)