首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

lucene QueryParser用法示范

2012-06-29 
lucene QueryParser用法示例package?demo.first ????import?java.io.IOException ??import?java.util.Da

lucene QueryParser用法示例

  1. package?demo.first; ??
  2. ??
  3. import?java.io.IOException; ??
  4. import?java.util.Date; ??
  5. ??
  6. import?org.apache.lucene.analysis.standard.StandardAnalyzer; ??
  7. import?org.apache.lucene.document.Document; ??
  8. import?org.apache.lucene.document.Field; ??
  9. import?org.apache.lucene.index.CorruptIndexException; ??
  10. import?org.apache.lucene.index.IndexWriter; ??
  11. import?org.apache.lucene.queryParser.QueryParser; ??
  12. import?org.apache.lucene.search.Hits; ??
  13. import?org.apache.lucene.search.IndexSearcher; ??
  14. import?org.apache.lucene.search.Query; ??
  15. import?org.apache.lucene.search.Searcher; ??
  16. import?org.apache.lucene.store.LockObtainFailedException; ??
  17. ??
  18. public?class?TestQueryParser?{ ??
  19. ??
  20. ????public?static?void?main(String[]?args)?throws?CorruptIndexException,?IOException?{ ??
  21. ????????String?path?=?"D://workspace//fwk//lucenedemo//firstLuceneIndex"; ??
  22. ????????TestQueryParser?tqp?=?new?TestQueryParser(); ??
  23. ????????tqp.createIndex(path); ??
  24. ????????Searcher?search?=?tqp.getSearcher(path); ??
  25. ????????System.out.println("#_2"); ??
  26. ????????tqp.testTime(search,?tqp.getQueryParser2()); ??
  27. ????????System.out.println("#_1"); ??
  28. ????????tqp.testTime(search,?tqp.getQueryParser1()); ??
  29. ????????System.out.println("#_3"); ??
  30. ????????tqp.testTime(search,?tqp.getQueryParser3()); ??
  31. ????????System.out.println("#_4"); ??
  32. ????????tqp.testTime(search,?tqp.getQueryParser4()); ??
  33. ????????System.out.println("#_5"); ??
  34. ????????tqp.testTime(search,?tqp.getQueryParser5()); ??
  35. ????????System.out.println("#_6"); ??
  36. ????????tqp.testTime(search,?tqp.getQueryParser6()); ??
  37. ????????System.out.println("#_7"); ??
  38. ????????tqp.testTime(search,?tqp.getQueryParser7()); ??
  39. ????} ??
  40. ???? ??
  41. ????public?void?testTime(Searcher?search,Query?query)?throws?IOException{ ??
  42. ????????Date?start?=?new?Date(); ??
  43. ????????Hits?hits?=?search.search(query); ??
  44. ????????for?(int?i?=?0;?i?<?hits.length();?i++)?{ ??
  45. ????????????System.out.println(hits.id(i)); ??
  46. ????????????System.out.println(hits.doc(i)); ??
  47. ????????????System.out.println(hits.score(i)); ??
  48. ????????} ??
  49. ???????? ??
  50. ????????System.out.println("本次搜索用时:"?+?((new?Date()).getTime()?-?start.getTime())?+?"毫秒"); ??
  51. ???????? ??
  52. ????} ??
  53. ???? ??
  54. ????public?Searcher?getSearcher(String?path)?throws?CorruptIndexException,?IOException{ ??
  55. ????????????return?new?IndexSearcher(path); ??
  56. ????} ??
  57. ???? ??
  58. ????public?Query?getQueryParser1(){ ??
  59. ????????//默认搜索字段new?QueryParser("content",?new?StandardAnalyzer()); ??
  60. ????????try?{ ??
  61. ????????????return?queryParser.parse("搜索?-?擎"); ??
  62. ????????}?catch?(Exception?e)?{ ??
  63. ????????????e.printStackTrace(); ??
  64. ????????} ??
  65. ????????return?null; ??
  66. ????} ??
  67. ???? ??
  68. ????public?Query?getQueryParser2(){ ??
  69. ????????QueryParser?queryParser?=?new?QueryParser("content",?new?StandardAnalyzer()); ??
  70. ????????try?{ ??
  71. ????????????return?queryParser.parse("欢迎"); ??
  72. ????????}?catch?(Exception?e)?{ ??
  73. ????????????e.printStackTrace(); ??
  74. ????????} ??
  75. ????????return?null; ??
  76. ????} ??
  77. ???? ??
  78. ????public?Query?getQueryParser3(){ ??
  79. ????????QueryParser?queryParser?=?new?QueryParser("content",?new?StandardAnalyzer()); ??
  80. ????????try?{ ??
  81. ????????????return?queryParser.parse("搜索?and?擎"); ??
  82. ????????}?catch?(Exception?e)?{ ??
  83. ????????????e.printStackTrace(); ??
  84. ????????} ??
  85. ????????return?null; ??
  86. ????} ??
  87. ???? ??
  88. ????public?Query?getQueryParser4(){ ??
  89. ????????QueryParser?queryParser?=?new?QueryParser("content",?new?StandardAnalyzer()); ??
  90. ????????try?{ ??
  91. ????????????//content字段搜索?索引???title字段搜寻?你好return?queryParser.parse("索引?title:你好"); ??
  92. ????????}?catch?(Exception?e)?{ ??
  93. ????????????e.printStackTrace(); ??
  94. ????????} ??
  95. ????????return?null; ??
  96. ????} ??
  97. ???? ??
  98. ????public?Query?getQueryParser5(){ ??
  99. ????????QueryParser?queryParser?=?new?QueryParser("content",?new?StandardAnalyzer()); ??
  100. ????????//允许使用正则表达式方式true); ??
  101. ????????try?{ ??
  102. ????????????return?queryParser.parse("*索*"); ??
  103. ????????}?catch?(Exception?e)?{ ??
  104. ????????????e.printStackTrace(); ??
  105. ????????} ??
  106. ????????return?null; ??
  107. ????} ??
  108. ????/**?????*?采用标准分词器StandardAnalyzer会在创建索引的时候把存入的英文全部换成小写放在索引中?查询的时候也会将查询的关键词转为小写进行查询?????*?@return?????*/??
  109. ????public?Query?getQueryParser6(){ ??
  110. ????????QueryParser?queryParser?=?new?QueryParser("testCapital",?new?StandardAnalyzer()); ??
  111. ????????try?{ ??
  112. ????????????return?queryParser.parse("hellOwangzi"); ??
  113. ????????}?catch?(Exception?e)?{ ??
  114. ????????????e.printStackTrace(); ??
  115. ????????} ??
  116. ????????return?null; ??
  117. ????} ??
  118. ???? ??
  119. ????/**?????*?采用标准分词器StandardAnalyzer会在创建索引的时候把存入的and?or等关键字过滤掉?所以在查询的时候怎么也查不到?????*?@return?????*/??
  120. ????public?Query?getQueryParser7(){ ??
  121. ????????QueryParser?queryParser?=?new?QueryParser("testAndOr",?new?StandardAnalyzer()); ??
  122. ????????try?{ ??
  123. ????????????//return?queryParser.parse("and");return?queryParser.parse("test"); ??
  124. ????????}?catch?(Exception?e)?{ ??
  125. ????????????e.printStackTrace(); ??
  126. ????????} ??
  127. ????????return?null; ??
  128. ????} ??
  129. ????/**?????*?创建索引?????*?@param?path?????*/??
  130. ????public?void?createIndex(String?path){ ??
  131. ????????try?{ ??
  132. ????????????IndexWriter?writer?=?new?IndexWriter(path,new?StandardAnalyzer(),true); ??
  133. ????????????Document?docA?=?new?Document(); ??
  134. ????????????//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置//Field.Store?是否覆盖原来的索引文件,而不是重新建一个new?Field("content","搜索引擎",Field.Store.YES,Field.Index.TOKENIZED); ??
  135. ????????????//我们把列(fieldA)加到某一行(docA)中new?Field("title","你好中国",Field.Store.YES,Field.Index.TOKENIZED)); ??
  136. ????????????docA.add(new?Field("content","欢迎你llying",Field.Store.YES,Field.Index.TOKENIZED)); ??
  137. ????????????docA.add(new?Field("lastModifyTime","2008-9-17",Field.Store.YES,Field.Index.TOKENIZED)); ??
  138. ????????????docA.add(new?Field("testCapital","HelloWangzi",Field.Store.YES,Field.Index.TOKENIZED)); ??
  139. ????????????docA.add(new?Field("testAndOr","test?and",Field.Store.YES,Field.Index.TOKENIZED)); ??
  140. ???????????? ??
  141. ????????????Document?docB?=?new?Document(); ??
  142. ????????????//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置new?Field("content","创建索引",Field.Store.YES,Field.Index.TOKENIZED); ??
  143. ????????????//我们把列(fieldA)加到某一行(docA)中new?Field("title","你好世界",Field.Store.YES,Field.Index.TOKENIZED)); ??
  144. ????????????docB.add(new?Field("content","欢迎加入jee高级开发群46176507",Field.Store.YES,Field.Index.TOKENIZED)); ??
  145. ????????????docB.add(new?Field("lastModifyTime","2008-9-6",Field.Store.YES,Field.Index.TOKENIZED)); ??
  146. ????????????docB.add(new?Field("testCapital","hellowangZi",Field.Store.YES,Field.Index.TOKENIZED)); ??
  147. ????????????docB.add(new?Field("testAndOr","test?or",Field.Store.YES,Field.Index.TOKENIZED)); ??
  148. ???????????? ??
  149. ????????????writer.addDocument(docA); ??
  150. ????????????writer.addDocument(docB); ??
  151. ???????????? ??
  152. ????????????//如果对海量数据进行创建索引的时候,需要对索引进行优化,以便提高速度//跟数据库类似,打开一个连接,使用完后,要关闭它catch?(CorruptIndexException?e)?{ ??
  153. ????????????e.printStackTrace(); ??
  154. ????????}?catch?(LockObtainFailedException?e)?{ ??
  155. ????????????e.printStackTrace(); ??
  156. ????????}?catch?(IOException?e)?{ ??
  157. ????????????e.printStackTrace(); ??
  158. ????????} ??
  159. ????} ??
  160. ??
  161. }??

热点排行