1、下载Lucene开发包,请到:http://lucene.apache.org/

2、在myeclipse环境部署该开发包:

3、代码编写:

package Lucene;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; /**
* 建立索引
* @author Administrator
*
*/
public class Indexer { /**
* @param args
*/
public static void main(String[] args) throws Exception{ String indexDir = "E:\\index";///在指定目录创建索引文件夹
String dataDir = "E:\\dataSource";///对指定目录中的“.txt”文件进行索引 long start = System.currentTimeMillis();
Indexer indexer = new Indexer(indexDir);
int numIndexed;
try{
numIndexed = indexer.index(dataDir, new TextFilesFilter());
}finally{
indexer.close();
}
long end = System.currentTimeMillis(); System.out.println("索引 "+ numIndexed + " 文件花费 "+
(end - start) + "ms"); } private IndexWriter writer; //创建Lucene Index Writer
public Indexer(String indexDir)throws IOException{
Directory dir = FSDirectory.open(new File(indexDir));
/*
* Version.LUCENE_30:是版本号参数,Lucene会根据输入的版本值,
* 针对该值对应的版本进行环境和行为匹配
*/
writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), true,
IndexWriter.MaxFieldLength.UNLIMITED);
} //关闭Index Writer
public void close()throws IOException{
writer.close();
} //返回被索引文档文档数
public int index(String dataDir, FileFilter filter)throws Exception{
File[] files = new File(dataDir).listFiles(); for(File f:files){
if(!f.isDirectory() &&
!f.isHidden()&&
f.exists()&&
f.canRead()&&
(filter == null || filter.accept(f))){
indexFile(f);
}
}
return writer.numDocs();
} //只索引.txt文件,采用FileFilter
private static class TextFilesFilter implements FileFilter{ @Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().toLowerCase().endsWith(".txt");
} } protected Document getDocument(File f) throws Exception{
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));//索引文件内容
doc.add(new Field("filename", f.getName(),//索引文件名
Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("fullpath", f.getCanonicalPath(),//索引文件完整路径
Field.Store.YES, Field.Index.NOT_ANALYZED)); return doc;
} //向Lucene索引中添加文档
private void indexFile(File f) throws Exception{
System.out.println("Indexing "+f.getCanonicalPath());
Document doc = getDocument(f);
writer.addDocument(doc);
} }

这时编译运行代码,如果没出错的话,会出现下面的结果:

Indexing E:\dataSource\1.txt
Indexing E:\dataSource\2.txt
Indexing E:\dataSource\3.txt
Indexing E:\dataSource\4.txt
索引 4 文件花费 259ms

参考:http://biancheng.dnbcw.info/1000wen/448393.html

使用Lucene开发自己的搜索引擎的更多相关文章

  1. 2.使用Lucene开发自己的搜索引擎–indexer索引程序中基本类介绍

    (1)Directory:Directory类描述了Lucene索引的存放位置,它是一个抽象,其子类负责具体制定索引的存储路径.FSDirectory.open方法来获取真实文件在文件系统中的存储路径 ...

  2. 1.使用Lucene开发自己的搜索引擎--倒排索引基础知识

    1.单词--文档矩阵 单词-文档矩阵是表达两者之间所具有的一种包含关系的概念模型,图3-1展示了其含义.图3-1的每列代表一个文档,每行代表一个单词,打对勾的位置代表包含关系.

  3. 【课程分享】基于Lucene4.6+Solr4.6+Heritrix1.14+S2SH实战开发从无到有垂直搜索引擎

    对这个课程有兴趣的朋友,能够加我的QQ2059055336和我联系,能够和您分享.  课程介绍:最有前途的软件开发技术--搜索引擎技术  搜索引擎作为互联网发展中至关重要的一种应用,已经成为互联网各个 ...

  4. Lucene.Net+盘古分词->开发自己的搜索引擎

    //封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...

  5. lucene开发序之luke神器

    lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...

  6. [原创]一种基于Python爬虫和Lucene检索的垂直搜索引擎的实现方法介绍

    声明:本文首发在博客园晨星落羽,Shulin_Cao和lvmememe首页,转载请注明出处. 前言 2016.5到2017.5,我们三人(lvmememe,Shulin_Cao,晨星落羽)共同完成了一 ...

  7. Lucene系列一:搜索引擎核心理论

    一.为什么需要搜索引擎 问题1:数据库索引的原理是怎样的? 索引原理:对列值创建排序存储,数据结构={列值.行地址}.在有序数据列表中就可以利用二分查找快速找到要查找的行的地址,再根据地址直接取行数据 ...

  8. Lucene开发实例:Lucene中文分词(转载)

    1.准备工作下载lucene 3.6.1 : http://lucene.apache.org/下载中文分词IK Analyzer: http://code.google.com/p/ik-analy ...

  9. [转载]用.NET开发的磁力搜索引擎——Btbook.net

    去年10月份开始研究相关的协议与资料,中途乱七八糟的事情差点没坚持下来,寒假里修修补补上礼拜把Btbook发布了,经过社交网络的推广之后,上线第三天UV就达到了两万多,也算是对这几个月工作的一点肯定吧 ...

随机推荐

  1. linux下使用localhost和127.0.0.1都不能连接的解决思路

    linux下刚安装了mysql,尝试写了程序连接mysql,出现了只有用本地ip地址才能连接,而127.0.0.1和localhost都不能访问 解决这个问题主要查看3个方向 .hosts中是否有ip ...

  2. 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    VisualState 和 VisualStateManager 控件的默认 Style, ControlTemplate, VisualState 示例1.演示“VisualState 和 Visu ...

  3. extracting lines bases a list using awk

    extracting lines bases a list using awk awk 'NR==FNR{a[$1]=$0; next}($1 in a){print a[$1]"\n&qu ...

  4. Cocoa pod的使用注意点

    一.CocoaPods是什么? CocoaPods是一个负责管理iOS项目中第三方开源库的工具.CocoaPods的项目源码在Github上管理.该项目开始于2011年8月12日,在这两年多的时间里, ...

  5. cmd执行sql文件

    string infile = @"C:\Users\yudm\Desktop\test\Patch.sql"; Process sqlprocess = new Process( ...

  6. Windows 8及以上系统安装好SQL Server 2008之后找不到SQL Server配置管理器的问题

    直接的方法: 打开[运行]->输入[C:\Windows\SysWOW64\mmc.exe /32 C:\Windows\SysWOW64\SQLServerManager10.msc]即可. ...

  7. Cloud Design Patterns Book Reading(undone)

    目录 . the most common problem areas in cloud application development ) Availability ) Data Management ...

  8. hihoCoder 1401 Registration

    多队列模拟. 与POJ #1025 Department类似, 不过简化很多. 貌似这类模拟题经常出现. 用STL中的优先队列 (priority_queue<>) 很好写. 这题我写得很 ...

  9. JS 复制对象

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  10. Python翻转字符串或者列表的方式

    1. reversed class reversed(object) | reversed(sequence) -> reverse iterator over values of the se ...