package cn.lucene.lesson1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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.Store;
import org.apache.lucene.document.StringField;
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.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
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;
/**
* Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。
*
*/
public class TxtFileIndex {
/*索引目录*/
private final static String INDEX_PATH = "d:/lucene/index";
/*文件目录*/
private final static String DATA_PATH = "d:/lucene/data";
public static void main(String[] args) throws Exception{
//创建索引
//createTxtIndexs();
//搜索索引
searchTxtIndexs();
}
private static void searchTxtIndexs() throws IOException {
File indexDir = new File(INDEX_PATH);
//索引位置
Directory directory = FSDirectory.open(indexDir);
//索引读取
IndexReader reader = DirectoryReader.open(directory);
//索引搜索
IndexSearcher searcher = new IndexSearcher(reader);
//定义关键字
Term term = new Term("contents", "he");
//关键字搜索
TermQuery query = new TermQuery(term);
//
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("查询结果总数---" + topDocs.totalHits + ", 最大的评分--" + topDocs.getMaxScore());
for (int i = 0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.println("contents====" + document.get("contents"));
System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--" + scoreDocs[i].shardIndex);
}
reader.close();
}
/**
* 创建索引
* @throws IOException
* @throws FileNotFoundException
*/
private static void createTxtIndexs() throws IOException, FileNotFoundException {
File indexDir = new File(INDEX_PATH);
File dataDir = new File(DATA_PATH);
//标准分析器
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_43);
//获得文件目录
File[] dataFiles = dataDir.listFiles();
//索引的存储的位置
Directory directory = FSDirectory.open(indexDir);
//设置了存放索引的文件夹将以覆盖或者新建的方式建立
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_43, luceneAnalyzer);
iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
//创建索引, 作用是把一个个的 Document 对象加到索引中来
IndexWriter indexWriter = new IndexWriter(directory, iwConfig);
long startTime = System.currentTimeMillis();
//遍历文件
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();
BufferedReader reader = new BufferedReader(new FileReader(dataFiles[i]));
document.add(new StringField("id", dataFiles[i].getCanonicalPath(), Store.YES));
//document.add(new TextField("contents", reader));//此构造方法不进行存储,需要将reader内容读取出来
String line = reader.readLine();
StringBuffer sbf = new StringBuffer();
while(line != null){
sbf.append(line);
line = reader.readLine();
}
document.add(new TextField("contents", sbf.toString(), Store.YES));
reader.close();
indexWriter.addDocument(document);
}
}
System.out.println("indexs created!");
indexWriter.close();
long endTime = System.currentTimeMillis();
System.out.println("创建索引耗时: " + (endTime - startTime) + " milliseconds. " + dataDir.getPath());
}
}
最近下载更多
最近浏览更多
szf123 LV12
2023年12月10日
微信网友_6040315240812544 LV8
2022年10月20日
19895630790 LV1
2022年7月1日
chenhongjie LV5
2021年10月21日
Altria LV5
2021年8月13日
kilord LV1
2021年5月31日
ronnie0507 LV8
2021年5月17日
lodddy LV6
2021年5月4日
3270302262
2021年3月27日
暂无贡献等级
caozhaoqi83 LV5
2021年1月21日

