1、新建两个文件夹htm和index,其中htm中存放被索引的文件,index文件中存放建立的索引文件。

2、新建解析目录中所有文件的类,用来解析指定目录下的所有文件。

import java.io.File;

public class FileList {
 private static final String
SEP="/";
 private static StringBuffer sb=new
StringBuffer("");
 public static String[] getFiles(File
file){//取得file目录中所有的文件
  if(file.isDirectory()){//如果是目录则进一步迭代
   File[]
fs=file.listFiles();
   for(int
i=0;i<fs.length;i++){
    getFiles(fs[i]);
   }
  }
  else{
   sb.append(file.getPath()+SEP);//输出文件
  }
  String
s=sb.toString();
  return s.split(SEP);
 }
 public
static String[] getFiles(String path){
  File file=new
File(path);
  if(file.isDirectory()){
   File[]
fs=file.listFiles();
   for(int
i=0;i<fs.length;i++){
    getFiles(fs[i]);
   }
  }
  else{
   sb.append(file.getPath()+SEP);
  }
  String
s=sb.toString();
  return s.split(SEP);
 }
 public
static String getExt(File file){
  String
ext=file.getName();
  try
{
   ext=ext.substring(ext.lastIndexOf(".")+1);
  }
catch (Exception e)
{
   ext="";
  }
  return
ext;
 }
}

3、读取文件内容用来建立对此文件的索引,这里读取文件的所有字符,只支持文本文件,本文暂时没有对其他文档的支持,读者可以利用其他组件来解析其他复杂文档。

import java.io.BufferedReader;
import java.io.File;
import
java.io.FileReader;

public class FileText {
 public static String getText(File
file){//获得文件的内容
  StringBuffer sb=new
StringBuffer("");
  try {
   FileReader fr=new
FileReader(file);
   BufferedReader br=new
BufferedReader(fr);
   String
text=br.readLine();
   while(text!=null){
    sb.append(text);
    text=br.readLine();
   }
   br.close();
  }
catch (Exception e)
{
   sb.append("");
  }
  return
sb.toString();
 }
 public static String getText(String
filename){
  File file=new File(filename);
  String
text="";
  try
{
   text=getText(file);
  } catch (Exception e)
{
   text="";
  }
  return
text;
 }
}

4、利用Lucene建立索引文件,同时存入指定目录。

import java.io.File;
import java.io.IOException;
import
java.text.SimpleDateFormat;
import java.util.Date;
import
javax.swing.JOptionPane;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import
org.apache.lucene.document.Document;
import
org.apache.lucene.document.Field;
import
org.apache.lucene.index.IndexWriter;
import
org.apache.lucene.index.IndexWriterConfig;
import
org.apache.lucene.store.SimpleFSDirectory;
import
org.apache.lucene.util.Version;

public class LuceneIndexer {
 public void createIndex(String
filesPath,String indexPath) throws IOException{
  File path=new
File(indexPath);
  SimpleFSDirectory indexDir=new
SimpleFSDirectory(path);//读取被索引的文件目录
  CJKAnalyzer analyzer=new
CJKAnalyzer(Version.LUCENE_31);//创建一个二分法分析器
  IndexWriterConfig
conf=new IndexWriterConfig(Version.LUCENE_31,
analyzer);
  IndexWriter writer=new
IndexWriter(indexDir,conf);
  String[]
files=FileList.getFiles(filesPath);//读取被索引的文件
  for(int
i=0;i<files.length;i++){
   File f=new
File(files[i]);
   String
ext=FileList.getExt(f);
   if(ext.equalsIgnoreCase("html")||ext.equalsIgnoreCase("htm")){//只建立网页文件的索引
    Document
doc=new
Document();//新建一个索引文件用于索引当前html文件
    //filename字段
    String
filename=f.getName();
    Field field=new
Field("filename",filename,Field.Store.YES,Field.Index.ANALYZED);
    doc.add(field);
    //uri字段
    String
uri=f.getPath();
    field=new
Field("uri",uri,Field.Store.YES,Field.Index.NO);
    doc.add(field);
    //cdate字段
    Date
date=new Date(f.lastModified());
    SimpleDateFormat
sdf=new SimpleDateFormat("yyyy-MM-dd E");
    String
cdate=sdf.format(date);
    field=new
Field("cdate",cdate,Field.Store.YES,Field.Index.NO);
    doc.add(field);
    //size字段
    double
si=f.length();
    String
size="";
    if(si>1024){
     size=String.valueOf(Math.floor(si/1024))+"K";
    }
    else{
     size=String.valueOf(si)+"Bytes";
    }
    field=new
Field("size",size,Field.Store.YES,Field.Index.NO);
    doc.add(field);
    //text字段
    String
text=FileText.getText(f);
    field=new
Field("text",text,Field.Store.YES,Field.Index.ANALYZED);
    doc.add(field);
    //digest字段
    String
digest="";
    if(text.length()>200){
     digest=text.substring(0,
200);
    }
    else{
     digest=text;
    }
    field=new
Field("digest",digest,Field.Store.YES,Field.Index.ANALYZED);
    doc.add(field);
    //归入索引
    writer.addDocument(doc);
   }
  }
  writer.close();//关闭索引器
  JOptionPane.showMessageDialog(null,
"索引建立完毕!","提示", JOptionPane.INFORMATION_MESSAGE);
 }
}

