[Java] lucene3.0查询处理 →→→→→进入此内容的聊天室

来自 , 2021-03-17, 写在 Java, 查看 130 次.
URL http://www.code666.cn/view/3341f6f0
  1. package luceneQuery;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.util.Date;
  7.  
  8. import org.apache.lucene.analysis.Analyzer;
  9. import org.apache.lucene.analysis.SimpleAnalyzer;
  10. import org.apache.lucene.document.Document;
  11. import org.apache.lucene.document.Field;
  12. import org.apache.lucene.index.IndexWriter;
  13. import org.apache.lucene.index.Term;
  14. import org.apache.lucene.search.BooleanClause;
  15. import org.apache.lucene.search.BooleanQuery;
  16. import org.apache.lucene.search.FuzzyQuery;
  17. import org.apache.lucene.search.IndexSearcher;
  18. import org.apache.lucene.search.PhraseQuery;
  19. import org.apache.lucene.search.PrefixQuery;
  20. import org.apache.lucene.search.ScoreDoc;
  21. import org.apache.lucene.search.TermQuery;
  22. import org.apache.lucene.search.TermRangeQuery;
  23. import org.apache.lucene.search.WildcardQuery;
  24. import org.apache.lucene.search.spans.SpanFirstQuery;
  25. import org.apache.lucene.search.spans.SpanNearQuery;
  26. import org.apache.lucene.search.spans.SpanNotQuery;
  27. import org.apache.lucene.search.spans.SpanOrQuery;
  28. import org.apache.lucene.search.spans.SpanQuery;
  29. import org.apache.lucene.search.spans.SpanTermQuery;
  30. import org.apache.lucene.store.Directory;
  31. import org.apache.lucene.store.FSDirectory;
  32. import org.apache.lucene.store.RAMDirectory;
  33.  
  34. public class QueryTest {
  35.  
  36. static String sIndex_Path="E:/index";
  37. static String sText_path="E:/textbook";
  38. static protected String[] keywords = {"001","002","003","004","005"};
  39. static protected String[] textdetail = {"记录 一","记录 二","记录 三","一 2345 记录","记录 新 一"};
  40. static File fIndex_Path=new File(sIndex_Path);
  41. /**===========================================================
  42. * 名称:IndexBuilder
  43. * 功能:构造磁盘索引,添加内容到指定目录,为后继检索查询做好准备
  44. =============================================================**/
  45. public static void IndexBuilder(){
  46.    try{
  47.     Date start = new Date();
  48.     File f=new File(sText_path);
  49.     File[] list=f.listFiles();
  50.     File file2 = new File(sIndex_Path);
  51.     //创建磁盘索引目录
  52.     Directory dir = FSDirectory.open(file2);
  53.     Directory ramdir = new RAMDirectory();
  54.     Analyzer TextAnalyzer = new SimpleAnalyzer();
  55.     //创建磁盘索引
  56.     IndexWriter TextIndex = new IndexWriter(dir, TextAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
  57.     //创建内存索引
  58.     IndexWriter RAMTextIndex = new IndexWriter(ramdir,TextAnalyzer,true, IndexWriter.MaxFieldLength.LIMITED);
  59.     for(int i=0;i<list.length;i++){
  60.      Document document = new Document();
  61.      Field field_name = new Field("name", list[1].getName(),
  62.        Field.Store.YES, Field.Index.NOT_ANALYZED);
  63.      document.add(field_name);
  64.      FileInputStream inputfile = new FileInputStream(list[i]);
  65.      int len = inputfile.available();
  66.      byte[] buffer = new byte[len];
  67.      inputfile.read(buffer);
  68.      inputfile.close();
  69.  
  70.      String contenttext = new String(buffer);
  71.      Field field_content = new Field("content", contenttext,
  72.        Field.Store.YES, Field.Index.ANALYZED);
  73.      document.add(field_content);
  74.    
  75.      Field field_size = new Field("size",String.valueOf(len),Field.Store.YES,Field.Index.NOT_ANALYZED);
  76.      document.add(field_size);
  77.      TextIndex.addDocument(document);
  78.      TextIndex.optimize();
  79.     }
  80.       //关闭磁盘索引
  81.       TextIndex.close();
  82.       Date end = new Date();
  83.       long tm_index = end.getTime()-start.getTime();
  84.       System.out.print("Total Time:(ms)");
  85.       System.out.println(tm_index);
  86.    }catch(IOException e){
  87.     e.printStackTrace();
  88.    }
  89.    System.out.println("index Sccess");
  90. }
  91. /**===================================================================
  92. *名称:LuceneTermQuery
  93. *功能:构造检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  94. ===================================================================**/
  95. public static void LuceneTermQuery(String word){
  96.    try{
  97.     Directory Index_Dir=FSDirectory.open(fIndex_Path);
  98.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  99.     Term t = new Term("id", "002");
  100.     TermQuery query = new TermQuery(t);
  101.     System.out.print(query.toString());
  102.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  103.     System.out.println("Search result:");
  104.     for (int i = 0; i < hits.length; i++) {
  105.      Document hitDoc = searcher.doc(hits[i].doc);
  106.         System.out.println(hitDoc.get("fieldname"));
  107.     }
  108.    }catch(IOException e){
  109.     e.printStackTrace();
  110.    }
  111.    System.out.println("Search Success");
  112. }
  113. /**===================================================================
  114. *名称:LuceneRangeQuery
  115. *功能:构造范围检索查询器,对指定的索引进行查询,找到指定的文档,并输
  116. ===================================================================**/
  117. public static void LuceneRangeQuery(String lowerTerm, String upperTerm){
  118.    try{
  119.     Directory Index_Dir=FSDirectory.open(fIndex_Path);
  120.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  121.     TermRangeQuery query = new TermRangeQuery("numval",lowerTerm,upperTerm,true,true);
  122.     System.out.print(query.toString());
  123.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  124.     System.out.println("Search result:");
  125.     for (int i = 0; i < hits.length; i++) {
  126.      Document hitDoc = searcher.doc(hits[i].doc);
  127.         System.out.println(hitDoc.get("fieldname"));
  128.     }
  129.    }catch(IOException e){
  130.     e.printStackTrace();
  131.    }
  132.    System.out.println("Search Success");
  133. }
  134. /**=========================================================================
  135. *名称:LuceneBooleanQuery
  136. *功能:构造布尔检索查询器,对指定的索引进行查询,找到指定的值,并输出相应的结果
  137. =========================================================================**/
  138. public static void LuceneBooleanQuery(){
  139.    try {
  140.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  141.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  142.     Term term1 = new Term("content","记录");
  143.     Term term2 = new Term("content","二");
  144.     TermQuery query1 = new TermQuery(term1);
  145.     TermQuery query2 = new TermQuery(term2);
  146.     BooleanQuery query = new BooleanQuery();
  147.     query.add(query1,BooleanClause.Occur.MUST);
  148.     query.add(query2,BooleanClause.Occur.MUST);
  149.     System.out.println(query.toString());
  150.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  151.     System.out.println("Search result:");
  152.     for (int i = 0; i < hits.length; i++) {
  153.      Document hitDoc = searcher.doc(hits[i].doc);
  154.         System.out.println(hitDoc.get("fieldname"));
  155.     }
  156.    } catch (IOException e) {
  157.     e.printStackTrace();
  158.    }
  159.    System.out.println("Search Success");
  160. }
  161. /**=========================================================================
  162. * 名称:LucenePrefixQuery
  163. * 功能:构造前缀检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  164. ==========================================================================*/
  165. public static void LucenePrefixQuery(String word){
  166.    try {
  167.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  168.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  169.     Term term = new Term("content",word);
  170.     PrefixQuery query = new PrefixQuery(term);
  171.     System.out.println(query.toString());
  172.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  173.     System.out.println("Search result:");
  174.     for (int i = 0; i < hits.length; i++) {
  175.      Document hitDoc = searcher.doc(hits[i].doc);
  176.         System.out.println(hitDoc.get("fieldname"));
  177.     }
  178.    } catch (IOException e) {
  179.     e.printStackTrace();
  180.    }
  181.    System.out.println("Search Success");
  182. }
  183. /**=========================================================================
  184. * 名称:LucenePhraseQuery
  185. * 功能:构造短语检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  186. ==========================================================================*/
  187. public static void LucenePhraseQuery(String word1, String word2){
  188.    try {
  189.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  190.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  191.     Term term1 = new Term("content",word1);
  192.     Term term2 = new Term("content",word2);
  193.     PhraseQuery query = new PhraseQuery();
  194.     query.add(term1);
  195.     query.add(term2);
  196.     System.out.println(query.toString());
  197.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  198.     System.out.println("Search result:");
  199.     for (int i = 0; i < hits.length; i++) {
  200.      Document hitDoc = searcher.doc(hits[i].doc);
  201.         System.out.println(hitDoc.get("fieldname"));
  202.     }
  203.    } catch (IOException e) {
  204.     e.printStackTrace();
  205.    }
  206.    System.out.println("Search Success");
  207. }
  208. /**=========================================================================
  209. * 名称:LuceneFuzzyQuery
  210. * 功能:构造模糊检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  211. ==========================================================================*/
  212. public static void LuceneFuzzyQuery(String word){
  213.    try {
  214.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  215.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  216.     Term term = new Term("content",word);
  217.     FuzzyQuery query = new FuzzyQuery(term);
  218.     System.out.println(query.toString());
  219.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  220.     System.out.println("Search result:");
  221.     for (int i = 0; i < hits.length; i++) {
  222.      Document hitDoc = searcher.doc(hits[i].doc);
  223.         System.out.println(hitDoc.get("fieldname"));
  224.     }
  225.    } catch (IOException e) {
  226.     e.printStackTrace();
  227.    }
  228.    System.out.println("Search Success");
  229. }
  230. /**=========================================================================
  231. * 名称:LuceneWildcardQuery
  232. * 功能:构造通配符检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  233. ==========================================================================*/
  234. public static void LuceneWildcardQuery(String word){
  235.    try {
  236.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  237.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  238.     Term term = new Term("content",word);
  239.     WildcardQuery query = new WildcardQuery(term);
  240.     System.out.println(query.toString());
  241.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  242.     System.out.println("Search result:");
  243.     for (int i = 0; i < hits.length; i++) {
  244.      Document hitDoc = searcher.doc(hits[i].doc);
  245.         System.out.println(hitDoc.get("fieldname"));
  246.     }
  247.    } catch (IOException e) {
  248.     e.printStackTrace();
  249.    }
  250.    System.out.println("Search Success");
  251. }
  252. /**=========================================================================
  253. * 名称:LuceneSpanFirstQuery
  254. * 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  255. ==========================================================================*/
  256. public static void LuceneSpanFirstQuery(String word){
  257.    try {
  258.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  259.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  260.     Term term = new Term("content",word);
  261.     SpanTermQuery query = new SpanTermQuery(term);
  262.     SpanFirstQuery firstquery = new SpanFirstQuery(query,2);
  263.     System.out.println(firstquery.toString());
  264.     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  265.     System.out.println("Search result:");
  266.     for (int i = 0; i < hits.length; i++) {
  267.      Document hitDoc = searcher.doc(hits[i].doc);
  268.         System.out.println(hitDoc.get("fieldname"));
  269.     }
  270.    } catch (IOException e) {
  271.     e.printStackTrace();
  272.    }
  273.    System.out.println("Search Success");
  274. }
  275. /**=========================================================================
  276. * 名称:LuceneSpanNearQuery
  277. * 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  278. ==========================================================================*/
  279. public static void LuceneSpanNearQuery(String word1,String word2,String word3){
  280.    try {
  281.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  282.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  283.     Term term1 = new Term("content",word1);
  284.     Term term2 = new Term("content",word2);
  285.     Term term3 = new Term("content",word3);
  286.     SpanTermQuery query1 = new SpanTermQuery(term1);
  287.     SpanTermQuery query2 = new SpanTermQuery(term2);
  288.     SpanTermQuery query3 = new SpanTermQuery(term3);
  289.     SpanQuery[] queryarray = new SpanQuery[]{query1,query2,query3};
  290.     SpanNearQuery nearquery = new SpanNearQuery(queryarray,1,true);
  291.     System.out.println(nearquery.toString());
  292.     ScoreDoc[] hits = searcher.search(nearquery, null, 1000).scoreDocs;
  293.     System.out.println("Search result:");
  294.     for (int i = 0; i < hits.length; i++) {
  295.      Document hitDoc = searcher.doc(hits[i].doc);
  296.         System.out.println(hitDoc.get("fieldname"));
  297.     }
  298.    } catch (IOException e) {
  299.     e.printStackTrace();
  300.    }
  301.    System.out.println("Search Success");
  302. }
  303. /**=========================================================================
  304. * 名称:LuceneSpanNotQuery
  305. * 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  306. ==========================================================================*/
  307. public static void LuceneSpanNotQuery(String word1,String word2,String word3){
  308.    try {
  309.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  310.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  311.     Term term1 = new Term("content",word1);
  312.     Term term2 = new Term("content",word2);
  313.     Term term3 = new Term("content",word3);
  314.     SpanTermQuery query1 = new SpanTermQuery(term1);
  315.     SpanTermQuery query2 = new SpanTermQuery(term2);
  316.     SpanTermQuery query3 = new SpanTermQuery(term3);
  317.     SpanQuery[] queryarray = new SpanQuery[]{query1,query2};
  318.     SpanNearQuery nearquery = new SpanNearQuery(queryarray,1,true);
  319.     SpanNotQuery notquery = new SpanNotQuery(nearquery,query3);
  320.     System.out.println(notquery.toString());
  321.     ScoreDoc[] hits = searcher.search(notquery, null, 1000).scoreDocs;
  322.     System.out.println("Search result:");
  323.     for (int i = 0; i < hits.length; i++) {
  324.      Document hitDoc = searcher.doc(hits[i].doc);
  325.         System.out.println(hitDoc.get("fieldname"));
  326.     }
  327.    } catch (IOException e) {
  328.     e.printStackTrace();
  329.    }
  330.    System.out.println("Search Success");
  331. }
  332. /**=========================================================================
  333. * 名称:LuceneSpanOrQuery
  334. * 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
  335. ==========================================================================*/
  336. public static void LuceneSpanOrQuery(String word1,String word2,String word3){
  337.    try {
  338.     Directory Index_Dir = FSDirectory.open(fIndex_Path);
  339.     IndexSearcher searcher = new IndexSearcher(Index_Dir);
  340.     Term term1 = new Term("content",word1);
  341.     Term term2 = new Term("content",word2);
  342.     Term term3 = new Term("content",word3);
  343.     SpanTermQuery query1 = new SpanTermQuery(term1);
  344.     SpanTermQuery query2 = new SpanTermQuery(term2);
  345.     SpanTermQuery query3 = new SpanTermQuery(term3);
  346.     SpanQuery[] queryarray1 = new SpanQuery[]{query1,query2};
  347.     SpanQuery[] queryarray2 = new SpanQuery[]{query2,query3};
  348.     SpanNearQuery nearquery1 = new SpanNearQuery(queryarray1,1,true);
  349.     SpanNearQuery nearquery2 = new SpanNearQuery(queryarray2,1,true);
  350.     SpanOrQuery orquery = new SpanOrQuery(new SpanNearQuery[]{nearquery1,nearquery2});
  351.     System.out.println(orquery.toString());
  352.     ScoreDoc[] hits = searcher.search(orquery, null, 1000).scoreDocs;
  353.     System.out.println("Search result:");
  354.     for (int i = 0; i < hits.length; i++) {
  355.      Document hitDoc = searcher.doc(hits[i].doc);
  356.         System.out.println(hitDoc.get("fieldname"));
  357.     }
  358.    } catch (IOException e) {
  359.     e.printStackTrace();
  360.    }
  361.    System.out.println("Search Success");
  362. }
  363.  
  364. }
  365. //java/1315

回复 "lucene3.0查询处理"

这儿你可以回复上面这条便签

captcha