lucene查询的核心类:
IndexSearcher、Term、Query、TermQuery、TopDocs、ScoreDoc
// 此处是创建索引存放的地址
Directory driectory = FSDirectory.open(new File("D:/lucene/files2"));
IndexReader reader = IndexReader.open(driectory);
IndexSearcher indexSearche=new IndexSearcher(reader);
IndexSearcher indexSearcher = new IndexSearcher(reader);
Query query=new TermQuery(new Term(field,name));
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:" + tds.totalHits);
for (ScoreDoc sd : tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);//根据ScoreDoc获取相应的Document
System.out.println(doc.get("fieldName"));
}
Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
IndexSearcher searcher = new IndexSearcher(reader);
Query q = new TermQuery(new Term("fieldName", "keyWord"));
TopDocs tds = searcher.search(q, 500);
ScoreDoc[] sds = tds.scoreDocs;
// 查询500个,从start开始,从end结束
for (int i = start; i < end; i++) {
Document doc = searcher.doc(sds[i].doc);
System.out.println("path" + doc.get("path"));
}
Query q =NumericRangeQuery.newIntRange(field,start,end,true,true);
TopDocs tds = searcher.search(q, num);
//此时的value就是通过前缀来匹配的
Query q =new PrefixQuery(new Term(field,value));
//此查询是查询filename以java开始的所有结果集
Query q = new WildcardQuery(new Term("filename","java*"));
//调整相似读 允许有个别错误的查询
Query q = new FuzzyQuery(new Term("field", ""), 0.4f, 0);
QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));
Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, "content",new StandardAnalyzer(Version.LUCENE_35));
Query q = parser.parse(query);
TopDocs tds = searcher.search(q, 500);
ScoreDoc[] sds = tds.scoreDocs;
// 每次取出所有数据,从这写数据进行再查询
int start = (pageIndex - 1) * pageSize;
int end = pageIndex * pageSize;
for (int i = start; i < end; i++) {
Document doc = searcher.doc(sds[i].doc);
System.out.println("filename=" + doc.get("filename"));
}
searcher.close();
public class AnalyzerUtils {
public void searcher(String queryStr, Sort sort) {
try {
Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Version version = Version.LUCENE_35;
Analyzer analyzer = new StandardAnalyzer(version);
QueryParser parser = new QueryParser(version, "content", analyzer);
Query query = parser.parse(queryStr);
TopDocs tds = null;
if (sort == null) {
// 默认排序
tds = searcher.search(query, 50);
} else {
// 按照穿日的值进行排序
tds = searcher.search(query, 50, sort);
}
for (ScoreDoc sd : tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println("评分:" + sd.score + "-->filename"
+ doc.get("filename"));
}
searcher.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 先按大小再按评分排序
analyzerUtils.searcher("java", new Sort(new SortField("size",
SortField.INT), SortField.FIELD_SCORE));
// 按照id排序
analyzerUtils.searcher("java", Sort.INDEXORDER);
AnalyzerUtils analyzerUtils = new AnalyzerUtils();
// 先按大小再按评分排序
analyzerUtils.searcher("java", new Sort(new SortField("size",SortField.INT), SortField.FIELD_SCORE));
// 按照id排序
analyzerUtils.searcher("java", Sort.INDEXORDER);
// 按照默认评分排序
analyzerUtils.searcher("java", null);
// 按照文件的大小排序
analyzerUtils.searcher("java", new Sort(new SortField("size",SortField.INT)));
// 按照日期排序
analyzerUtils.searcher("java", new Sort(new SortField("date",SortField.LONG)));
// 通过设置一个最后一个参数booleanReverse是否反转的意思就是倒叙即DESC还是ASC
analyzerUtils.searcher("java", new Sort(new SortField("date",SortField.LONG)));