首页 > 学院 > 开发设计 > 正文

基于lucene的案例开发:查询语句创建PackQuery

2019-11-15 00:51:48
字体:
来源:转载
供稿:网友
基于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?
  1. /**
  2. *@Description:创建查询Query
  3. */
  4. packagecom.lulei.lucene.query;
  5. importjava.io.IOException;
  6. importjava.io.StringReader;
  7. importjava.util.ArrayList;
  8. importorg.apache.lucene.analysis.Analyzer;
  9. importorg.apache.lucene.analysis.TokenStream;
  10. importorg.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  11. importorg.apache.lucene.index.Term;
  12. importorg.apache.lucene.queryparser.classic.MultiFieldQueryParser;
  13. importorg.apache.lucene.queryparser.classic.ParseException;
  14. importorg.apache.lucene.queryparser.classic.QueryParser;
  15. importorg.apache.lucene.search.BooleanClause.Occur;
  16. importorg.apache.lucene.search.BooleanQuery;
  17. importorg.apache.lucene.search.NumericRangeQuery;
  18. importorg.apache.lucene.search.PhraseQuery;
  19. importorg.apache.lucene.search.PrefixQuery;
  20. importorg.apache.lucene.search.Query;
  21. importorg.apache.lucene.search.TermQuery;
  22. importorg.apache.lucene.search.TermRangeQuery;
  23. importorg.apache.lucene.search.WildcardQuery;
  24. importorg.apache.lucene.util.Version;
  25. importcom.lulei.lucene.index.manager.IndexManager;
  26. publicclassPackQuery{
  27. //分词器
  28. privateAnalyzeranalyzer;
  29. //使用索引中的分词器
  30. publicPackQuery(StringindexName){
  31. analyzer=IndexManager.getIndexManager(indexName).getAnalyzer();
  32. }
  33. //使用自定义分词器
  34. publicPackQuery(Analyzeranalyzer){
  35. this.analyzer=analyzer;
  36. }
  37. /**
  38. *@paramkey
  39. *@paramfields
  40. *@returnQuery
  41. *@throwsParseException
  42. *@Author:lulei
  43. *@Description:查询字符串匹配多个查询域
  44. */
  45. publicQuerygetMultiFieldQuery(Stringkey,String[]fields)throwsParseException{
  46. MultiFieldQueryParserparse=newMultiFieldQueryParser(Version.LUCENE_43,fields,analyzer);
  47. Queryquery=null;
  48. query=parse.parse(key);
  49. returnquery;
  50. }
  51. /**
  52. *@paramkey
  53. *@paramfield
  54. *@returnQuery
  55. *@throwsParseException
  56. *@Author:lulei
  57. *@Description:查询字符串匹配单个查询域
  58. */
  59. publicQuerygetOneFieldQuery(Stringkey,Stringfield)throwsParseException{
  60. if(key==null||key.length()<1){
  61. returnnull;
  62. }
  63. QueryParserparse=newQueryParser(Version.LUCENE_43,field,analyzer);
  64. Queryquery=null;
  65. query=parse.parse(key);
  66. returnquery;
  67. }
  68. /**
  69. *@paramkey
  70. *@paramfields
  71. *@paramoccur
  72. *@returnQuery
  73. *@throwsIOException
  74. *@Author:lulei
  75. *@Description:查询字符串、多个查询域以及查询域在查询语句中的关系
  76. */
  77. publicQuerygetBooleanQuery(Stringkey,String[]fields,Occur[]occur)throwsIOException{
  78. if(fields.length!=occur.length){
  79. System.out.println("fields.lengthisn'tequalsoccur.length,pleasecheckparams!");
  80. returnnull;
  81. }
  82. BooleanQueryquery=newBooleanQuery();
  83. TokenStreamtokenStream=analyzer.tokenStream("",newStringReader(key));
  84. ArrayList<String>analyzerKeys=newArrayList<String>();
  85. while(tokenStream.incrementToken()){
  86. CharTermAttributeterm=tokenStream.getAttribute(CharTermAttribute.class);
  87. analyzerKeys.add(term.toString());
  88. }
  89. for(inti=0;i<fields.length;i++){
  90. BooleanQueryqueryField=newBooleanQuery();
  91. for(StringanalyzerKey:analyzerKeys){
  92. TermQuerytermQuery=newTermQuery(newTerm(fields[i],analyzerKey));
  93. queryField.add(termQuery,Occur.SHOULD);
  94. }
  95. query.add(queryField,occur[i]);
  96. }
  97. returnquery;
  98. }
  99. /**
  100. *@paramquerys
  101. *@paramoccur
  102. *@returnQuery
  103. *@Author:lulei
  104. *@Description:组合多个查询,之间的关系由occur确定
  105. */
  106. publicQuerygetBooleanQuery(ArrayList<Query>querys,ArrayList<Occur>occurs){
  107. if(querys.size()!=occurs.size()){
  108. System.out.println("querys.size()isn'tequalsoccurs.size(),pleasecheckparams!");
  109. returnnull;
  110. }
  111. BooleanQueryquery=newBooleanQuery();
  112. for(inti=0;i<querys.size();i++){
  113. query.add(querys.get(i),occurs.get(i));
  114. }
  115. returnquery;
  116. }
  117. /**
  118. *@paramfieldName
  119. *@paramvalue
  120. *@return
  121. *@Author:lulei
  122. *@Description:StringField属性的搜索
  123. */
  124. publicQuerygetStringFieldQuery(Stringvalue,StringfieldName){
  125. Queryquery=null;
  126. query=newTermQuery(newTerm(fieldName,value));
  127. returnquery;
  128. }
  129. /**
  130. *@paramfields
  131. *@paramvalues
  132. *@return
  133. *@Author:lulei
  134. *@Description:多个StringField属性的搜索
  135. */
  136. publicQuerygetStringFieldQuery(String[]values,String[]fields,Occuroccur){
  137. if(fields==null||values==null||fields.length!=values.length){
  138. returnnull;
  139. }
  140. ArrayList<Query>querys=newArrayList<Query>();
  141. ArrayList<Occur>occurs=newArrayList<Occur>();
  142. for(inti=0;i<fields.length;i++){
  143. querys.add(getStringFieldQuery(values[i],fields[i]));
  144. occurs.add(occur);
  145. }
  146. returngetBooleanQuery(querys,occurs);
  147. }
  148. /**
  149. *@paramkey
  150. *@paramfield
  151. *@paramlucene43
  152. *@return
  153. *@throwsParseException
  154. *@Author:lulei
  155. *@Description:查询字符串和单个查询域QueryParser是否使用4.3
  156. */
  157. publicQuerygetOneFieldQuery(Stringkey,Stringfield,booleanlucene43)throwsParseException{
  158. if(key==null||key.length()<1){
  159. returnnull;
  160. }
  161. if(lucene43){
  162. returngetOneFieldQuery(key,field);
  163. }
  164. @SuppressWarnings("deprecation")
  165. QueryParserparse=newQueryParser(Version.LUCENE_30,field,analyzer);
  166. Queryquery=null;
  167. query=parse.parse(key);
  168. returnquery;
  169. }
  170. /**
  171. *@paramkey
  172. *@paramfield
  173. *@Author:lulei
  174. *@Description:key开头的查询字符串,和单个域匹配
  175. */
  176. publicQuerygetStartQuery(Stringkey,Stringfield){
  177. if(key==null||key.length()<1){
  178. returnnull;
  179. }
  180. Queryquery=newPrefixQuery(newTerm(field,key));
  181. returnquery;
  182. }
  183. /**
  184. *@paramkey
  185. *@paramfields
  186. *@paramoccur
  187. *@Author:lulei
  188. *@Description:key开头的查询字符串,和多个域匹配,每个域之间的关系由occur确定
  189. */
  190. publicQuerygetStartQuery(Stringkey,String[]fields,Occuroccur){
  191. if(key==null||key.length()<1){
  192. returnnull;
  193. }
  194. ArrayList<Query>querys=newArrayList<Query>();
  195. ArrayList<Occur>occurs=newArrayList<Occur>();
  196. for(Stringfield:fields){
  197. querys.add(getStartQuery(key,field));
  198. occurs.add(occur);
  199. }
  200. returngetBooleanQuery(querys,occurs);
  201. }
  202. /**
  203. *@paramkey
  204. *@paramfields
  205. *@Author:lulei
  206. *@Description:key开头的查询字符串,和多个域匹配,每个域之间的关系Occur.SHOULD
  207. */
  208. publicQuerygetStartQuery(Stringkey,String[]fields){
  209. returngetStartQuery(key,fields,Occur.SHOULD);
  210. }
  211. /**
  212. *@paramkey
  213. *@paramfield
  214. *@paramslop
  215. *@return
  216. *@Author:lulei
  217. *@Description:自定每个词元之间的最大距离
  218. */
  219. publicQuerygetPhraseQuery(Stringkey,Stringfield,intslop){
  220. if(key==null||key.length()<1){
  221. returnnull;
  222. }
  223. StringReaderreader=newStringReader(key);
  224. PhraseQueryquery=newPhraseQuery();
  225. query.setSlop(slop);
  226. try{
  227. TokenStreamtokenStream=this.analyzer.tokenStream(field,reader);
  228. tokenStream.reset();
  229. CharTermAttributeterm=tokenStream.getAttribute(CharTermAttribute.class);
  230. while(tokenStream.incrementToken()){
  231. query.add(newTerm(field,term.toString()));
  232. }
  233. reader.close();
  234. }catch(IOExceptione){
  235. e.printStackTrace();
  236. returnnull;
  237. }
  238. returnquery;
  239. }
  240. /**
  241. *@paramkey
  242. *@paramfields
  243. *@paramslop
  244. *@paramoccur
  245. *@return
  246. *@Author:lulei
  247. *@Description:自定每个词元之间的最大距离,查询多个域,每个域之间的关系由occur确定
  248. */
  249. publicQuerygetPhraseQuery(Stringkey,String[]fields,intslop,Occuroccur){
  250. if(key==null||key.length()<1){
  251. returnnull;
  252. }
  253. ArrayList<Query>querys=newArrayList<Query>();
  254. ArrayList<Occur>occurs=newArrayList<Occur>();
  255. for(Stringfield:fields){
  256. querys.add(getPhraseQuery(key,field,slop));
  257. occurs.add(occur);
  258. }
  259. returngetBooleanQuery(querys,occurs);
  260. }
  261. /**
  262. *@paramkey
  263. *@paramfields
  264. *@paramslop
  265. *@return
  266. *@Author:lulei
  267. *@Description:自定每个词元之间的最大距离,查询多个域,每个域之间的关系是Occur.SHOULD
  268. */
  269. publicQuerygetPhraseQuery(Stringkey,String[]fields,intslop){
  270. returngetPhraseQuery(key,fields,slop,Occur.SHOULD);
  271. }
  272. /**
  273. *@paramkey
  274. *@paramfield
  275. *@return
  276. *@Author:lulei
  277. *@Description:通配符检索eg:getWildcardQuery("a*thor","field")
  278. */
  279. publicQuerygetWildcardQuery(Stringkey,Stringfield){
  280. if(key==null||key.length()<1){
  281. returnnull;
  282. }
  283. returnnewWildcardQuery(newTerm(field,key));
  284. }
  285. /**
  286. *@paramkey
  287. *@paramfields
  288. *@paramoccur
  289. *@return
  290. *@Author:lulei
  291. *@Description:通配符检索,域之间的关系为occur
  292. */
  293. publicQuerygetWildcardQuery(Stringkey,String[]fields,Occuroccur){
  294. if(key==null||key.length()<1){
  295. returnnull;
  296. }
  297. ArrayList<Query>querys=newArrayList<Query>();
  298. ArrayList<Occur>occurs=newArrayList<Occur>();
  299. for(Stringfield:fields){
  300. querys.add(getWildcardQuery(key,field));
  301. occurs.add(occur);
  302. }
  303. returngetBooleanQuery(querys,occurs);
  304. }
  305. /**
  306. *@paramkey
  307. *@paramfields
  308. *@return
  309. *@Author:lulei
  310. *@Description:通配符检索,域之间的关系为Occur.SHOULD
  311. */
  312. publicQuerygetWildcardQuery(Stringkey,String[]fields){
  313. returngetWildcardQuery(key,fields,Occur.SHOULD);
  314. }
  315. /**
  316. *@paramkeyStart
  317. *@paramkeyEnd
  318. *@paramfield
  319. *@paramincludeStart
  320. *@paramincludeEnd
  321. *@return
  322. *@Author:lulei
  323. *@Description:范围搜索
  324. */
  325. publicQuerygetRangeQuery(StringkeyStart,StringkeyEnd,Stringfield,booleanincludeStart,booleanincludeEnd){
  326. returnTermRangeQuery.newStringRange(field,keyStart,keyEnd,includeStart,includeEnd);
  327. }
  328. /**
  329. *@parammin
  330. *@parammax
  331. *@paramfield
  332. *@paramincludeMin
  333. *@paramincludeMax
  334. *@return
  335. *@Author:lulei
  336. *@Description:范围搜索
  337. */
  338. publicQuerygetRangeQuery(intmin,intmax,Stringfield,booleanincludeMin,booleanincludeMax){
  339. returnNumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
  340. }
  341. /**
  342. *@parammin
  343. *@parammax
  344. *@paramfield
  345. *@paramincludeMin
  346. *@paramincludeMax
  347. *@return
  348. *@Author:lulei
  349. *@Description:范围搜索
  350. */
  351. publicQuerygetRangeQuery(floatmin,floatmax,Stringfield,booleanincludeMin,booleanincludeMax){
  352. returnNumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
  353. }
  354. /**
  355. *@parammin
  356. *@parammax
  357. *@paramfield
  358. *@paramincludeMin
  359. *@paramincludeMax
  360. *@return
  361. *@Author:lulei
  362. *@Description:范围搜索
  363. */
  364. publicQuerygetRangeQuery(doublemin,doublemax,Stringfield,booleanincludeMin,booleanincludeMax){
  365. returnNumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
  366. }
  367. publicstaticvoidmain(String[]args)throwsIOException{
  368. }
  369. }

PackQuery类的构造方法,可以手动指定分词器也可以使用索引的分词器。个人建议,在项目中使用索引中的分词器,这样就不会因为分词器的不同造成不知名的错误。

----------------------------------------------------------------------------------------------------

ps:最近发现其他网站可能会对博客转载,上面并没有源链接,如想查看更多关于 基于lucene的案例开发 请点击这里。或访问网址http://blog.csdn.net/xiaojimanman/article/category/2841877


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表