标签:store char buffered pac comm 如何 read stream directory
Lucene版本:7.1
使用Lucene的关键点
- 创建文档(Document),添加文件(Field);
- 把文档加入IndexWriter;
- 使用QueryParser.parse()构建查询内容;
- 使用IndexSearcher的search()方法,进行查询;
一、创建索引基本流程
//open a Directory,存放索引文件
//FSDirectory指的是存放到文件夹,还可以存放到缓存RAMDirectory
//indexPath:文件路径
Directory dir = FSDirectory.open(Paths.get(indexPath));
//instantiate Analyzer,处理文本文件
//StandardAnalyzer使用了Unicode文本分割算法,把符号转成小写,过滤出常用语
//不同语言需要使用不同的Analyzer,详见:https://lucene.apache.org/core/7_1_0/analyzers-common/overview-summary.html
Analyzer analyzer = new StandardAnalyzer();
//索引配置内容
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
//CREATE,APPEND,CREATE_OR_APPEND
iwc.setOpenMode(OpenMode.CREATE);
//instantiate IndexWriter
IndexWriter writer = new IndexWriter(dir, iwc);
//instantiate Document,表示文件的文本内容及创建时间和位置信息等
Document doc = new Document();
//"path":索引字段
doc.add(new StringField("path", file.toString(), Field.Store.YES));
//doc.add(new LongPoint("modified", lastModified));
//doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
//添加到IndexWriter
writer.addDocument(doc);
//关闭
writer.close();
二、搜索基本流程
IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
//索引字段
QueryParser parser = new QueryParser("contents", analyzer);
//查询结果
Query query = parser.parse("123456");
TopDocs results = searcher.search(query, 5 * hitsPerPage);
ScoreDoc[] hits = results.scoreDocs;
标签:store char buffered pac comm 如何 read stream directory
原文地址:http://www.cnblogs.com/bigshark/p/7899147.html