使用Lucene开发自己的搜索引擎
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开发自己的搜索引擎的更多相关文章
- 2.使用Lucene开发自己的搜索引擎–indexer索引程序中基本类介绍
(1)Directory:Directory类描述了Lucene索引的存放位置,它是一个抽象,其子类负责具体制定索引的存储路径.FSDirectory.open方法来获取真实文件在文件系统中的存储路径 ...
- 1.使用Lucene开发自己的搜索引擎--倒排索引基础知识
1.单词--文档矩阵 单词-文档矩阵是表达两者之间所具有的一种包含关系的概念模型,图3-1展示了其含义.图3-1的每列代表一个文档,每行代表一个单词,打对勾的位置代表包含关系.
- 【课程分享】基于Lucene4.6+Solr4.6+Heritrix1.14+S2SH实战开发从无到有垂直搜索引擎
对这个课程有兴趣的朋友,能够加我的QQ2059055336和我联系,能够和您分享. 课程介绍:最有前途的软件开发技术--搜索引擎技术 搜索引擎作为互联网发展中至关重要的一种应用,已经成为互联网各个 ...
- Lucene.Net+盘古分词->开发自己的搜索引擎
//封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...
- lucene开发序之luke神器
lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...
- [原创]一种基于Python爬虫和Lucene检索的垂直搜索引擎的实现方法介绍
声明:本文首发在博客园晨星落羽,Shulin_Cao和lvmememe首页,转载请注明出处. 前言 2016.5到2017.5,我们三人(lvmememe,Shulin_Cao,晨星落羽)共同完成了一 ...
- Lucene系列一:搜索引擎核心理论
一.为什么需要搜索引擎 问题1:数据库索引的原理是怎样的? 索引原理:对列值创建排序存储,数据结构={列值.行地址}.在有序数据列表中就可以利用二分查找快速找到要查找的行的地址,再根据地址直接取行数据 ...
- Lucene开发实例:Lucene中文分词(转载)
1.准备工作下载lucene 3.6.1 : http://lucene.apache.org/下载中文分词IK Analyzer: http://code.google.com/p/ik-analy ...
- [转载]用.NET开发的磁力搜索引擎——Btbook.net
去年10月份开始研究相关的协议与资料,中途乱七八糟的事情差点没坚持下来,寒假里修修补补上礼拜把Btbook发布了,经过社交网络的推广之后,上线第三天UV就达到了两万多,也算是对这几个月工作的一点肯定吧 ...
随机推荐
- jsrender for 标签
for object使用 1.为进入object中直接使用其中的属性 <div id="result"></div> <script id=" ...
- 【BZOJ-4008】亚瑟王 概率与期望 + DP
4008: [HNOI2015]亚瑟王 Time Limit: 20 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 832 Solved: 5 ...
- 【poj3615】 Cow Hurdles
http://poj.org/problem?id=3615 (题目链接) 题意 给出一张有向图,求从u到v最大边最小的路径的最大边.→_→不会说话了.. Solution 好久没写Floyd了,水一 ...
- 【bzoj2120】 数颜色
http://www.lydsy.com/JudgeOnline/problem.php?id=2120 (题目链接) 题意 给出一个n个数,m个询问,每次询问一个区间或修改一个数,求区间内不同的数有 ...
- 什么是 WSGI -- Python 中的 “CGI” 接口简介
今天在 git.oschina 的首页上看到他们推出演示平台,其中,Python 的演示平台支持 WSGI 接口的应用.虽然,这个演示平台连它自己提供的示例都跑不起来,但是,它还是成功的勾起了我对 W ...
- Myeclipse的show in breadcrumb
m如何取消eclipse的show in breadcrumb 不小心点了show in breadcrumb,在编辑器界面上面多一层路径条,多余碍事,不晓得怎么取消,搞了半天终于弄好,方法如下: 点 ...
- python 生成器和递归
生成器 1.定义 问题:python会把对象放到内存中,我们每次定义变量.列表等都会在内存中占用对应的地址块,所以当内存容量一定时,列表的容量会受到内存的限制,而且假如我们创建了一个包含200万个元素 ...
- CentOS设置防火墙开放端口
1. iptables是linux下的防火墙,同时也是服务的名称. service iptables status service iptables start service iptables st ...
- AngularJs angular.Module模块接口配置
angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...
- c 开源代码
阅读优秀代码是提高开发人员修为的一种捷径……1. WebbenchWebbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在 ...