1.删除全部
public IndexWriter getIndexWriter() throws IOException{
Directory directory=FSDirectory.open(new File("D:\\资料\\index"));
Analyzer analyzer=new IKAnalyzer();
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();
}
2.指定条件删除
@Test
public void testDelete()throws Exception{
IndexWriter indexWriter=getIndexWriter();
Query query=new TermQuery(new Term("fileName","apache"));
indexWriter.deleteDocuments(query);
indexWriter.close();
}
3.索引库的修改
@Test
public void testUpdate() throws IOException{
IndexWriter indexWriter=getIndexWriter();
Document doc=new Document();
doc.add(new TextField("fileN","测试文件名",Store.YES));
doc.add(new TextField("fileC","测试文件内容",Store.YES));
indexWriter.updateDocument(new Term("fileName","lucene"),doc,new IKAnalyzer());
indexWriter.close();
}
4.查询所有
public IndexSearcher getIndexSearcher() throws IOException{
//创建一个Directory对象,也就是索引库位置
Directory directory=FSDirectory.open(new File("D:\\资料\\index"));
//创建一个IndexReader对象,需要指定directory对象
IndexReader indexReader=DirectoryReader.open(directory);
//创建一个IndexSearch对象,需要指定indexReader对象
return new IndexSearcher(indexReader);
}
//执行查询结果
public void printResult(IndexSearcher indexSearch,Query query) throws IOException{
TopDocs topDocs=indexSearch.search(query, 2);
//返回查询结果,遍历查询结果并返回
ScoreDoc[] scoreDocs=topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
int doc=scoreDoc.doc;
Document document=indexSearch.doc(doc);
//文件名称
String fileName=document.get("fileName");
System.out.println(fileName);
//文件内容
String fileContent=document.get("fileContent");
System.out.println(fileContent);
//文件大小
String fileSize=document.get("fileSize");
System.out.println(fileSize);
//文件路径
String filePath=document.get("filePath");
System.out.println(filePath);
}
}
//查询所有
@Test
public void testMatchAllDocsQuery() throws IOException{
IndexSearcher indexSearcher=getIndexSearcher();
Query query=new MatchAllDocsQuery();
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}
5.根据数值范围查询
@Test
public void testNumberRangeQuery() throws IOException{
IndexSearcher indexSearcher=getIndexSearcher();
Query query=NumericRangeQuery.newLongRange("fileSize",100L,200L,true,true);
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}
6.组合查询
@Test
public void testBooleanQuery() throws IOException{
IndexSearcher indexSearcher=getIndexSearcher();
BooleanQuery query=new BooleanQuery();
Query query1=new TermQuery(new Term("fileName","apacher"));
Query query2=new TermQuery(new Term("fileContent","lucene"));
query.add(query1,Occur.MUST);
query.add(query2,Occur.MUST);
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}
7.条件解析的对象查询
@Test
public void testQueryParser() throws ParseException, IOException{
IndexSearcher indexSearcher=getIndexSearcher();
QueryParser queryParser=new QueryParser("fileName",new IKAnalyzer());
Query query=queryParser.parse("*:*");
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}
8.多默认域查询
@Test
public void testMultiFieldQueryParser() throws ParseException, IOException{
IndexSearcher indexSearcher=getIndexSearcher();
String[] fields={"fileName","fileContent"};
MultiFieldQueryParser queryParser=new MultiFieldQueryParser(fields,new IKAnalyzer());
Query query=queryParser.parse("lucene is java");
printResult(indexSearcher,query);
//关闭资源
indexSearcher.getIndexReader().close();
}