5、测试

public class IndexerTest {

public static void main(String[] args) {
  //
TODO Auto-generated method stub
  LuceneIndexer indexer=new
LuceneIndexer();
  try
{
   indexer.createIndex("D:/我的文档/lucene/htm",
"D:/我的文档/lucene/index");
  } catch (IOException e)
{
   // TODO Auto-generated catch
block
   e.printStackTrace();
  }
 }

}

(转自:http://blog.sina.com.cn/s/blog_69b6a7c60100wa4n.html)

利用Lucene将被索引文件目录中的所有文件建立索引的更多相关文章

  1. solr中通过SFTP访问文件建立索引

    需求: 从oracle数据库中根据记录的文件名filename_html(多个文件以逗号隔开),文件路径path,备用文件名bakpath中获取 主机172.21.0.31上对应的html文件内容,并 ...

  2. 【mysql】mysql中单列索引、联合索引、Join联表查询建立索引 和 EXPLAIN的分析使用

    2.创建联合索引,从坐到右分别为:userid.openId.name   2. #### --------------  多表联合查询 update 2019/03/13  ------------ ...

  3. Oracle数据库中如何选择合适的索引类型 .

    索引就好象一本字典的目录.凭借字典的目录,我们可以非常迅速的找到我们所需要的条目.数据库也是如此.凭借Oracle数据库的索引,相关语句可以迅速的定位记录的位置,而不必去定位整个表. 虽然说,在表中是 ...

  4. mysql建立索引,实际工作中建立索引的示例

    1.根据业务场景建立相应的组合索引,一般是在主键,外键,常用来筛选查询的字段,按照字段之间组合的紧密程度,建立一定顺序的索引. 例如:为 t_org_exam_join_member_day  建立索 ...

  5. Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构

    Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构 1. 索引的分类1 1.1. 按照存储结构划分btree,hash,bitmap,fulltext1 1.2. 索引的类型  按查找 ...

  6. Mysql数据库建立索引的优缺点有哪些?

    索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息. 什么是索引 数据库索引好比是一本书前面的目录,能加快数据库的查询速度. 例如这样一个查询:select * ...

  7. mysql 建立索引的原则(转)

    索引查询是数据库中重要的记录查询方法,要不要进入索引以及在那些字段上建立索引都要和实际数据库系统的查询要求结合来考虑,下面给出实际中的一些通用的原则: 1. 在经常用作过滤器的字段上建立索引: 2. ...

  8. mysql 建立索引场合及索引使用

    索引建立场合: ① where后边字段 适合建立索引 ② order by 排序字段适合建立索引 ③ 索引覆盖 即 所要查询的字段本身就是索引 直接在索引中查询数据. 例如 select name,a ...

  9. SQL Server 索引维护(1)——如何获取索引使用情况

    前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...

随机推荐

  1. How to rebuild RPM database on a Red Hat Enterprise Linux system?

    本文是笔者最近遇到的一个故障的处理过程,解决方案是Rebuild RPM 的DB,后面内容其实是REDHAT官方的solutions,不过我遇到的现象和解决方案都与官方有点出入,故一直帖出来: 我遇到 ...

  2. Oracle Database Link 的创建和使用小见

    假设:需要从数据库db_a通过db_link连接到db_b查询数据库b的部分相关信息 前提条件: 数据库a账户需要有创建dblink的权限,如果没有可以使用dba账户赋权限 grant CREATE ...

  3. pickle和cPickle:Python对象的序列化(上)

    https://segmentfault.com/a/1190000002493548 pickle模块实现了一种算法,将任意一个Python对象转化成一系列字节(byets).此过程也调用了seri ...

  4. MySQL错误Another MySQL daemon already running with the same unix socket.v

    etc/init.d/mysqld start 结果显示 Another MySQL daemon already running with the same unix socket.显示另一个MyS ...

  5. 【DB2】NOT IN使用中的大坑

    1.环境准备 ------建表TB DROP TABLE TB; CREATE TABLE TB ( ID INTEGER, LEVEL_DETAIL ) ); INSERT INTO TB (ID, ...

  6. Azure Storage 分块上传

    概述 Azure 存储提供三种类型的 Blob:块 Blob.页 Blob 和追加 Blob.其中,块 Blob 特别适用于存储短的文本或二进制文件,例如文档和媒体文件. 块 Blob 由块组成,每个 ...

  7. vs mvc 视图中找不到 viewdata viewbag的解决方案

    1.查看views下的web.config文件是否存在 2.检查config中system.web.mvc ,version中版本号与自己的vs内置mvc版本一致 迁移项目可能有此问题

  8. Atitit.biz业务系统 面向框架  面向模式---------数据映射imp

    Atitit.biz业务系统 面向框架  面向模式---------数据映射imp 1.1. 面向变量  面向过程  面向对象 面向组件  面向框架  面向服务 面向模式1 1.2. 第2章 架构模式 ...

  9. java的集合层次图

  10. Pycharm上python unittest不执行"if __name__ == '__main__' "问题or选择非unittest run

    转:http://www.cnblogs.com/csjd/p/6366535.html python unittest不执行"if __name__ == '__main__' " ...