Luence建索、检索、中文分词

 新手,勿批!

所需架包:IKAnalyzer2012FF_u1.jar,lucene-core-4.0.0.jar,lucene-queries-4.1.0.jar,lucene-queryparser-4.1.0.jar

 

代码:

 

package org.apache.lucene.demo;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

@SuppressWarnings("unused")
public class IKAnalyzerDemo {
 String fieldName = "title";
    String fieldValue="content";
    Analyzer analyzer = new IKAnalyzer();
    /**
     * 创建索引
     * @param pathFile  索引存放位置
     */
 @SuppressWarnings("deprecation")
 public void createIndex(String pathFile){
    String text1 = "上海(Shanghai),简称“沪”或“申”,中国第一大城市,中华人民共和国直辖市之一,中国国家中心城市,中国的经济、金融中心,繁荣的国际大都市,是中国首个自贸区“中国(上海)自由贸易试验区”的所在地。";
         String text2 = "天津,简称津,中华人民共和国直辖市、中国国家中心城市、中国北方经济中心、环渤海地区经济中心、中国北方国际航运中心、中国北方国际物流中心、国际港口城市和生态城市、国际航运融资中心、中国中医药研发中心、亚太区域海洋仪...";
         String text3 = "北京是中华人民共和国的首都、直辖市和国家中心城市之一,中国的政治、文化、科教和国际交往中心,中国经济、金融的决策和管理中心,也是中华人民共和国中央人民政府和全国人民代表大会的办公所在地,位于华北平原的东北边缘,背靠燕山,有永定河流经老城西南,毗邻天津市和河北省";
         try {
          //文件生成在classpath下
             //RAMDirectory directory = new RAMDirectory();
             //Directory directory = FSDirectory.getDirectory(path);
             //索引存放文件夹  通过测试该文件夹可以持续放入索引 每建索一次 就会生成对应的文件
           Directory directory=FSDirectory.open(new File(pathFile)); 
              IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer);
              IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
              /**
               * Field.Store.YES 表示是否存储    Field.Index.ANALYZED 表示对该对象是否建索
               * 一般存储需要展示的内容,对有用的如:标题、内容进行建索,链接或者序号不用建索。
               */
              Document document1 = new Document();
              document1.add(new Field(fieldName, "中国上海", Field.Store.YES, Field.Index.ANALYZED));
              document1.add(new Field(fieldValue, text1, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document1);
             
              Document document2 = new Document();
              document2.add(new Field(fieldName, "中国天津", Field.Store.YES, Field.Index.ANALYZED));
              document2.add(new Field(fieldValue, text2, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document2);
             
              Document document3 = new Document();
              document3.add(new Field(fieldName, "北京", Field.Store.YES, Field.Index.ANALYZED));
              document3.add(new Field(fieldValue, text3, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document3);
              indexWriter.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
 }
 /**
  * 根据关键字检索    多个关键字 检索多个字段
  * @param keyName  关键字
  * @param pathFile  索引存放路径
  */
 @SuppressWarnings("deprecation")
 public void IKAnalyzerLuence(String[] keyName,String pathFile){
  try {
    //搜索
   Directory directory=FSDirectory.open(new File(pathFile));
         IndexReader indexReader = IndexReader.open(directory);
         IndexSearcher searcher = new IndexSearcher(indexReader);
         //请求关键字
         //String request = "中国经济";
         try {
          /**
           * clauses中的个数和 keyName的个数 以及new String[]{fieldName,fieldValue}中的参数个数相同
           */
          BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,BooleanClause.Occur.MUST};
       Query queryOBJ = MultiFieldQueryParser.parse(Version.LUCENE_40,keyName, new String[]{fieldName,fieldValue}, clauses, analyzer);
    //Query query = parser.parse(request);
    TopDocs topDocs = searcher.search(queryOBJ, 10);
    System.out.println("命中数:"+topDocs.totalHits);
    ScoreDoc[] docs = topDocs.scoreDocs;
    for(ScoreDoc doc : docs){
     Document d = searcher.doc(doc.doc);
     System.out.println("标题:"+d.get(fieldName));
     System.out.println("内容:"+d.get(fieldValue));
    }
   } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally{
    if(indexReader != null){
     try{
      indexReader.close();
     }catch (IOException e) {
      e.printStackTrace();
     }
    }
    if(directory != null){
     try{
      directory.close();
     }catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * 根据关键字检索    一个关键字 检索多个字段
  * @param keyName  关键字
  * @param pathFile  索引存放路径
  */
 @SuppressWarnings("deprecation")
 public void IKAnalyzerLuence2(String keyName,String pathFile){
  try {
    //搜索
   Directory directory=FSDirectory.open(new File(pathFile));
         IndexReader indexReader = IndexReader.open(directory);
         IndexSearcher searcher = new IndexSearcher(indexReader);
         //请求关键字
         //String request = "中国经济";
         try {
          /**
           * 如果为SHOULD 检索的内容中关键字可有可无,MUST:必须有,MUST_NOT:必须不含有
           */
          BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,BooleanClause.Occur.MUST};
       Query queryOBJ = MultiFieldQueryParser.parse(Version.LUCENE_40,keyName, new String[]{fieldName,fieldValue}, clauses, analyzer);
    //Query query = parser.parse(request);
    TopDocs topDocs = searcher.search(queryOBJ, 10);
    System.out.println("命中数:"+topDocs.totalHits);
    ScoreDoc[] docs = topDocs.scoreDocs;
    for(ScoreDoc doc : docs){
     Document d = searcher.doc(doc.doc);
     System.out.println("标题:"+d.get(fieldName));
     System.out.println("内容:"+d.get(fieldValue));
    }
   } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally{
    if(indexReader != null){
     try{
      indexReader.close();
     }catch (IOException e) {
      e.printStackTrace();
     }
    }
    if(directory != null){
     try{
      directory.close();
     }catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  String pathFile="D://lucene/index";
  /**
   * 创建索引
   */
  new IKAnalyzerDemo().createIndex(pathFile);
  
  /**
   * 1、多个关键字检索多个字段
   * String keyName[]={"中国","上海"};
   * new IKAnalyzerDemo().IKAnalyzerLuence(keyName, pathFile);
   */
  /**
   * 2、 一个关键字检索一个或者多个字段
   */
  String keyName="中国经济";
  new IKAnalyzerDemo().IKAnalyzerLuence2(keyName, pathFile);
   }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值