lucene 添删改查代码

时间:2019-11-20
本文章向大家介绍lucene 添删改查代码,主要包括lucene 添删改查代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package com.xl.lucene;

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.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneManager {

public IndexWriter getIndexWriter() throws Exception {

Directory directory = FSDirectory.open(new File("E:\\WorkSpace\\allOtherItem\\luceneresult"));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
return new IndexWriter(directory, config);
}

// 全删除
@Test
public void testAllDelete() throws Exception {
IndexWriter indexWriter = getIndexWriter();
indexWriter.deleteAll();
indexWriter.close();
}

// 根据条件
@Test
public void testDelete() throws Exception {
IndexWriter indexWriter = getIndexWriter();
Query query = new TermQuery(new Term("fileName", "java.txt"));
indexWriter.deleteDocuments(query);
indexWriter.close();
}

// 修改
@Test
public void testUpdate() throws Exception {
IndexWriter indexWriter = getIndexWriter();
Document document = new Document();
document.add(new TextField("fileN", "测试文件名", Field.Store.YES));
document.add(new TextField("fileN", "测试文件内容", Field.Store.NO));
indexWriter.updateDocument(new Term("fileName", "java.txt"), document, new IKAnalyzer());
indexWriter.close();
}

// indexReader IndexSearcher
public IndexSearcher getIndexSearcher() throws IOException {
//查询所有
Directory directory = FSDirectory.open(new File("E:\\WorkSpace\\allOtherItem\\luceneresult"));
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
return new IndexSearcher(indexReader);
}

// 执行查询结果
public void printResult(IndexSearcher indexSearcher,Query query) throws Exception {
TopDocs topDocs = indexSearcher.search(query, 10);

// 第六步返回查询结果.遍历查询结果并输出
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
if (scoreDocs == null) {
System.out.println("读取失败");
} else {
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
//文件名称
String fileName = document.get("fileName");
//文件内容
String fileContent = document.get("fileContent");
//文件大小
String fileSize = document.get("fileSize");
//文件路径
String filePath = document.get("filePath");
System.out.println(fileName);
System.out.println(fileContent);
System.out.println(fileSize);
System.out.println(filePath);
System.out.println("------------#----------------------------------------------");

}
}
}

// 查询所有
@Test
public void testMatchAllDocsQuery() throws Exception {
IndexSearcher indexSearcher = getIndexSearcher();
Query query = new MatchAllDocsQuery();
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}

// 根据数值范围查询
@Test
public void testNumbericRangeQuery()throws Exception{
IndexSearcher indexSearcher = getIndexSearcher();
Query query = NumericRangeQuery.newLongRange("fileSize",0L,200L,true,true);//NumericRangeQuery.newLongRange("fileSize",0,200,true,true);
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}

// 组合查询
@Test
public void testBooleanQuery()throws Exception{
IndexSearcher indexSearcher = getIndexSearcher();
BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("fileName","java.txt"));
Query query2 = new TermQuery(new Term("fileContent","good"));
//Must 是必须有 SHOULD是可以有可以不有 Must_not 是必须没有
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);

booleanQuery.add(query2, BooleanClause.Occur.SHOULD);

printResult(indexSearcher,booleanQuery);
//关闭资源
indexSearcher.getIndexReader().close();
}

}

原文地址:https://www.cnblogs.com/lovetl/p/11897834.html