-
前提:在使用lucene进行搜索的时候,必须先生成索引文件,即必须先进行上一章节的案例,生成索引文件如下:
- 该索引文件为"segments"开头,如果没有该文件则说明没有索引文件则报错:org.apache.lucene.index.IndexNotFoundException: no segments* file found in SimpleFSDirectory@E:luceneindex lockFactory=org.apache.lucene.store.NativeFSLockFactory@87aac27: files: [_0.cfe, _0.cfs, _0.si, write.lock]
-
搜索
package com.shyroke.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.StandardDirectoryReader; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.SimpleFSDirectory; public class Search { /** * 根据关键字检索文件 * * @param indexDir * 存放索引的目录 * @param key * 关键字 * @throws IOException * @throws ParseException */ public static void search(String indexDir, String key) throws IOException, ParseException { Directory directory = new SimpleFSDirectory(Paths.get(indexDir)); IndexReader reader = DirectoryReader.open(directory); Analyzer analyzer=new StandardAnalyzer(); QueryParser queryParser=new QueryParser("fileContents", analyzer); Query query=queryParser.parse(key); IndexSearcher searcher = new IndexSearcher(reader); long startTime=System.currentTimeMillis(); TopDocs topDocs=searcher.search(query, 10); long endTime=System.currentTimeMillis(); System.out.println("匹配 "+key+" 总共花费:"+(endTime-startTime)+"毫秒,查询到"+topDocs.totalHits+"条记录"); for(ScoreDoc scoreDoc:topDocs.scoreDocs) { Document document=searcher.doc(scoreDoc.doc); System.out.println(document.get("filePath")); } } public static void main(String[] args) { String indexDir="E:\lucene\index"; String key="Zygmunt#Saloni"; try { Search.search(indexDir, key); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }
结果:
- 此时的关键字是“Zygmunt#Saloni” ,查询结果是在LICENSE.txt中,但是该文件中并没有这个内容,但是有
这样也会匹配到,这是分词器StandardAnalyzer在起作用。