前言:
本案例所引用的jar以及搜索的文档内容均在https://blog.csdn.net/qq_15076569/article/details/83031572基础之上进行
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.tools.ant.taskdefs.condition.IsFalse;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
public class LunceneSearch {
public static void main(String[] args) throws IOException, ParseException {
//1.执行查询
//创建单个查询对象
QueryParser queryParser = new QueryParser("content",new IKAnalyzer());
Query query = queryParser.parse("大墓碑");
query(query);
//2.词条查询
//2.1词条查询的词条是不可在分割的,也就是一个索引中能完全匹配到的一个完整的词
//2.2使用场景:主要是针对不可在分割的字段,例如id
TermQuery termQuery = new TermQuery(new Term("content","好吃"));
System.out.println("------------词条查询--------------");
query(termQuery);
//3.通配符查询
//通配符:
//*: 代表多个字符
//?: 代表一个占位符
WildcardQuery wildcardQuery = new WildcardQuery(new Term("content", "*蓝瘦*"));
System.out.println("------------通配符查询--------------");
query(wildcardQuery);
//4.模糊查询
/**
* 模糊查询:
* 指的是通过替换, 补位, 移动 能够在二次切换内查询数据即可返回
* 参数1: term 指定查询的字段和内容
* 参数2: int n 表示最大编辑的次数 最大2
*
*/
FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term("content", "比蓝瘦"),2);
System.out.println("------------模糊查询--------------");
query(fuzzyQuery);
//5.数值范围查询
/**
* 获取NumericRangeQuery的方式:
* 通过提供的静态方法获取:
* NumericRangeQuery.newIntRange()
* NumericRangeQuery.newFloatRange()
* NumericRangeQuery.newDoubleRange()
* NumericRangeQuery.newLongRange()
*
*
* 数值范围查询:
* 参数1: 指定要查询的字段
* 参数2: 指定要查询的开始值
* 参数3: 指定要查询的结束值
* 参数4: 是否包含开始
* 参数5: 是否包含结束
*/
NumericRangeQuery numericRangeQuery =NumericRangeQuery.newIntRange("id",0,4,false,false);
System.out.println("-----------数值范围查询--------------");
query(numericRangeQuery);
//6.组合查询
NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id", 1, 2, true, false);
NumericRangeQuery<Integer> query2 = NumericRangeQuery.newIntRange("id", 1, 2, false, true);
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
query(booleanQuery);
}
public static void query(Query query) throws IOException {
//1.创建查询核心对象
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(new File("E://test")));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
//2.执行查询
TopDocs topDocs = indexSearcher.search(query, 10);
//3.解析结果
System.out.println("查询总结果个数:"+topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;//获取得分文档集合
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;//文档ID
float score = scoreDoc.score;//文档得分;约接近1匹配度越高
//根据id获取文档
Document document = indexSearcher.doc(docId);
String content = document.get("content");
String title = document.get("title");
System.out.println(score+","+content+","+title);
}
}
}