lucene Hello World
一个lucene创建索引和查找索引的样例:
创建索引:
public class Indexer {
private IndexWriter indexWriter;
/**
* 构造器实例化indexWriter
* @throws Exception
*/
public Indexer(String indexPath) throws Exception {
Directory directory = FSDirectory.open(Paths.get(indexPath));//索引存储的位置
Analyzer analyzer = new StandardAnalyzer();//标准分析器
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(directory, iwc);
}
/**
* 关闭indexWriter
* @param indexWriter
* @throws IOException
*/
public void close() throws Exception {
indexWriter.close();
}
/**
* 获取文档Document
* @throws FileNotFoundException
*/
public Document getDocumnet(File f) throws Exception {
Document doc = new Document();
doc.add(new TextField("content", new FileReader(f)));
doc.add(new TextField("tittle",f.getName(),Field.Store.YES));
doc.add(new TextField("path",f.getCanonicalPath(), Field.Store.YES));
return doc;
}
/**
* 索引当个文件
* @throws Exception
*/
public void indexFile(File f) throws Exception {
System.out.println(f.getName());
Document doc = this.getDocumnet(f);
indexWriter.addDocument(doc);
}
/**
* 索引一个目录下的所有文件
* @param filePath 目录路径
* @return 索引文件的个数
* @throws Exception
*/
public int index(String filePath) throws Exception {
File[] files = new File(filePath).listFiles();
for(File f:files) {
this.indexFile(f);
}
return indexWriter.numDocs();
}
public static void main(String[] args) {
String indexPath = "G:\\工作\\luence\\index";
String dataPath = "G:\\工作\\luence\\data";
Indexer indexer = null;
int indexNum=0;
try {
indexer = new Indexer(indexPath);
indexNum = indexer.index(dataPath);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
indexer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("索引了"+indexNum+"个文件");
}
}
查找索引:
public class Searcher {
public static void search(String indexPath,String searchStr) throws Exception { Directory dir = FSDirectory.open(Paths.get(indexPath));
IndexReader indeReader = DirectoryReader.open(dir);
IndexSearcher indexSearch = new IndexSearcher(indeReader); Analyzer analyzer = new StandardAnalyzer();//标准分词器
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse(searchStr);
TopDocs td = indexSearch.search(query, 10);
for(ScoreDoc sc:td.scoreDocs) {
Document doc = indexSearch.doc(sc.doc);
System.out.println(doc.get("tittle"));
System.out.println(doc.get("path"));
}
}
public static void main(String[] args) throws Exception {
Searcher.search("G:\\工作\\luence\\index\\", "Hollywood");
}
}
lucene Hello World的更多相关文章
- lucene 基础知识点
部分知识点的梳理,参考<lucene实战>及网络资料 1.基本概念 lucence 可以认为分为两大组件: 1)索引组件 a.内容获取:即将原始的内容材料,可以是数据库.网站(爬虫).文本 ...
- 用lucene替代mysql读库的尝试
采用lucene对mysql中的表建索引,并替代全文检索操作. 备注:代码临时梳理很粗糙,后续修改. import java.io.File; import java.io.IOException; ...
- Lucene的评分(score)机制研究
首先,需要学习Lucene的评分计算公式—— 分值计算方式为查询语句q中每个项t与文档d的匹配分值之和,当然还有权重的因素.其中每一项的意思如下表所示: 表3.5 评分公式中的因子 评分因子 描 述 ...
- Lucene的分析资料【转】
Lucene 源码剖析 1 目录 2 Lucene是什么 2.1.1 强大特性 2.1.2 API组成- 2.1.3 Hello World! 2.1.4 Lucene roadmap 3 索引文件结 ...
- Lucene提供的条件判断查询
第一.按词条搜索 - TermQuery query = new TermQuery(new Term("name","word1"));hits = sear ...
- Lucene 单域多条件查询
在Lucene 中 BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表 ...
- lucene自定义过滤器
先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...
- lucene+IKAnalyzer实现中文纯文本检索系统
首先IntelliJ IDEA中搭建Maven项目(web):spring+SpringMVC+Lucene+IKAnalyzer spring+SpringMVC搭建项目可以参考我的博客 整合Luc ...
- 全文检索解决方案(lucene工具类以及sphinx相关资料)
介绍两种全文检索的技术. 1. lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...
- MySQL和Lucene索引对比分析
MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr.ElasticSearch)的核心类库.两者的索引(index)有什么区别呢?以前写过 ...
随机推荐
- 嵌入式Linux设备驱动程序:发现硬件配置
嵌入式Linux设备驱动程序:发现硬件配置 Embedded Linux device drivers: Discovering the hardware configuration Interfac ...
- Django(53)二次封装Response
前言 有时候我们使用drf的Response,会发现默认返回的格式不太友好,每次我们都需要写入以下的格式 return Response({ "status": 0, " ...
- Ucore lab1实验报告
练习一 Makefile 1.1 OS镜像文件ucore.img 是如何一步步生成的? + cc kern/init/init.c + cc kern/libs/readline.c + cc ker ...
- JUC 并发编程--01,线程,进程,经典卖票案例, juc的写法
进程: 就是一个程序, 里面包含多个线程, 比如一个QQ程序 线程: 进程中最小的调度单元, 比如 QQ中的自动保存功能 并发: 多个线程操作同一资源, 抢夺一个cpu的执行片段, 快速交替 并行: ...
- Spring IOC(控制反转)思想笔记
Spring IOC(控制反转)思想笔记 IOC控制反转基本理念就是将程序控制权从程序员手中交给用户自定义,从而避免了因为用户一个小需求的变化使得程序员需要改动大量代码. 案例 如果按照之前javaw ...
- Spring Cloud10:Zipkin 服务跟踪
一.概述 为什么要有服务跟踪,分布式系统中有很多个服务在相互调用,调用关系是错综复杂的,如果这时出现了问题,我们在进行问题排查的时候,或者在优化架构的时候,工作量就比较大,这时候就需要我们能够准确的跟 ...
- 简单测试 APISIX2.6 网关
Apache APISIX是一个动态的.实时的.高性能的 API 网关.它提供丰富的流量管理功能,例如负载均衡.动态上游服务.金丝雀发布.断路.身份验证.可观察性等.您可以使用 Apache APIS ...
- python通过字典实现购物车案例-用户端
import os dict01 = { 'iphone' : { '5999' : { '总部位于美国' : '价格相对较贵', }, }, 'wahaha' : { '15' : { '总部位于中 ...
- Java知识,面试总会问到虚拟机,虚拟机类加载机制你懂吗?
虚拟机把描述类的数据从Class文件文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 与那些在编译时需要进行连接工作的语言不同 ...
- NOIP模拟测试13「矩阵游戏·跳房子·优美序列」
矩阵游戏 考试时思路一度和正解一样,考试到最后还是打了80分思路,结果80分打炸了只得了40分暴力分 题解 算出来第一列的总值,每次通过加每两列之间的差值得出下一列的总值 算第一列我们只需要让当前点* ...