近来项目需要使用Lucene,工作之余上网学习了下相关内容,做个笔记
1.创建索引
步骤:创建IndexWriter
IndexWriter writer = new IndexWriter(
new NIOFSDirectory(new File(path)), new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
创建Document
创建Field 包含 field名和field值
将Field通过Document的add方法添加到Document中
Document doc = new Document();
doc.add(new Field("text", "It is a text area", Store.YES,
Index.ANALYZED_NO_NORMS));
doc.add(new Field("info", "It is a Infomation area", Store.YES,
Index.ANALYZED_NO_NORMS));
将Document通过IndexWriter的addDocument方法添加到IndexWriter中
关闭IndexWriter
writer.addDocument(doc);writer.close();
2从索引中根据关键字查询
创建IndexSearcher
IndexSearcher searcher = new IndexSearcher(new NIOFSDirectory(new File(
path)));
创建Query
Query query = new QueryParser(Version.LUCENE_30, field,
new StandardAnalyzer(Version.LUCENE_30)).parse(keyword);
通过IndexSearcher的search方法查找关键字,使用TopDocs封装结果集
TopDocs docs = searcher.search(query, 10);
全部代码:(包换了合并内存索引到硬盘索引中)
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
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.NIOFSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
public class TestLucen {
public static final String path = "E:\\workspaces\\lucene\\index";
public static void main(String[] args) throws Exception {
writeIndex();
readIndex("text", "area");
}
public static void writeIndex() throws Exception {
// 硬盘索引
IndexWriter writer = new IndexWriter(
new NIOFSDirectory(new File(path)), new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
// Ram索引
RAMDirectory ram = new RAMDirectory();
IndexWriter ramwriter = new IndexWriter(ram, new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
Document doc = new Document();
Document doc1 = new Document();
doc.add(new Field("text", "It is a text area", Store.YES,
Index.ANALYZED_NO_NORMS));
doc.add(new Field("info", "It is a Infomation area", Store.YES,
Index.ANALYZED_NO_NORMS));
writer.addDocument(doc);
doc1.add(new Field("text", "it is another area", Store.YES,
Index.ANALYZED));
ramwriter.addDocument(doc1);
ramwriter.optimize();
ramwriter.close();
// 将Ram索引合并到硬盘索引上,必须先调用ram的close方法
writer.addIndexes(IndexReader.open(ram));
writer.optimize();
writer.close();
}
public static void readIndex(String field, String keyword) throws Exception {
IndexSearcher searcher = new IndexSearcher(new NIOFSDirectory(new File(
path)));
Query query = new QueryParser(Version.LUCENE_30, field,
new StandardAnalyzer(Version.LUCENE_30)).parse(keyword);
TopDocs docs = searcher.search(query, 10);
System.out.println("查找到" + docs.totalHits + "个\n对应的text为:");
ScoreDoc[] doc = docs.scoreDocs;
for (ScoreDoc d : doc) {
Document docu = searcher.doc(d.doc);
System.out.println(docu.get(field));
}
}
}
执行结果:
查找到2个
对应的text为:
It is a text area
it is another area
分享到:
相关推荐
Struts标签Lucene中文分词构建基于词典的Lucene分析器使用Log4j进行日志记录 您使用过旅游指南吗?它总是能让我们快速的找到目的地。我对此有深刻的印象,希望这篇指南也能使您快速的完成Struts标签相关的任务。我...
本资源使用 Elasticsearch结合spring data,mybatis plus,logback日志等流行框架搭建自己的全文搜索引擎,大家跟着做就能学会其中的操作方式(Java api封装方法和spring data封装的操作方法)。 注:可以结合博主...
站内搜索引擎源码 源码描述: XL站内搜索引擎 采用lucene.net分词技术,lognet4log日志管理 把内容按照keywords进行高亮显示等 适合想了解搜索相关方面内容的用户下载学习
今年学习了一下ES向量插件(亚马逊设计的那个方式还挺有意思的,重新设计向量索引文件,但文件管理依托于lucene),搞了搞知识图谱(JanusGraph),最近又在弄ES存储计算分离(基于共享存储):triangular_flag: 更新...
原动力 性能 价格 收益 成本 VS 起源与目标 大数据与Hadoop 应用模式 大数据的核心理念 Xoom不跨境汇款异常交易报警 网管异常日志分析 随机样本 全体数据 Google Rosetta VS. IBM Candide:美国标准局(NIST)机器...
2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...
日志管理:系统日志列表查询、在线查看、在线下载等 路线管理:集成百度地图API,提供线路查询管理功能 Druid Monitor(监控):集成阿里巴巴连接池,提供在线连接池监控程序,包括:数据源、SQL监控、URL监控...
GC日志详细分析 JVM调优实战 Mysql性能调优 SQL执行原理详解 索引底层剖析 执行计划与SQL优化 Mysql锁机制与事务隔离级别详解 并发编程 JMM内存模型 并发同步处理 并发包之tools工具 并发包之atomic原子操作 阻塞...
我们的项目将使用Java编写,使用lucene进行文本分析,使用log4j记录统计信息,以便我们可以专注于不同文本分类算法的实现。 这个软件是如何工作的? 我们的软件会将一批文件作为训练数据,以及用户指定的测试文件。 ...
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索...
博客系统博客系统BLOG是学习JavaWeb开发4个月以来的总结实践项目,使用SSM(Spring,SpringMVC,MyBatis)框架,MVC三层结构,Lucene全部检索引擎,Junit 4单元测试,logback日志框架,Druid数据库连接池,Shiro安全...
3.强大的日志系统,如异常日志,页面访问404日志,静态生成日志等 4.支持移动WebApp的皮肤,你可以修改皮肤快速创建web app站点快速模板,引入百度的gmu,也就意味着你可以在app模板里随时使用gmu里的部件级访问,...
搜索引擎技术:Lucene.Net、多线程开发、爬虫技术、网页分析、正则表达式、Log4Net日志框架、Quartz.Net定时作业调度。 大型互联网开发技术:代码生成、网页静态化、基于JQuery的Web2.0页面开发、AJAX、SEO、网站...
笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!此时此...
大型数据库的设计与编程技巧 本人最近开发一个访问统计系统,日志非常的大,都保存在数据库里面。 我现在按照常规的设计方法对表进行设计,已经出现了查询非常缓慢地情形。 大家对于这种情况如何来设计数据库呢?...