用Lucene做一个简单的Java搜索工具

时间:2007-01-22 15:42:41   来源:剑心博客  作者:  点击:次  出处:技术无忧
关键字:Lucene 简单 Java 搜索


    /**
     *
     *@paramqueryString
     *@returnHits
     *@throwsSearchException
     */
    public Hits serach(String queryString) throws SearchException {
       if (parser == null)
           thrownew SearchException("parser is null!");
       try {
           returnsearcher.search(parser.parse(queryString));
       } catch (IOException ioe) {
           thrownew SearchException(ioe.getMessage());
       } catch (ParseException pe) {
           thrownew SearchException(pe.getMessage());
       }
    }
 
    /**
     *
     *输出hits的结果,从start开始到end,不包括end
     *
     *@paramhits
     *@paramstart
     *@paramend
     *@throwsSearchException
     */
    publicstatic Hits display(Hits hits, int start, int end) throws SearchException {
       try {
           while (start < end) {
              Document doc = hits.doc(start);
              String path = doc.get("path");
              if (path != null) {
                  System.out.println((start + 1) + "- " + path);
              } else {
                  System.out.println((start + 1) + "- " + "No such path");
              }
              start++;
           }
       } catch (IOException ioe) {
           thrownew SearchException(ioe.getMessage());
       }
       return hits;
    }
main方法
    /**
     *@paramargs
     */
    publicstaticvoid main(String[] args) throws Exception {
 
       String field = "contents";
       String index = "./index";
       finalint rows_per_page = 2;
       finalchar NO = 'n';
 
       SearchJavaFiles sjf = new SearchJavaFiles(new IndexSearcher(IndexReader.open(index)));
       sjf.setParser(field, new StandardAnalyzer());
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
 
       while (true) {
           System.out.println("Query: ");
           String line = in.readLine();
           if (line == null || line.length() < 2) {
              System.out.println("eixt query");
              break;
           }
           Hits hits = sjf.serach(line);
           System.out.println("searching for " + line + " Result is ");
 
           int len = hits.length();
           int i = 0;
           if (len > 0)
              while (true) {
                  if (i + rows_per_page >= len) {
                     SearchJavaFiles.display(hits, i, len);
                     break;
                  } else {
                     SearchJavaFiles.display(hits, i, i += rows_per_page);
                     System.out.println("more y/n?");
                     line = in.readLine();
                     if (line.length() < 1 || line.charAt(0) == NO)
                         break;
                  }
              }
           else
              System.out.println("not found");
       }
    }
}
 
SearchException.java
package powerwind;
 
publicclass SearchException extends Exception {
 
    public SearchException(String message) {
       super("Throw SearchException while searching files: " + message);
    }
 
}


 
 
完善设想:
1、文件格式:
能够处理Zip文件Jar文件,索引里面的java源文件。
通过反射机制索引class类文件。
2、输入输出:
除控制台输入输出外,还可以选择从文件读取查询关键字,输出查询结果到文件。
3、用户界面:
图形界面操作,双击查询结果的某条记录可以打开相应文件。
4、性能方面
索引文件时,用缓存和多线程处理

WWW.pC51.Net


 3/3   |‹ ‹‹ 1 2 3

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面