Lucene的例子(hibernate实现)

来源:互联网 发布:网络摄影头怎么安装 编辑:程序博客网 时间:2024/05/21 19:12

前期准备:

(一):

1.lucene 各个版本jar包:http://blog.csdn.net/u013341688/article/details/50634443

2.hibernate相关的jar包

3.oracle数据库连接jar包

以下是我的本地Jar包:



(二):

1.建表Employee_info,构建实体类及其映射文件

实体类里包含empid,empname,email等属性,并有get set方法(略)

不多说了,大图奉上:



(三):

测试类LuceneTest代码:

/** * 基于lucene2.0 * @author zmt * */public class LuceneTest {private static IndexWriter indexWriter = null;private static Analyzer analyzer = null;private static String indexAddr = "D:\\indexAddr";//在本地建立索引的路径/** * 从数据库中查数据 * @param sql * @return * @throws SQLException */public static ArrayList<EmployeeInfo> getDate(String sql) throws SQLException {  ArrayList<EmployeeInfo> EmployeeInfo = new ArrayList<EmployeeInfo>();  Session session = HibernateSessionFactory.getSession();  Connection conn = session.connection();  PreparedStatement  ps = conn.prepareStatement(sql);  ResultSet rs = ps.executeQuery();  while (rs.next()) {   // 把数据库里的数据取出来,封装到List集合里   EmployeeInfo i = new EmployeeInfo();   i.setEmpId(rs.getString("EMPLOYEE_ID"));     // 对应你的员工表里的id   i.setEmpName(rs.getString("EMPLOYEE_NAME")); // 取表里的员工姓名   i.setEmail(rs.getString("email"));           // 邮件   EmployeeInfo.add(i);  }   return EmployeeInfo;}/** * 建立索引存放的位置 */public static void createFileIndex() {  try {   /* 这里放索引文件的位置 */   File indexDir = new File(indexAddr); // 存放 检索文件的路径   if (!indexDir.exists()) {       indexDir.mkdirs();   }      /*创建标准文本分析器, 标准的是可以支持的中文的*/   Analyzer luceneAnalyzer = new StandardAnalyzer();      /* 可以说是创建一个新的写入工具    第一个参数是要索引建立在哪个目录里    第二个参数是新建一个文本分析器,这里用的是标准的大家也可以自己写一个    第三个参数如果是true,在建立索引之前先将原先的目录清空,false代表在原来基础上进行操作*/   indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true);      indexWriter.setMaxFieldLength(100000);   indexWriter.optimize();  } catch (IOException e) {   System.out.println("建立索引失败!!!");   e.printStackTrace();  }} /**  * 添加数据到索引里去  * @param empid  * @param email  * @param empname  * @return  */ public static String createIndex(String empid,String empname,String email ) {  try {      /*document对象,相当于数据库中一条记录*/   Document document = new Document();     /*Field对象,相当于数据库中字段*/   Field Filedempid = new Field("empid", empid, Field.Store.YES,     Field.Index.TOKENIZED);// Field.Index.TOKENIZED 表示域里面的内容将被索引, 如果设置为NO的话就不能检索   Field Filedempname = new Field("empname", empname, Field.Store.YES,     Field.Index.TOKENIZED);   Field FieldBody = new Field("email", email, Field.Store.YES,     Field.Index.NO);    document.add(FieldBody);   document.add(Filedempname);   document.add(Filedempid);      /*增加document到索引去,*/   indexWriter.addDocument(document);  } catch (IOException e) {        e.printStackTrace();         return "建立索引失败!!!!!!";  }    return "建立索引成功!!!!!!!"; }  /**  * 关闭索引  * @throws IOException  */ public static void close() throws IOException { indexWriter.close();    // 这里非常的重要,不关闭直接导致你的索引创建不成功 }  /**  * 查询索引的方法  * @param info  * @return  * @throws IOException  * @throws org.apache.lucene.queryParser.ParseException  */ public static ArrayList<Document> getQueryDate(String info)   throws IOException,ParseException {    ArrayList<Document> doc = new ArrayList<Document>();  String queryString = info;  IndexSearcher searcher = new IndexSearcher(indexAddr);// 一定要跟你建索引的位置 一致  analyzer = new StandardAnalyzer();  Query query = null;    if (searcher != null) {   /*合并你搜索的字段, 增强你的搜索能力!!*/   BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD };   /*这里就是在两个范围内进行收索 , 不过这些索引的字段必须要在添加数据到索引的时候设置它*/   query = MultiFieldQueryParser.parse(queryString, new String[] {"empid", "empname" }, clauses, analyzer);    /*设置返回的最大数目,就返回前5条*/   TopDocCollector collector = new TopDocCollector(5);      searcher.search(query, collector);   ScoreDoc[] hits1 = collector.topDocs().scoreDocs;   /*返回的结果是一个数组*/   if (hits1.length > 0) {    for (int i = 0; i < hits1.length; i++) {     Document doc1 = searcher.doc(hits1[i].doc);      /*这是从这个返回的数组里面迭代每一个数据,  它的值是Document*/     doc.add(doc1);     System.out.println("empid:"+doc1.get("empid"));     System.out.println("empname:"+doc1.get("empname"));     System.out.println("email:"+doc1.get("email"));    }   } else {   System.out.println("没有数据");   }  }else {System.out.println("找不到索引位置");}     return doc; }    public static void main(String[] args) throws SQLException, IOException, ParseException {  //1.装载数据,添加索引 String sql = "select * from EMPLOYEE_INFO"; createFileIndex(); ArrayList<EmployeeInfo> empInfo = getDate(sql); for(EmployeeInfo info:empInfo){ createIndex(info.getEmpId(), info.getEmpName(), (info.getEmail())==null?"":info.getEmail()); } close();   //2.查找工号为137576的信息 long startTime = System.currentTimeMillis(); ArrayList<Document> doc =  getQueryDate("137576  137566"); long endTime = System.currentTimeMillis();  System.out.println("查找完毕。。。耗时:"+((endTime-startTime)/1000)+"秒");}}


//-----------------------------------------------------------------------

运行结果:




1 0