通过Query的实现类来实现查询:
使用query的实现类TermQyery来实现对某一域的精准查询:
首先获取索引库的存放位置,创建链接索引库的IndexReader对象,并使用该对象来创建操作索引库的IndexSearcher对象,然后使用TermQuery来创建Query对象,并使用IndexSearcher的seach()方法来实现查询,获取对应文档的唯一id,并使用该id来获取文档对象,进而获取具体的信息,最后关流,代码实现如下:
//创建directory对象
Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
//创建 IndexReader 对象,用来连接索引库
IndexReader indexReader = DirectoryReader.open(directory);
//创建 IndexSearcher对象,用来查询索引库
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//创建查询条件 查询 fileName域中 为java的 ,精准查询
Query query = new TermQuery(new Term("fileName", "测试测试"));
//开始查询
TopDocs topDocs = indexSearcher.search(query, 2);
//获得查询结果(文档document的对应的id)
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
//文档id
int docId = scoreDoc.doc;
//通过文档id来获取文档对象
Document document = indexSearcher.doc(docId);
//通过文档对象document来获取域值
String fileName = document.get("fileName");
String filePath = document.get("filePath");
String fileSize = document.get("fileSize");
String fileContext = document.get("fileContext");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
System.out.println("fileSize:"+fileSize);
System.out.println("fileContext:"+fileContext);
System.out.println("-----------------");
}
//关闭IndexReader 索引库连接
indexReader.close();
使用Query的MatchAllDocsQuery实现类来实现查询所有效果:
实现原理类似使用termQuery实现类实现精准查询,不同的使用不同的实现类来创建Query对象,代码实现如下:
try {
Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//MatchAllDocsQyery()子类 用来查询所有 TermQuery() 精准查询
Query query = new MatchAllDocsQuery();
TopDocs topDocs = indexSearcher.search(query, 100);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
String fileName = document.get("fileName");
String filePath = document.get("filePath");
String fileSize = document.get("fileSize");
String fileContext = document.get("fileContext");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
System.out.println("fileSize:"+fileSize);
System.out.println("fileContext:"+fileContext);
System.out.println("-----------------");
}
indexSearcher.getIndexReader().close();
} catch (IOException e) {
e.printStackTrace();
}
使用BooleanQuery实现类来实现多条件查询,代码如下:
try {
Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
IndexReader indexreader = DirectoryReader.open(directory);
IndexSearcher indexsearcher = new IndexSearcher(indexreader);
BooleanQuery.Builder booleanBuilder = new BooleanQuery.Builder();
Query query = new TermQuery(new Term("fileName","java"));
// Occur.SHOULD(语法 空格) 类比于 or Occur.MUST(语法+) 类似于 and Occur.MUST_NOT(语法-) 类似于 <> (不等于)
booleanBuilder.add(query, Occur.SHOULD);
Query query1 = new TermQuery(new Term("fileName","样品"));
booleanBuilder.add(query1, Occur.SHOULD);
Query query2 = new TermQuery(new Term("fileName","管理"));
booleanBuilder.add(query2, Occur.SHOULD);
BooleanQuery booleanQuery = booleanBuilder.build();
TopDocs topDocs = indexsearcher.search(booleanQuery, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexsearcher.doc(docId);
String fileName = document.get("fileName");
String filePath = document.get("filePath");
String fileSize = document.get("fileSize");
String fileContext = document.get("fileContext");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
System.out.println("fileSize:"+fileSize);
System.out.println("fileContext:"+fileContext);
System.out.println("-----------------");
}
indexsearcher.getIndexReader().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用queryparser来实现解析查询:
使用QueryParser来指定默认查询域
try {
Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//默认域, 和分词器
QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
/**
* 查询所有语法
*/
//Query query = queryParser.parse("*:*");
/**
* 查询某一字符串
*/
Query query = queryParser.parse("java is 样品");
/**
* 查询符合多个域的语法
*/
//Query query = queryParser.parse("-fileName:java fileName:管理");
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
String fileName = document.get("fileName");
String filePath = document.get("filePath");
String fileSize = document.get("fileSize");
String fileContext = document.get("fileContext");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
System.out.println("fileSize:"+fileSize);
System.out.println("fileContext:"+fileContext);
System.out.println("-----------------");
}
indexSearcher.getIndexReader().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用MultiFieldQueryParser来实现多默认域解析查询:
try {
Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
String[] fields = {"filePath","fileName","fileContext"};
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
Query query = multiFieldQueryParser.parse("java is greate 管理 新建");
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
String fileName = document.get("fileName");
String filePath = document.get("filePath");
String fileSize = document.get("fileSize");
String fileContext = document.get("fileContext");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
System.out.println("fileSize:"+fileSize);
System.out.println("fileContext:"+fileContext);
System.out.println("-----------------");
}
indexSearcher.getIndexReader().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}