一:luecne初体验
- package com.cmy.lucene.lucene;
- import java.io.File;
- import java.io.FileReader;
- import java.nio.file.Paths;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.TextField;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- public class Indexer {
- private IndexWriter writer;
- /**
- * 构造方法,实例化indexwriter
- * @param indexDir
- * @throws Exception
- */
- public Indexer(String indexDir) throws Exception{
- Directory directory = FSDirectory.open(Paths.get(indexDir));
- Analyzer analyzer = new StandardAnalyzer();//标准分词器
- IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
- writer = new IndexWriter(directory, indexWriterConfig);
- }
- /**
- *
- * @throws Exception
- */
- public void close() throws Exception{
- writer.close();
- }
- /**
- *
- * @param dataDir
- * @throws Exception
- */
- public int index(String dataDir) throws Exception{
- File []files = new File(dataDir).listFiles();
- for(File file:files){
- IndexFile(file);
- }
- return writer.numDocs();//返回索引文件的数量
- }
- /**
- * 索引指定文件
- * @param file
- * @throws Exception
- */
- private void IndexFile(File file) throws Exception {
- System.out.println("索引文件:"+file.getCanonicalPath());//返回规范化的绝对路径
- Document document = getDocument(file);
- writer.addDocument(document);;
- }
- /**
- * 获取文档,文档里再设置每个字段
- * @param file
- * @return
- */
- private Document getDocument(File file) throws Exception{
- Document document = new Document();//定义文档对象
- document.add(new TextField("contents",new FileReader(file)));//在文档中引入字段(key,value)形式
- document.add(new TextField("fileName",file.getName(),Field.Store.YES));
- document.add(new TextField("fullPath",file.getCanonicalPath(),Field.Store.YES));
- return document;
- }
- public static void main(String[] args) {
- String indexDir = "D:\\lucene";
- String dataDir = "E:\\JavaEE\\luceneData";
- Indexer indexer = null;
- int numIndexed = 0;
- long start = System.currentTimeMillis();
- try {
- indexer = new Indexer(indexDir);
- numIndexed = indexer.index(dataDir);
- } catch (Exception e) {
- e.printStackTrace();
- e.printStackTrace();
- }finally {
- try {
- indexer.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- long end = System.currentTimeMillis();
- System.out.println("索引: "+numIndexed+" 个文件,话费了"+(end-start)+" s");
- }
- }
- package com.cmy.lucene.lucene;
- import java.nio.channels.ScatteringByteChannel;
- import java.nio.file.Paths;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.DirectoryReader;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.queryparser.classic.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;
- public class Searcher {
- public static void search(String indexDir,String qString) throws Exception{
- Directory directory = FSDirectory.open(Paths.get(indexDir));
- IndexReader reader = DirectoryReader.open(directory);//读取完整路径下的reader
- IndexSearcher iSearcher = new IndexSearcher(reader);//索引查询器,参数是Indexreader
- Analyzer analyzer = new StandardAnalyzer();//标准分词器
- QueryParser parser = new QueryParser("contents", analyzer);//解析制定内容,使用制定分词器
- Query query = parser.parse(qString);
- long start = System.currentTimeMillis();
- TopDocs hits = iSearcher.search(query, 10);//传入query对象,返回的数据数量,此处返回前十条,哎,那总该有个顺序吧,怎么搞
- long end = System.currentTimeMillis();
- System.out.println("匹配"+qString+",总共花费"+(end-start)+" 毫秒");
- //遍历结果集,获取文档
- for(ScoreDoc scoreDoc:hits.scoreDocs){
- Document document = iSearcher.doc(scoreDoc.doc);//获取结果集中的doc主键(id)并据此查询获取文档对象
- System.out.println("fullPath: "+document.get("fullPath"));//获取完整的fullPath,
- }
- reader.close();
- }
- public static void main(String[] args) {
- String indexDir = "D:\\lucene";
- String dataDir = "Zygmunt Saloni";
- try {
- search(indexDir,dataDir);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
一:luecne初体验的更多相关文章
- .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...
- Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验
Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...
- Spring之初体验
Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...
- Xamarin.iOS开发初体验
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0
- 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...
- 【Knockout.js 学习体验之旅】(1)ko初体验
前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- 百度EChart3初体验
由于项目需要在首页搞一个订单数量的走势图,经过多方查找,体验,感觉ECharts不错,封装的很细,我们只需要看自己需要那种类型的图表,搞定好自己的json数据就OK.至于说如何体现出来,官网的教程很详 ...
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...
随机推荐
- db2无法force掉备份连接的处理办法
在数据库在线备份的时候会与Load和ALTER TABLE <表名> ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE发生冲突导致这两种操作被挂 ...
- python字符串
字符串格式化 字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典) >>> format = "hello, %s. % ...
- java分享第七天-01(Hashmap和Hashtable的区别&Property)
一.Hashmap和Hashtable的区别 1 主要:Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,非同步,效率相对高 2 父类:Hashtable是Dictionary ...
- Lua字符串库
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- javaScript代码执行顺序
javaScript是一种描述型脚本语言,由浏览器进行动态的解析和执行. 页面加载过程中,浏览器会对页面上载入的每个js代码块进行扫描. JavaScript是一段一段的分析执行的,在分析执行同一段代 ...
- Java POI导入导出Excel
1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...
- Apache Spark技术实战之8:Standalone部署模式下的临时文件清理
未经本人同意严禁转载,徽沪一郎. 概要 在Standalone部署模式下,Spark运行过程中会创建哪些临时性目录及文件,这些临时目录和文件又是在什么时候被清理,本文将就这些问题做深入细致的解答. 从 ...
- 中科院开源协会镜像站 Android SDK镜像
中科院开源协会镜像站 Android SDK镜像测试发布 https://forum.opencas.org/t/184
- javascript入门:prototype和面向对象的实现
由于工作需要,需要大量使用javascript,于是对其进行了一下学习. 学习任何一个语言,最重要的是掌握其和其他语言不同的关键特性.对javascript来说,我总结就是prototype.就像me ...
- a chip multiprocessor
COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A multicore computer ...