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)有什么区别呢?以前写过 ...
随机推荐
- Nsight Compute Profilier 分析
profiler报告包含每次内核启动分析期间收集的所有信息.在用户界面中,它包含一个包含常规信息的标题,以及用于在报告页面或单个收集的启动之间切换的控件.默认情况下,报告以选定的详细信息页面开始. 页 ...
- Httprunner的使用
一.httprunner的简介 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试.性能测试.线上监控.持续集成等多种 ...
- 『言善信』Fiddler工具 — 11、Fiddler中Composer功能详解
目录 1.Composer功能介绍 2.Composer界面说明 3.使用方式 (1)自定义Request请求 (2)Composer重复发送请求 (3)Composer篡改请求数据 1.Compos ...
- P1091 [NOIP2004 提高组] 合唱队形
题目描述 $N$位同学站成一排,音乐老师要请其中的$\left ( N-K\right )$位同学出列,使得剩下的$K$位同学排成合唱队形. 合唱队形是指这样的一种队形:设$K$位同学从左到右依次编号 ...
- 【C++】Vector判断元素是否存在,去重,求交集,求并集
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> //sort函数.交并补函数 4 ...
- ES6中的字符串
字符串在编程语言中,一直是使用率很高的数据,虽说函数在js语言中是一等公民,但是字符串,怎么也能算得上是千年老二,下面我就一起和大家探讨一下在es6中的一些新用法和特性,特别是在字符串拼接的时候,会解 ...
- 怎么回答面试官:你对Spring的理解?
最近看了点Spring的源码,正好来稍微扯一扯,帮一部分培训班的朋友撕开一道口子,透透气.我自己都是看的培训班视频,所以也算培训班出身吧.所以下文开口闭口"培训班",不要觉得是我在 ...
- noip2010 总结
机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词 ...
- 如何在国产龙芯架构平台上运行c/c++、java、nodejs等编程语言
高能预警:本文内容过于硬核,涉及编译器原理.cpu指令集.机器码.编程语言原理.跨平台原理等计算机专业基础知识,建议具有c.c++.java.nodejs等多种编程语言开发能力,且实战经验丰富的资深开 ...
- Unity Lamba错误集
请先看对象层级视图 /// <summary> /// 选择关卡的内容对象 /// </summary> public Transform Transform_levelCon ...