基于lucene的案例开发:查询语句创建PackQuery转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44656141
http://www.llwjy.com/blogdetail/162e5e70516d7ddfb6df8f77e6b13a2b.html
个人博客站已经上线了,网址 www.llwjy.com ~欢迎各位吐槽
-----------------------------------------------------------------------------------------------------------
在之前的《基于lucene的案例开发:Query查询》这篇博客中对实际开发过程中比较常见的Query做了简单的介绍,这里就介绍下具体的代码实现。查看最新代码点击这里或访问http://www.llwjy.com/source/com.lulei.lucene.query.PackQuery.html
view plaincopy to clipboardPRint?- /**
- *@Description:创建查询Query
- */
- packagecom.lulei.lucene.query;
- importjava.io.IOException;
- importjava.io.StringReader;
- importjava.util.ArrayList;
- importorg.apache.lucene.analysis.Analyzer;
- importorg.apache.lucene.analysis.TokenStream;
- importorg.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- importorg.apache.lucene.index.Term;
- importorg.apache.lucene.queryparser.classic.MultiFieldQueryParser;
- importorg.apache.lucene.queryparser.classic.ParseException;
- importorg.apache.lucene.queryparser.classic.QueryParser;
- importorg.apache.lucene.search.BooleanClause.Occur;
- importorg.apache.lucene.search.BooleanQuery;
- importorg.apache.lucene.search.NumericRangeQuery;
- importorg.apache.lucene.search.PhraseQuery;
- importorg.apache.lucene.search.PrefixQuery;
- importorg.apache.lucene.search.Query;
- importorg.apache.lucene.search.TermQuery;
- importorg.apache.lucene.search.TermRangeQuery;
- importorg.apache.lucene.search.WildcardQuery;
- importorg.apache.lucene.util.Version;
- importcom.lulei.lucene.index.manager.IndexManager;
- publicclassPackQuery{
- //分词器
- privateAnalyzeranalyzer;
- //使用索引中的分词器
- publicPackQuery(StringindexName){
- analyzer=IndexManager.getIndexManager(indexName).getAnalyzer();
- }
- //使用自定义分词器
- publicPackQuery(Analyzeranalyzer){
- this.analyzer=analyzer;
- }
- /**
- *@paramkey
- *@paramfields
- *@returnQuery
- *@throwsParseException
- *@Author:lulei
- *@Description:查询字符串匹配多个查询域
- */
- publicQuerygetMultiFieldQuery(Stringkey,String[]fields)throwsParseException{
- MultiFieldQueryParserparse=newMultiFieldQueryParser(Version.LUCENE_43,fields,analyzer);
- Queryquery=null;
- query=parse.parse(key);
- returnquery;
- }
- /**
- *@paramkey
- *@paramfield
- *@returnQuery
- *@throwsParseException
- *@Author:lulei
- *@Description:查询字符串匹配单个查询域
- */
- publicQuerygetOneFieldQuery(Stringkey,Stringfield)throwsParseException{
- if(key==null||key.length()<1){
- returnnull;
- }
- QueryParserparse=newQueryParser(Version.LUCENE_43,field,analyzer);
- Queryquery=null;
- query=parse.parse(key);
- returnquery;
- }
- /**
- *@paramkey
- *@paramfields
- *@paramoccur
- *@returnQuery
- *@throwsIOException
- *@Author:lulei
- *@Description:查询字符串、多个查询域以及查询域在查询语句中的关系
- */
- publicQuerygetBooleanQuery(Stringkey,String[]fields,Occur[]occur)throwsIOException{
- if(fields.length!=occur.length){
- System.out.println("fields.lengthisn'tequalsoccur.length,pleasecheckparams!");
- returnnull;
- }
- BooleanQueryquery=newBooleanQuery();
- TokenStreamtokenStream=analyzer.tokenStream("",newStringReader(key));
- ArrayList<String>analyzerKeys=newArrayList<String>();
- while(tokenStream.incrementToken()){
- CharTermAttributeterm=tokenStream.getAttribute(CharTermAttribute.class);
- analyzerKeys.add(term.toString());
- }
- for(inti=0;i<fields.length;i++){
- BooleanQueryqueryField=newBooleanQuery();
- for(StringanalyzerKey:analyzerKeys){
- TermQuerytermQuery=newTermQuery(newTerm(fields[i],analyzerKey));
- queryField.add(termQuery,Occur.SHOULD);
- }
- query.add(queryField,occur[i]);
- }
- returnquery;
- }
- /**
- *@paramquerys
- *@paramoccur
- *@returnQuery
- *@Author:lulei
- *@Description:组合多个查询,之间的关系由occur确定
- */
- publicQuerygetBooleanQuery(ArrayList<Query>querys,ArrayList<Occur>occurs){
- if(querys.size()!=occurs.size()){
- System.out.println("querys.size()isn'tequalsoccurs.size(),pleasecheckparams!");
- returnnull;
- }
- BooleanQueryquery=newBooleanQuery();
- for(inti=0;i<querys.size();i++){
- query.add(querys.get(i),occurs.get(i));
- }
- returnquery;
- }
- /**
- *@paramfieldName
- *@paramvalue
- *@return
- *@Author:lulei
- *@Description:StringField属性的搜索
- */
- publicQuerygetStringFieldQuery(Stringvalue,StringfieldName){
- Queryquery=null;
- query=newTermQuery(newTerm(fieldName,value));
- returnquery;
- }
- /**
- *@paramfields
- *@paramvalues
- *@return
- *@Author:lulei
- *@Description:多个StringField属性的搜索
- */
- publicQuerygetStringFieldQuery(String[]values,String[]fields,Occuroccur){
- if(fields==null||values==null||fields.length!=values.length){
- returnnull;
- }
- ArrayList<Query>querys=newArrayList<Query>();
- ArrayList<Occur>occurs=newArrayList<Occur>();
- for(inti=0;i<fields.length;i++){
- querys.add(getStringFieldQuery(values[i],fields[i]));
- occurs.add(occur);
- }
- returngetBooleanQuery(querys,occurs);
- }
- /**
- *@paramkey
- *@paramfield
- *@paramlucene43
- *@return
- *@throwsParseException
- *@Author:lulei
- *@Description:查询字符串和单个查询域QueryParser是否使用4.3
- */
- publicQuerygetOneFieldQuery(Stringkey,Stringfield,booleanlucene43)throwsParseException{
- if(key==null||key.length()<1){
- returnnull;
- }
- if(lucene43){
- returngetOneFieldQuery(key,field);
- }
- @SuppressWarnings("deprecation")
- QueryParserparse=newQueryParser(Version.LUCENE_30,field,analyzer);
- Queryquery=null;
- query=parse.parse(key);
- returnquery;
- }
- /**
- *@paramkey
- *@paramfield
- *@Author:lulei
- *@Description:key开头的查询字符串,和单个域匹配
- */
- publicQuerygetStartQuery(Stringkey,Stringfield){
- if(key==null||key.length()<1){
- returnnull;
- }
- Queryquery=newPrefixQuery(newTerm(field,key));
- returnquery;
- }
- /**
- *@paramkey
- *@paramfields
- *@paramoccur
- *@Author:lulei
- *@Description:key开头的查询字符串,和多个域匹配,每个域之间的关系由occur确定
- */
- publicQuerygetStartQuery(Stringkey,String[]fields,Occuroccur){
- if(key==null||key.length()<1){
- returnnull;
- }
- ArrayList<Query>querys=newArrayList<Query>();
- ArrayList<Occur>occurs=newArrayList<Occur>();
- for(Stringfield:fields){
- querys.add(getStartQuery(key,field));
- occurs.add(occur);
- }
- returngetBooleanQuery(querys,occurs);
- }
- /**
- *@paramkey
- *@paramfields
- *@Author:lulei
- *@Description:key开头的查询字符串,和多个域匹配,每个域之间的关系Occur.SHOULD
- */
- publicQuerygetStartQuery(Stringkey,String[]fields){
- returngetStartQuery(key,fields,Occur.SHOULD);
- }
- /**
- *@paramkey
- *@paramfield
- *@paramslop
- *@return
- *@Author:lulei
- *@Description:自定每个词元之间的最大距离
- */
- publicQuerygetPhraseQuery(Stringkey,Stringfield,intslop){
- if(key==null||key.length()<1){
- returnnull;
- }
- StringReaderreader=newStringReader(key);
- PhraseQueryquery=newPhraseQuery();
- query.setSlop(slop);
- try{
- TokenStreamtokenStream=this.analyzer.tokenStream(field,reader);
- tokenStream.reset();
- CharTermAttributeterm=tokenStream.getAttribute(CharTermAttribute.class);
- while(tokenStream.incrementToken()){
- query.add(newTerm(field,term.toString()));
- }
- reader.close();
- }catch(IOExceptione){
- e.printStackTrace();
- returnnull;
- }
- returnquery;
- }
- /**
- *@paramkey
- *@paramfields
- *@paramslop
- *@paramoccur
- *@return
- *@Author:lulei
- *@Description:自定每个词元之间的最大距离,查询多个域,每个域之间的关系由occur确定
- */
- publicQuerygetPhraseQuery(Stringkey,String[]fields,intslop,Occuroccur){
- if(key==null||key.length()<1){
- returnnull;
- }
- ArrayList<Query>querys=newArrayList<Query>();
- ArrayList<Occur>occurs=newArrayList<Occur>();
- for(Stringfield:fields){
- querys.add(getPhraseQuery(key,field,slop));
- occurs.add(occur);
- }
- returngetBooleanQuery(querys,occurs);
- }
- /**
- *@paramkey
- *@paramfields
- *@paramslop
- *@return
- *@Author:lulei
- *@Description:自定每个词元之间的最大距离,查询多个域,每个域之间的关系是Occur.SHOULD
- */
- publicQuerygetPhraseQuery(Stringkey,String[]fields,intslop){
- returngetPhraseQuery(key,fields,slop,Occur.SHOULD);
- }
- /**
- *@paramkey
- *@paramfield
- *@return
- *@Author:lulei
- *@Description:通配符检索eg:getWildcardQuery("a*thor","field")
- */
- publicQuerygetWildcardQuery(Stringkey,Stringfield){
- if(key==null||key.length()<1){
- returnnull;
- }
- returnnewWildcardQuery(newTerm(field,key));
- }
- /**
- *@paramkey
- *@paramfields
- *@paramoccur
- *@return
- *@Author:lulei
- *@Description:通配符检索,域之间的关系为occur
- */
- publicQuerygetWildcardQuery(Stringkey,String[]fields,Occuroccur){
- if(key==null||key.length()<1){
- returnnull;
- }
- ArrayList<Query>querys=newArrayList<Query>();
- ArrayList<Occur>occurs=newArrayList<Occur>();
- for(Stringfield:fields){
- querys.add(getWildcardQuery(key,field));
- occurs.add(occur);
- }
- returngetBooleanQuery(querys,occurs);
- }
- /**
- *@paramkey
- *@paramfields
- *@return
- *@Author:lulei
- *@Description:通配符检索,域之间的关系为Occur.SHOULD
- */
- publicQuerygetWildcardQuery(Stringkey,String[]fields){
- returngetWildcardQuery(key,fields,Occur.SHOULD);
- }
- /**
- *@paramkeyStart
- *@paramkeyEnd
- *@paramfield
- *@paramincludeStart
- *@paramincludeEnd
- *@return
- *@Author:lulei
- *@Description:范围搜索
- */
- publicQuerygetRangeQuery(StringkeyStart,StringkeyEnd,Stringfield,booleanincludeStart,booleanincludeEnd){
- returnTermRangeQuery.newStringRange(field,keyStart,keyEnd,includeStart,includeEnd);
- }
- /**
- *@parammin
- *@parammax
- *@paramfield
- *@paramincludeMin
- *@paramincludeMax
- *@return
- *@Author:lulei
- *@Description:范围搜索
- */
- publicQuerygetRangeQuery(intmin,intmax,Stringfield,booleanincludeMin,booleanincludeMax){
- returnNumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
- }
- /**
- *@parammin
- *@parammax
- *@paramfield
- *@paramincludeMin
- *@paramincludeMax
- *@return
- *@Author:lulei
- *@Description:范围搜索
- */
- publicQuerygetRangeQuery(floatmin,floatmax,Stringfield,booleanincludeMin,booleanincludeMax){
- returnNumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
- }
- /**
- *@parammin
- *@parammax
- *@paramfield
- *@paramincludeMin
- *@paramincludeMax
- *@return
- *@Author:lulei
- *@Description:范围搜索
- */
- publicQuerygetRangeQuery(doublemin,doublemax,Stringfield,booleanincludeMin,booleanincludeMax){
- returnNumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
- }
- publicstaticvoidmain(String[]args)throwsIOException{
- }
- }
PackQuery类的构造方法,可以手动指定分词器也可以使用索引的分词器。个人建议,在项目中使用索引中的分词器,这样就不会因为分词器的不同造成不知名的错误。
----------------------------------------------------------------------------------------------------
ps:最近发现其他网站可能会对博客转载,上面并没有源链接,如想查看更多关于 基于lucene的案例开发 请点击这里。或访问网址http://blog.csdn.net/xiaojimanman/article/category/2841877