刚开始接触Lucene,看到好多推荐Lucene in action
虽然代码用的是lucene 1,但原理讲的很清楚,稍作修改即可。
下面给出lucene最简单的一个应用实例,从书上修改而来
Lucene in action 中用的为Lucene 1,我用lucene 2来实现了Lucene in action 第一章 初始Lucene
索引过程:
核心类:
IndexWriter, Directory, Analyzer, Document, Field
Indexer如下
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
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;
/**
* @author xusulong
*
*/
public class Indexer {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\index");
File dataDir = new File("D:\\lucene-2.4.1");
long start = new Date().getTime();
int numIndexed = index(indexDir, dataDir);
long end = new Date().getTime();
System.out.println("Indexing " + numIndexed + " files took "
+ (end - start) + " milliseconds");
}
public static int index(File indexDir, File dataDir) throws IOException{
if(!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir + " does not exixt or is not a diretory");
}
//创建lucene索引
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
writer.setUseCompoundFile(false);
indexDirectory(writer, dataDir);
int numIndexed = writer.maxDoc();
writer.optimize();
writer.close(); //关闭索引
return numIndexed;
}
private static void indexDirectory(IndexWriter writer, File dir) throws IOException{
File[] files = dir.listFiles();
for( int i = 0; i < files.length; i ++) {
File f = files[i];
if(f.isDirectory()) {
indexDirectory(writer, f);//遍历
} else if(f.getName().toLowerCase().endsWith(".txt")) {
indexFile(writer, f);
}
}
}
private static void indexFile(IndexWriter writer, File f) throws IOException{
if(f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = new Document();
//索引文件中的内容
doc.add(new Field("contents", new FileReader(f)));
//索引文件名
doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
//将Document对象添加到lucene的索引中去
writer.addDocument(doc);
}
}
搜索过程:
核心类:
IndexSearcher, Term, Query, QueryParser, TopDocs, ScoreDoc
Searcher如下
import java.io.File;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* @author xusulong
*
*/
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\index");
String q = "lucene";
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ " does not exixt or is not a diretory");
}
search(indexDir, q);
}
public static void search(File indexDir, String q) throws Exception {
// 打开索引
Directory fsDir = FSDirectory.getDirectory(indexDir);
// 对查询的字符串进行解析
IndexSearcher is = new IndexSearcher(fsDir);
QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
Query query = parser.parse(q);
long start = new Date().getTime();
// 搜索索引
TopDocs topDocs = is.search(query, 100);
ScoreDoc[] hits = topDocs.scoreDocs;
long end = new Date().getTime();
System.out.println("Found " + hits.length + " document(s) (in "
+ (end - start) + " milliseconds) that mactched query '" + q
+ "':");
for (int i = 0; i < hits.length; i++) {
int docId = hits[i].doc;
// 检索匹配到的文件,显示文件名
Document doc = is.doc(docId);
System.out.println(docId + ": " + doc.get("filename"));
}
}
}
结果
两个程序都在我的机器上运行成功(windows xp),结果如下:
Indexer:
Indexing D:\lucene-2.4.1\BUILD.txt
Indexing D:\lucene-2.4.1\CHANGES.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\java\org\apache\lucene\analysis\nl\stems.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\java\org\apache\lucene\analysis\nl\words.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\de\data.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\ru\stemsUnicode.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\ru\test1251.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\ru\testKOI8.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\ru\testUnicode.txt
Indexing D:\lucene-2.4.1\contrib\analyzers\src\test\org\apache\lucene\analysis\ru\wordsUnicode.txt
Indexing D:\lucene-2.4.1\contrib\ant\src\test\org\apache\lucene\ant\test.txt
Indexing D:\lucene-2.4.1\contrib\benchmark\CHANGES.txt
Indexing D:\lucene-2.4.1\contrib\benchmark\src\test\org\apache\lucene\benchmark\quality\trecQRels.txt
Indexing D:\lucene-2.4.1\contrib\benchmark\src\test\org\apache\lucene\benchmark\quality\trecTopics.txt
Indexing D:\lucene-2.4.1\contrib\CHANGES.txt
Indexing D:\lucene-2.4.1\contrib\memory\src\test\org\apache\lucene\index\memory\testqueries.txt
Indexing D:\lucene-2.4.1\contrib\memory\src\test\org\apache\lucene\index\memory\testqueries2.txt
Indexing D:\lucene-2.4.1\contrib\miscellaneous\README.txt
Indexing D:\lucene-2.4.1\contrib\queries\README.txt
Indexing D:\lucene-2.4.1\contrib\similarity\README.txt
Indexing D:\lucene-2.4.1\contrib\snowball\LICENSE.txt
Indexing D:\lucene-2.4.1\contrib\snowball\README.txt
Indexing D:\lucene-2.4.1\contrib\snowball\SNOWBALL-LICENSE.txt
Indexing D:\lucene-2.4.1\contrib\surround\README.txt
Indexing D:\lucene-2.4.1\contrib\wordnet\README.txt
Indexing D:\lucene-2.4.1\contrib\xml-query-parser\src\test\org\apache\lucene\xmlparser\reuters21578.txt
Indexing D:\lucene-2.4.1\docs\skin\images\README.txt
Indexing D:\lucene-2.4.1\docs\skin\note.txt
Indexing D:\lucene-2.4.1\LICENSE.txt
Indexing D:\lucene-2.4.1\NOTICE.txt
Indexing D:\lucene-2.4.1\README.txt
Indexing D:\lucene-2.4.1\src\jsp\README.txt
Indexing 32 files took 657 milliseconds
Searcher:
Found 14 document(s) (in 15 milliseconds) that mactched query 'lucene':
30: D:\lucene-2.4.1\README.txt
24: D:\lucene-2.4.1\contrib\wordnet\README.txt
17: D:\lucene-2.4.1\contrib\miscellaneous\README.txt
19: D:\lucene-2.4.1\contrib\similarity\README.txt
21: D:\lucene-2.4.1\contrib\snowball\README.txt
31: D:\lucene-2.4.1\src\jsp\README.txt
0: D:\lucene-2.4.1\BUILD.txt
29: D:\lucene-2.4.1\NOTICE.txt
23: D:\lucene-2.4.1\contrib\surround\README.txt
11: D:\lucene-2.4.1\contrib\benchmark\CHANGES.txt
14: D:\lucene-2.4.1\contrib\CHANGES.txt
18: D:\lucene-2.4.1\contrib\queries\README.txt
1: D:\lucene-2.4.1\CHANGES.txt
28: D:\lucene-2.4.1\LICENSE.txt
分享到:
相关推荐
搜索引擎jsp+servlet+jdbc+lucene框架,包括初始化索引,实时索引,关键字索引,数字及日期索引等功能,下载后即可使用,包目录结构清晰,易懂易学
在前面Lucene-2.2.0 源代码阅读学习(1)中,根据Lucene提供的一个Demo,详细分析研究一下索引器org.apache.lucene.index.IndexWriter类,看看它是如果定义的,掌握它建立索引的机制。 通过IndexWriter类的实现源代码...
基于lucene全文检索引擎实现的短文本匹配系统 ajax什么时候执行success,什么时候执行error 简单的说Ajax请求通过XMLHttpRequest对象发送请求,该对象有四个状态(readyState): 0-未初始化、1-正在初始化、2-发送...
这将允许您在S3中维护Lucene索引,并在S3存储桶中运行Lucene搜索。 这里的想法是通过将Lucene索引保留在S3中,从而使完整的搜索体系结构变得无服务器,这为我们提供了无限的廉价存储,并利用AWS Lambda无服务器...
2.静态化 3.搜索引擎优化 ... 静态化 cms ssh lucene 单点登陆 jar包太多,未上传,但全部列出,可自己加入 使用方法:将源码导入myeclipse,然后加入相应jar包(按lib下图片中加入), ... 装和数据初始化即可
2. 初始化项目 创建一个名为bookshop的数据库,将bookshop.sql导入 打开IntelliJ IDEA,将项目导入 修改jdbc.properties文件配置,同时配置Tomcat 开始运行,访问http://localhost:8080/home.do 实现功能 登录 上传...
URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的...
initBloom(bloomName, force, expectedNumItems, expectedFpp) :创建并初始化Bloom过滤器。 REST API GET /put/[/bloomName] 将项目放入布隆过滤器。 item (String) :要放入bloom筛选器的项目 bloomName ...
初始化 2 分词 2 配置文件 PanGu.xml 7 高亮组件PanGu.HighLight.dll 调用方法 8 字典管理 8 Demo.exe 11 PanGu4Lucene 调用方法 12 创建索引 12 插入数据 12 对要搜索的词分词 13 搜索 13 PanGu4Lucene 示例 15 ...
6、Hibernate Search 4.2 (Apache Lucene 3.6) 7、SiteMesh 2.4 8、JQuery 1.9 9、Twitter Bootstrap 2.3.1 10、....其他技术就不讲了,具体参考项目 项目访问: 1)后台后台访问地址:...
安装将Blacklight添加到您的Gemfile : gem "blacklight" 运行安装生成器,它将复制一些初始模板,迁移,路由和配置: rails generate blacklight:install文档,信息和支持依存关系Ruby 2.2+ 邦德勒Rails 5.1+贡献...
面向手机领域图像资源获取与索引系统 主题网络爬虫,从初始手机网站开始爬取3层深度的手机图片 存储:索引,lucene结构,
充当全文检索数据库的是Apache组织下的开源项目Lucene 检索工具,而Lucene只是个搜索引擎工具,它提供API接口,通过编写程序对信息进行索引和检索,在其后台需要网络爬虫程序的支持,其目的是通过网络爬虫软件抓取...
Ansj中文分词是一款纯Java、主要用于自然语言处理、高精度的中文分词工具,目标是“准确、高效、自由地进行中文分词”。 ... 此分词具有自己学习的...献给广大使用lucene要求高召回率的同学.没什么技术含量.但是很实用!
###数据库初始化 数据库采用mysql进行静态的数据存储,请在网盘中一并下载 init.sql ###项目构建工具 项目采用maven的项目管理工具管理,并且采用的SpringBoot微服务框架开发。此处建议使用idea、eclipse等工具进行...
Spring 5.0 + SpringMVC + JPA + Ehcache(可选Memcached) + Lucene 使用平台: JDK 1.8及以上 + Tomcat 8.0及以上 + MySQL 5.5.3及以上 源码运行教程: 1.将源代码导入到Eclipse中(基于Maven) 2.修改数据库配置...
Spring 5.0 + SpringMVC + JPA + Ehcache(可选Memcached) + Lucene 使用平台: JDK 1.8及以上 + Tomcat 8.0及以上 + MySQL 5.5.3及以上 源码运行教程: 1.将源代码导入到Eclipse中(基于Maven) 2.修改数据库配置...
通过使用Lucene工具包对下载资源。利用java.url中的类实现Spider程序与外界通讯,以及处理网页中的URL连接,对蜘蛛程序的核心类(通讯核心、蜘蛛程序工作核心),资源索引的建立与搜索新型了详细的研究。 通过设计...
初始版本允许使用 MySQL 数据库进行文件索引。 结果证明这非常耗时,搜索速度慢得要命。 另一个主要问题是只能找到准确的文件名。 作为解决方案,我已转向 Apache Lucene - 支持 SolR 的引擎。 Lucene 进行语法...
淘特站内搜索引擎是由淘特JSP搜索引擎发展而来,系统基于Lucene.Net核心,通过高效的中文分词算法将数据库中内容进行分析、索引并保存至硬盘中。前台搜索时,通过读取索引文件查询,避免了传统数据库查询在高并发及...