报课、招生咨询电话:010-51268840/41

首页 > 计算机考试 > JAVA考试认证 >
→论坛登陆 用户名  密码  
java之lucene初探

作者: 发布时间:2008-07-22 10:13:19 来源:育路计算机考试频道
 网上流行的lucene使用方法大部分是lucene1.4左右,而现在的lucene已经到了2.x,以下是lucene2.2.0的事例

  1.像往常的框架一样,自然需要将其所需的jar包导入

  导入lucene-core-2.2.0.jar(最新的jar)

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

  2.创建索引(Index)

  public void createIndex(){

  File indexDir=new File("f:/lucene/"); //将索引写入到"f:/lucene/"下;

  long start=new Date().getTime();

  StandardAnalyzer sa=new StandardAnalyzer(); //创建标准分析器

  try {

  IndexWriter indexWriter=new IndexWriter(indexDir,sa,true); //需要Index写入器,(创建)

  indexWriter.setMergeFactor(100);

  indexWriter.setMaxMergeDocs(100);

  indexWriter.setMaxFieldLength(5000);

  while(rs.next()){

  Document doc=new Document(); //创建文档(document)对象,rs是recordset对象,事先已经从数据库取出!

  doc.add(new Field("productName",rs.getString("productName"),Field.Store.YES,Field.Index.TOKENIZED)); //为document添加好field

  doc.add(new Field("unitprice",rs.getString("unitprice"),Field.Store.YES,Field.Index.TOKENIZED));

  indexWriter.addDocument(doc); //别忘记,将document加到写入器indexWriter

  }

  indexWriter.optimize(); //进行优化

  indexWriter.close();//关闭

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (LockObtainFailedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  long end=new Date().getTime();

  System.out.println("索引创建完毕,用时:"+(end-start)+"毫秒");

  }

  //----------------------------------- 3.创建检索方法(searcher)

  public Hits searcher(String queryString){

  Hits hits=null;

  try {

  IndexSearcher indexSearcher=new IndexSearcher("f:/lucene/"); //创建从"f:/lucene"检索数据的检索器

  String[] fields={"productName","unitprice"};

  QueryParser queryParser=new MultiFieldQueryParser(fields,new StandardAnalyzer()); //创建查询分析器

  Query query= queryParser.parse(queryString); //查询分析器解析 查询条件

  hits=indexSearcher.search(query);//检索器检索出所有符合条件的数据,封成hits返回.

  return hits;

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (ParseException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  return null;

  }

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

  4.调用 检索,开始 工作

  public static void main(String[] args) {

  Lucene lu= new Lucene();

  lu.getResultSet();

  lu.createIndex();

  Hits hits= lu.searcher("tofu");

  for(int i=0;i<hits.length();i++){

  try {

  Document doc=hits.doc(i);

  System.out.print(doc.get("productName"));

  System.out.print("|");

  System.out.println(doc.get("unitprice"));

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

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

  一下为全部的java代码;

  package com.sk.dome;

  import java.io.File;

  import java.io.IOException;

  import java.sql.Connection;

  import java.sql.DriverManager;

  import java.sql.PreparedStatement;

  import java.sql.ResultSetimport java.sql.SQLException;

  import java.util.Date;

  import java.util.List;

  import org.apache.lucene.analysis.standard.StandardAnalyzer;

  import org.apache.lucene.document.Document;

  import org.apache.lucene.document.Field;

  import org.apache.lucene.index.CorruptIndexException;

  import org.apache.lucene.index.IndexWriter;

  import org.apache.lucene.queryParser.MultiFieldQueryParser;

  import org.apache.lucene.queryParser.ParseException;

  import org.apache.lucene.queryParser.QueryParser;

  import org.apache.lucene.search.Hits;

  import org.apache.lucene.search.IndexSearcher;

  import org.apache.lucene.search.Query;

  import org.apache.lucene.store.LockObtainFailedException;

  public class Lucene {

  private ResultSet rs=null;

  private Connection conn;

  public ResultSet getResultSet(){

  try {

  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

  conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=northwind;user=sa;password=");

  PreparedStatement ps=conn.prepareStatement("select productName,Unitprice from products");

  rs=ps.executeQuery();

  return rs;

  } catch (ClassNotFoundException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  return null;

  }

  public void createIndex(){

  File indexDir=new File("f:/lucene/")

  long start=new Date().getTime();

  StandardAnalyzer sa=new StandardAnalyzer();

  try {

  IndexWriter indexWriter=new IndexWriter(indexDir,sa,true);

  indexWriter.setMergeFactor(100);

  indexWriter.setMaxMergeDocs(100);

  indexWriter.setMaxFieldLength(5000);

  while(rs.next()){

  Document doc=new Document();

  doc.add(new Field("productName",rs.getString("productName"),Field.Store.YES,Field.Index.TOKENIZED));

  doc.add(new Field("unitprice",rs.getString("unitprice"),Field.Store.YES,Field.Index.TOKENIZED));

  indexWriter.addDocument(doc);

  }

  indexWriter.optimize();

  indexWriter.close();

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (LockObtainFailedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } long end=new Date().getTime();

  System.out.println("索引创建完毕,用时:"+(end-start)+"毫秒");

  }

  public Hits searcher(String queryString){

  Hits hits=null;

  try {

  IndexSearcher indexSearcher=new IndexSearcher("f:/lucene/");

  String[] fields={"productName","unitprice"};

  QueryParser queryParser=new MultiFieldQueryParser(fields,new StandardAnalyzer());

  Query query= queryParser.parse(queryString);

  hits=indexSearcher.search(query);

  return hits;

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (ParseException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  return null;

  }

  public static void main(String[] args) {

  Lucene lu= new Lucene();

  lu.getResultSet();

  lu.createIndex();

  Hits hits= lu.searcher("tofu");

  for(int i=0;i<hits.length();i++){

  try {

  Document doc=hits.doc(i);

  System.out.print(doc.get("productName"));

  System.out.print("|");

  System.out.println(doc.get("unitprice"));

  } catch (CorruptIndexException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

  }

    育路网
 
 
                                        
评论】【加入收藏夹】【 】【打印】【关闭
育路网2007年夏令营联展
 更多有关新闻:
 
·[考试动态2008年医师资格考试网上报名须知 ·[考试动态2008年医师资格考试3月10日开始网
·[考试动态卫生部医师资格考试委员会公告 ·[考试动态2008年医师考试于9月20至21进行
·[考试动态关于北京考区2008年度国家医师资 ·[考试动态崇文区卫生局关于北京考区2008年
·[考试动态丰台区卫生局关于北京考区2008年 ·[考试动态2008年上海市医师资格考试(考区
·[考试动态2008年医师资格考试(天津考区) ·[考试动态惠州市卫生局关于2008医师资格考
·[考试动态无锡市关于2008年医师资格考试网 ·[考试动态关于珠海市2008年医师资格考试的
·[考试动态韶关市关于2008年医师资格考试网 ·[考试动态深圳市关于2008年医师资格考试深
·[考试动态杭州市卫生局关于2008年医师资格 ·[考试动态丽水市关于2008年医师资格考试报
发表评论
用户名: 密码:
验证码: 匿名发表
课程搜索:
选择分类:
课程关键字:
课程 学校
 2008年首都高校秋季招生
北京理工大学2008年招生简章
北京文理研修学院2008年招生简章
北京建设大学2008年招生简章
北京中山学院2008年招生简章
北京城市学院2008年招生简章
培黎职业学院2008年招生简章
北京金融学院2008年招生简章
北京吉利大学2008年招生简章
北方工商管理学院2008年招生简章
 本周推荐课程
·初中起点雅思半年脱产 ·启德雅思6分冲刺课程
·新概念1+2册慢速精讲课 ·环球新托福100分强化
·北文王长喜四级强化班 ·英语四、六级培训课程
·海文考研数学课程 ·海文考研英语课程
·北大企业管理高级研修班 ·清华企业领导人研修班
·中美高中生交换项目 ·北工大中加学院2+2本科
·劳动和社会保障部物流师 ·物业管理师职业培训
·市场营销经理国际资格 ·现场管理实务培训
清华大学留学
中法管理硕士预科班
课程咨询热线:010-51268840 51268841
 最新新闻
·2008云南公务员考试行测辅导—数学运算
·2008云南公务员考试行测辅导—数量关系
·2008云南公务员考试行测辅导—定义判断
·公务员禁兼任行业协会领导
·海淀公务员将佩身份识别卡上班
·广东人大代表:公务员加薪应实行听证并纳入人
·延平公务员需普通话测试
·2008云南公务员考试行测辅导—定义判断
·北京机动车排管中心2008年招考公务员公告
·鞍山232个公务员岗位公开招考
 育路社区            进入>>
 
学员报名服务中心: 北京北三环西路32号恒润中心1806(交通位置图
咨询电话:北京- 010-51268840/41 传真:010-51418040 上海-021-64392659、64397431
育路网-中国新锐教育社区: 北京站 | 上海站 | 郑州站| 武汉站
本站法律顾问: 邱清荣律师
北京育路互联科技有限公司版权所有 | 京ICP备05012189号