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. JNI之——在cmd命令行下编译执行C/C++源文件

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604269 一直用java来敲代码,java配置好jre路径之后.在cmd下编译 ...

  2. virtualbox虚拟机ubuntu操作系统,设置网络互通、访问,能访问虚拟机swoole的http服务

    网络互通 1.设置virtualbox网络连接模式为桥接网卡模式 2.重启虚拟机查看虚拟机IP ifconfig 3.开启window的telnet程序 控制面板->程序->启用或关闭wi ...

  3. mybatis 一对多

    person package com.kerwin.mybatis.pojo; import java.util.List; public class Person { private int id; ...

  4. CSAPP:异常控制流

    在一般的情况下,处理器处理的指令序列是相邻的(顺序执行). 异常控制流提供了指令的跳转,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常处理 在系统启动时,操作系统分配和初始化一张称为异常表的 ...

  5. linux phpredisAdmin安装步骤

    1:linux安装apache环境, 这一步可以不用安装plsql http://www.cnblogs.com/lufangtao/archive/2012/12/30/2839679.html 2 ...

  6. STL vector的构造函数和析构函数(2)

    原文来自:点击打开链接 译文: public member function vector的构造器:这里我仅仅翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> s ...

  7. windowns 查看端口占用

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  8. 在连接mysql数据库时出错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone

    这个错误是时区导致的,所以需要在配置连接url后面加上时区: url=jdbc:mysql://localhost:3309/test?serverTimezone=UTC 其中UTC是统一标准世界时 ...

  9. http协议---简述

    http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定. 也就是说,http协议规定了超文本传输所 ...

  10. MySQL:系列合集

    MySQL一:初识数据库 MySQL二:库操作 MySQL三:存储引擎 MySQL四:表操作 MySQL五:数据操作 MySQL六:索引原理与慢查询优化 MySQL七:数据备份 MySQL八:视图.触 ...