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 2 的 Windows 子系统上发布 CUDA
Linux 2 的 Windows 子系统上发布 CUDA 为响应大众需求,微软 宣布 在 2020 年 5 月的 建造 大会上推出了 建造 ( WSL 2 ) – GPU 加速功能.这一特性为许多计 ...
- java后端知识点梳理——java基础
面向对象 java三大特性 封装: 将事务封装成一个类,达到解耦,隐藏细节的效果.通过get/set等方法,封装了内部逻辑,并保留了特定的接口与外界联系. 继承: 从一个已知的类中派生出一个新的类,新 ...
- 聚类算法K-Means算法和Mean Shift算法介绍及实现
Question:什么是聚类算法 1.聚类算法是一种非监督学习算法 2.聚类是在没有给定划分类别的情况下,根据数据相似度进行样本分组的一种方法 3.理论上,相同的组的数据之间有相同的属性或者是特征,不 ...
- NOIP模拟测试2「排列 (搜索)·APIO划艇」
排序 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目描述 输入格式 数据范围与提示 对于30%的数据,1<=N<=4: 对于全部的数据,1<=N< ...
- DOS命令行(5)——Windows系统的配置与管理(下)
whoami --查看当前有效用户 这个工具可以用来获取本地系统上当前用户(访问令牌)的用户名和组信息,以及相应的安全标识符(SID).声明.本地系统上当前用户的权限.登录标识符(登录 ID).例如, ...
- excel VBA把一个单元格内容按逗号拆分并依次替换到另一个单元格的括号里面(本题例子,把文本中的括号换成{答案}的格式,并按顺序填空)
方法1:运用excel单元格拆分合并实现 思路:用VBA正则查询左侧括号个数,对右侧单元格逐一按逗号.顿号等符号分列,同时左侧按括号分列(分列只能按括号单边分列),分列完成后按要求合并,本题事例把括号 ...
- EXCEL根据某一列单元格特定标点符号分行,其它列内容一致
注意事项:此方法注意运用excel空值单元格填充,因此数据处理之前,如果存在值为空的,请特殊处理,后期处理完再替换为原来的空值 1.在决定分行数的列后插入一列 2.根据逗号之前值最多的数量来决定分行数 ...
- SystemVerilog 中的相等运算符:== or === ?
1. 四值逻辑的逻辑运算 在对比SystemVerilog中的相等运算符之前,先来看一下三种最基本的逻辑运算符,下文中以·表示与运算,以+表示或运算,以'表示非运算.我们都知道在逻辑代数中,只有0和1 ...
- DDoS防护方式以及产品
导航: 这里将一个案例事项按照流程进行了整合,这样查看起来比较清晰.部分资料来自于Cloudflare 1.DDoS介绍 2.常用DDoS攻击 3.DDoS防护方式以及产品 4.Cloudflare ...
- 5、mysql_sql语言介绍
5.1.什么是sql: 总结:sql语句是关系型数据库的结构化查询和程序设计语言: 5.2.sql的分类: 1.数据库查询语言(DQL): select user,host,password from ...