概述
对要搜索的信息创建Query查询对象,Lucene会根据Query查询对象生成最终的查询语法,类似关系数据库SQL语法一样,Lucene也有自己的查询语法;
可通过两种方法创建查询对象:
1.使用Lucene提供Query子类;
2.使用QueryParse解析查询表达式
TermQuery查询
通过项查询,TermQuery不使用分析器所以建议匹配不分词的Field域查询,比如订单号,分类ID号等;
指定要查询的域和要查询的关键词
package com.wn.Query; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.File; import java.io.IOException; /*根据域和关键词进行搜索*/ public class TermQuery { /*查询索引库*/ public static void main(String[] args)throws IOException { Directory directory= FSDirectory.open(new File("E:\Lucene\temp\index").toPath()); IndexReader indexReader= DirectoryReader.open(directory); IndexSearcher indexSearcher=new IndexSearcher(indexReader); //创建查询对象 Query query=new org.apache.lucene.search.TermQuery(new Term("fieldName","text01.txt")); //执行查询 TopDocs topDocs=indexSearcher.search(query,10); System.out.println("查询总数量:"+topDocs.totalHits); //遍历查询结果 for (ScoreDoc scoreDoc:topDocs.scoreDocs){ Document document = indexSearcher.doc(scoreDoc.doc); System.out.println(document.get("fieldName")); System.out.println(document.get("fieldContent")); System.out.println(document.get("fieldPath")); System.out.println(document.get("fieldSize")); System.out.println("===================================================="); } //关闭资源 indexSearcher.getIndexReader().close(); } }
效果实现
数值范围查询
package com.wn.Query; import org.apache.lucene.document.Document; import org.apache.lucene.document.LongPoint; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.File; import java.io.IOException; public class testRangeQuery { /*数值范围查询*/ public static void main(String[] args)throws IOException { Directory directory= FSDirectory.open(new File("E:\Lucene\temp\index").toPath()); IndexReader indexReader= DirectoryReader.open(directory); IndexSearcher indexSearcher=new IndexSearcher(indexReader); //创建查询对象 Query query= LongPoint.newRangeQuery("fieldSize",0,100); //执行查询 TopDocs topDocs=indexSearcher.search(query,10); System.out.println("查询总数量:"+topDocs.totalHits); //遍历查询结果 for (ScoreDoc scoreDoc:topDocs.scoreDocs){ Document document = indexSearcher.doc(scoreDoc.doc); System.out.println(document.get("fieldName")); System.out.println(document.get("fieldContent")); System.out.println(document.get("fieldPath")); System.out.println(document.get("fieldSize")); System.out.println("===================================================="); } //关闭资源 indexSearcher.getIndexReader().close(); } }
效果实现
QueryParser查询
通过QueryParse也可以创建Query,QueryParser提供一个parse方法,此方法可以直接根据查询语法来查询。Query对象执行的查询语法可通过system.out.println(query);查询。
需要使用到分词器,建议创建索引时使用的分词器和查询索引时使用的分词器要一致。需要加入queryParser依赖
package com.wn.Query; import org.apache.lucene.document.Document; import org.apache.lucene.document.LongPoint; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; /*搜索條件分詞*/ public class Queryparsers { public static void main(String[] args) throws IOException, ParseException { Directory directory= FSDirectory.open(new File("E:\Lucene\temp\index").toPath()); IndexReader indexReader= DirectoryReader.open(directory); IndexSearcher indexSearcher=new IndexSearcher(indexReader); //创建查询对象 QueryParser queryParser=new QueryParser("fieldContent",new IKAnalyzer()); Query query = queryParser.parse("全文检索"); //执行查询 TopDocs topDocs=indexSearcher.search(query,10); System.out.println("查询总数量:"+topDocs.totalHits); //遍历查询结果 for (ScoreDoc scoreDoc:topDocs.scoreDocs){ Document document = indexSearcher.doc(scoreDoc.doc); System.out.println(document.get("fieldName")); System.out.println(document.get("fieldContent")); System.out.println(document.get("fieldPath")); System.out.println(document.get("fieldSize")); System.out.println("===================================================="); } //关闭资源 indexSearcher.getIndexReader().close(); } }
实现效果