使用lucene實現簡單的全文檢索
阿新 • • 發佈:2019-02-13
<dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>
索引管理
建立索引
public static void createIndex(FileManage file){ //索引儲存路徑 String indexRootPath = getIndexRoot(); File f = new File(indexRootPath); if(!f.exists()){ f.mkdirs(); } String indexPath = indexRootPath+File.separator+File.separator; Directory indexDir = null; IndexWriter indexWriter = null; try{ indexDir = FSDirectory.open(new File(indexPath).toPath()); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); indexWriter = new IndexWriter(indexDir, config); File sourceFile = new File(sourceRootPath); String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8"); Document doc = new Document(); doc.add(new TextField("fileName", file.getPath(), Field.Store.YES)); doc.add(new TextField("content", content, Field.Store.YES)); indexWriter.addDocument(doc); indexWriter.flush(); }catch(Exception e){ log.error("create index error|",e); }finally{ if(null != indexWriter){ try { indexWriter.close(); } catch (IOException e) { log.error("",e); } } } }
刪除索引
public static void deleteIndex(FileManage file){ String indexRootPath = getIndexRoot(); File f = new File(indexRootPath); if(!f.exists()){ f.mkdirs(); } String indexPath = indexRootPath+File.separator; Directory indexDir = null; IndexWriter indexWriter = null; try{ indexDir = FSDirectory.open(new File(indexPath).toPath()); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); indexWriter = new IndexWriter(indexDir, config); indexWriter.deleteDocuments(new Term("fileName", file.getFileName())); indexWriter.flush(); }catch(Exception e){ log.error("create index error|",e); }finally{ if(null != indexWriter){ try { indexWriter.close(); } catch (IOException e) { log.error("",e); } } } }
更新索引
public static void updateIndex(FileManage file){
String indexRootPath = getIndexRoot();
String sourceRootPath = getSourceRoot();
File f = new File(indexRootPath);
if(!f.exists()){
f.mkdirs();
}
String indexPath = indexRootPath+File.separator;
Directory indexDir = null;
IndexWriter indexWriter = null;
try{
indexDir = FSDirectory.open(new File(indexPath).toPath());
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(indexDir, config);
File sourceFile = new File(file.getPath());
String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8");
Document doc = new Document();
doc.add(new TextField("fileName", file.getFileName(), Field.Store.YES));
doc.add(new TextField("content", content, Field.Store.YES));
indexWriter.updateDocument(new Term("fileName", file.getFileName()),doc);
indexWriter.flush();
}catch(Exception e){
log.error("create index error|",e);
}finally{
if(null != indexWriter){
try {
indexWriter.close();
} catch (IOException e) {
log.error("",e);
}
}
}
}
根據索引檢索
public static List<SearchResult> search(List<FileManage> sourcefiles, String param){
if(XaUtil.isEmpty(sourcefiles)){
return Collections.EMPTY_LIST;
}
List<SearchResult> result = new ArrayList<SearchResult>();
String indexRootPath = getIndexRoot();
File f = new File(indexRootPath);
if(!f.exists()){
f.mkdirs();
}
Directory indexDir = null;
DirectoryReader ireader = null;
try{
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
String[] fieldStr = new String[]{"fileName", "content"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST};
String indexPath = indexRootPath+File.separator;
indexDir = FSDirectory.open(new File(indexPath).toPath());
for(FileManage file : sourcefiles){
ireader = DirectoryReader.open(indexDir);
IndexSearcher isearcher = new IndexSearcher(ireader);
String[] queryAry = new String[]{file.getUuidName(), param};
Query query = MultiFieldQueryParser.parse(queryAry,fieldStr,flags,analyzer);
ScoreDoc[] hits = isearcher.search(query,1000).scoreDocs;
Document doc =null;
for(ScoreDoc hit : hits){
doc = isearcher.doc(hit.doc);
SearchResult sr = new SearchResult();
sr.setScore(hit.score);
sr.setFileName(doc.get("fileName"));
result.add(sr);
}
}
}catch (Exception e){
log.error("",e);
}finally {
if(null != indexDir){
try {
indexDir.close();
} catch (IOException e) {
log.error("",e);
}
}
if(null != ireader){
try {
ireader.close();
} catch (IOException e) {
log.error("",e);
}
}
return result;
}
}
(完)