使用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就达到了两万多,也算是对这几个月工作的一点肯定吧 ...
随机推荐
- selenium 3 对我们的影响
The major change in Selenium 3.0 is we're removing the original Selenium Core implementation and rep ...
- 管道命令和xargs的区别(经典解释)
一直弄不懂,管道不就是把前一个命令的结果作为参数给下一个命令吗,那在 | 后面加不加xargs有什么区别 NewUserFF 写道: 懒蜗牛Gentoo 写道: 管道是实现"将前面的标准输出 ...
- yii2的扩展程序包
查找yii2的扩展程序包 https://packagist.org/ 搜索yiisoft/yii2 可通过composer install下载 composer install下载程序包是通过com ...
- exit(0)、exit(1)、exit(-1)的区别
exit(0) - 正常退出 exit(1) - 异常退出(除0外,其他值均为异常退出)
- Spark 编程基础
1. 初始化Spark import org.apache.spark.{SparkContext, SparkConf} val conf=new SparkConf().setAppName(&q ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- Leetcode Perfect Square
这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...
- 【BZOJ-1923】外星千足虫 高斯消元 + xor方程组
1923: [Sdoi2010]外星千足虫 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 766 Solved: 485[Submit][Status ...
- 【poj2761】 Feed the dogs
http://poj.org/problem?id=2761 (题目链接) 题意 求区间第K大. Solution 和poj2104一模一样. 主席树代码 // poj2761 #include< ...
- eclipse安装插件的方法(简单、ERMaster插件安装)
ERMaster插件:https://sourceforge.net/projects/ermaster/ 链接:http://pan.baidu.com/s/1o7UWLMa 密码:wkax 可以编 ...