jar包:
Lucene包:
lucene-core-4.10.3.jar
lucene-analyzers-common-4.10.3.jar
lucene-queryparser-4.10.3.jar
其它:
commons-io-2.4.jar
junit-4.9.jar
package com.itheima.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
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.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
/**
* Lucene入门
* 创建索引
* 查询索引
* @author mjl
*
*/
public class FirstLucene {
/**
* @throws IOException
*
*/
@Test
public void testIndex() throws IOException{
// 第一步:创建一个java工程,并导入jar包。
// 第二步:创建一个indexwriter对象。open(new File("D:\\temp\\index"));
// 1)指定索引库的存放位置Directory对象
// 2)指定一个分析器,对文档内容进行分析
Directory directory = FSDirectory.open(new File("D:\\lucenesolr\\temp"));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
IndexWriter indexWriter = new IndexWriter(directory,config);
// 第三步:创建document对象。
Document document = new Document();
// 第四步:创建field对象,将field添加到document对象中。
File f = new File("D:\\lucenesolr\\searchsource");
File[] listFiles = f.listFiles();
for (File file : listFiles) {
//文件名称
String file_name = file.getName();
Field fileNameField = new TextField("fileName", file_name, Store.YES);
//文件大小
long file_size = FileUtils.sizeOf(file);
Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
//文件路径
String file_path = file.getPath();
Field filePathField = new StoredField("filePath", file_path);
//文件内容
String file_content = FileUtils.readFileToString(file);
Field fileContentField = new TextField("fileContent", file_content, Store.YES);
document.add(fileNameField);
document.add(fileSizeField);
document.add(filePathField);
document.add(fileContentField);
// 第五步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
indexWriter.addDocument(document);
}
// 第六步:关闭IndexWriter对象。
indexWriter.close();
}
}
原文:https://www.cnblogs.com/syj1993/p/10010374.html