java之lucene初探

来源:微学计算机考试频道发布时间:2008-07-22
 网上流行的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();

  }

  }

  }

  }