第二个例子(完整)

来源:互联网 发布:自学seo视频教程 编辑:程序博客网 时间:2024/06/11 11:26

 此例子经过修改已经可以使用:

 

package com.test.search;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.Token;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.compass.core.util.reader.StringReader;
import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

public class LuceneTest {
 private static String indexFilePath = "e://lucene//";

 //private static String indexFilePathClass = "e://lucene//class//";
 //private static String indexFilePathSchool = "e://lucene//school";

 public LuceneTest(String filepath) {
  this.indexFilePath = filepath;
 }

 public static void main(String[] args) {
  try {   
   LuceneTest luceneTest = new LuceneTest(indexFilePath);
   //luceneTest.initIndex();// 初始化索引
   
   //luceneTest.search("user", "sdw"); //需要解决分词问题,目前只支持整词检索// 调用search方法进行检索,填入想检索的文字 多个词可用空格分开,
   //luceneTest.search("class","1");// 调用search方法进行检索,填入想检索的文字 多个词可用空格分开
   luceneTest.search("school","大", 1, 2);// 调用search方法进行检索,填入想检索的文字 多个词可用空格分开

   // 删除一条记录
   //luceneTest.deleteIndex("wangduo","user");

   // 增加一条记录
   //luceneTest.addIndex("wangduo", "is a sb", "http://wangduo", "user");

   // 更新一条记录
   //luceneTest.updateIndex("wangduo", "is very good!!!!!!", "http://wenfujun", "user");

   System.out.println("检索完毕!");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 执行查询
  * @param searchType--查询类别(学生,班级,学校)
  * @param serchString--输入的查询关键字
  * @param startindex--前台可利用此参数来控制分页查询
  * @param maxpage--每页显示多少条结果记录
  * @throws Exception
  */
 public void search(String searchType, String serchWord, int startindex, int maxpage) throws Exception {
  Directory directory = new SimpleFSDirectory(new File(indexFilePath
    + File.separator + searchType));

  /* 创建一个搜索,搜索刚才创建的e://lucene//目录下的索引 */
  IndexSearcher indexSearcher = new IndexSearcher(directory, true);
  
  // 相似度评估器
  indexSearcher.setSimilarity(new IKSimilarity());
  
  /* 在这里我们只需要搜索一个目录 */
  IndexSearcher indexSearchers[] = { indexSearcher };
  
  /* Multisearcher表示多目录搜索,在这里我们只有一个目录 */
  Searcher searcher = new MultiSearcher(indexSearchers);

  /* 我们需要搜索两个域"ArticleTitle", "ArticleText"里面的内容 */
  String[] fields = { "title", "content", "url" };

  /* 这里只搜索title域,只有title域中有符合条件的数据才做为搜索结果集中的一部分,MUST表示and,MUST_NOT表示not, SHOULD表示or*/
  BooleanClause.Occur[] clauses = { BooleanClause.Occur.MUST,
    BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.MUST_NOT };

  /*
   * MultiFieldQueryParser表示多个域解析,
   * 同时可以解析含空格的字符串,如果我们搜索"老板",根据前面的索引,显然搜到的是第二份文件
   */
  Query query = new WildcardQuery(new Term("title","*"+serchWord+"*"));

  /* 把搜索出来的所有文件打印出来 */
  int thispage = maxpage;
  /* 开始搜索 */
  TopDocs topDocs = searcher.search(query, maxpage);


  
  System.out.println(" 命中: " + topDocs.totalHits);
  // 输出结果    
  
  if ((startindex + maxpage) > topDocs.totalHits) {
   System.out.println("topDocs.totalHits=="+topDocs.totalHits);
   thispage = topDocs.totalHits - startindex; // set the max index to maxpage or last
  }

  for (int i = startindex; i < (thispage + startindex); i++) {
   Document doc = searcher.doc(topDocs.scoreDocs[i].doc); // get the next
   // document
   String doctitle = doc.get("title"); // get its title
   String content = doc.get("content"); // get its path field
   String url = doc.get("url"); // get its url field
   System.out.println(doctitle);
   System.out.println(content);
   System.out.println(url);
  }

  /* 关闭 */
  searcher.close();
 }

 public void closeIndex(IndexWriter indexWriter) {
  try {
   indexWriter.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 从数据读取数据并初始化索引,在系统第一次启动时用,以后只对其做更新、删除等操作,
  * 系统再重启后也不需要,因为索引已经以文件的形式存储在的索引文件中,永久有效
  *
  * @return
  */
 public String initIndex() {

        String title = null;
        String content = null;
        String url = null;

  String sql_user = "select * from user";
  String sql_class = "select * from class";
  String sql_school = "select * from school";
  
   try
      {
        Connection conn;
        Statement stmt;
        ResultSet res;
        //加载Connector/J驱动
        //这一句也可写为:Class.forName("com.mysql.jdbc.Driver");
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/srx","root", "mysql");
        //执行SQL语句
        stmt = conn.createStatement();
       
        //初始化学生
        res = stmt.executeQuery(sql_user);
        while (res.next())
        {
           title = res.getString("name");
           content = res.getString("realname");
           url = "http://localhost:"+title;
           addIndex(title, content, url, "user");
        }

        //初始化班级
        res = stmt.executeQuery(sql_class);
        while (res.next())
        {
           title = res.getString("class_name");
           content = res.getString("class_content");
           url = "http://localhost:"+title;
           addIndex(title, content, url, "class");
        }
       
        //初始化学校
        res = stmt.executeQuery(sql_school);
        while (res.next())
        {
           title = res.getString("school_name");
           content = res.getString("school_content");
           url = "http://localhost:"+title;
           addIndex(title, content, url, "school");
        }
       
       
        res.close();

      }
      catch (Exception ex)
      {
        System.out.println("Error : " + ex.toString());
      }
  
  
  return null;
 }

 /**
  * 删除索引
  *
  * @return
  */
 public boolean deleteIndex(String title, String indexType) {
  boolean result = false;
  try {
   Directory directory = new SimpleFSDirectory(new File(indexFilePath + File.separator + indexType));
   //this.getWriter(indexFilePath + File.separator + indexType).deleteDocuments(new WildcardQuery(new Term("title",title)));// new Term("title",title));
   IndexReader reader = IndexReader.open(directory, false);
   int n = reader.deleteDocuments(new Term("title",title));
   if(n>0){
    result = true;
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
 }

 /**
  * 更新(修改)索引
  *
  * @return
  */
 public boolean updateIndex(String title, String content, String url, String indexType) {
  boolean result = false;
  if(this.deleteIndex(title, indexType)){
   if(this.addIndex(title, content, url, indexType)){
    result = true;
   }
  }
  return result;
 }

 /**
  * 添加索引
  * @param title
  * @param content
  * @param url
  * @param indexType--索引类别(user,class,school)
  * @return
  */
 public boolean addIndex(String title, String content, String url, String indexType) {
  try {
   // 增加document到索引去
   // document对象,相当于数据库中一条记录
   Document document = new Document();
   // Field对象,相当于数据库中字段
   Field FiledTitle = new Field("title", title, Field.Store.YES,
     Field.Index.ANALYZED);// Field.Index.ANALYZED 这就能进行索引了,
   // 如果设置为NO的话就不能检索
   Field FiledContent = new Field("content", content, Field.Store.YES,
     Field.Index.ANALYZED);
   Field FieldBody = new Field("url", url, Field.Store.YES,
     Field.Index.NO);
   document.add(FieldBody);
   document.add(FiledContent);
   document.add(FiledTitle);
   IndexWriter indexWriter = this.getWriter(indexFilePath + File.separator + indexType);
   indexWriter.addDocument(document);
   
   indexWriter.optimize();
   indexWriter.close();
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
  return true;
 }

 /**
  * 判断索引是否存在
  * @param indexDir
  * @return
  */
 public static boolean indexExist(String indexDir) {
  boolean isExt = true;
  try {
   isExt = IndexReader.indexExists(new SimpleFSDirectory(new File(
     indexDir)));
  } catch (Exception e) {
   e.printStackTrace();
  }
  return isExt;
 }
 
 
 /**
  * 获得IndexWriter
  * @param indexFilePath--索引目录,在调用此方法时已经指定类型是student、class或school
  * @return
  * @throws CorruptIndexException
  * @throws LockObtainFailedException
  * @throws IOException
  */
 private IndexWriter getWriter(String indexFilePath)
   throws CorruptIndexException, LockObtainFailedException,
   IOException {
  //Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
  //Analyzer analyzer = new ChineseAnalyzer();
  Analyzer analyzer = new IKAnalyzer(false);
  boolean append = true;
  File file = new File(indexFilePath + File.separator + "segments.gen");
  if (file.exists()) {
   append = false;
  }
  return new IndexWriter(new SimpleFSDirectory(new File(indexFilePath)),
    analyzer, append, new IndexWriter.MaxFieldLength(999999999));
 }

 

}

原创粉丝点击