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初体验的更多相关文章

  1. .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验

    不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...

  2. Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验

    Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...

  3. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  4. Xamarin.iOS开发初体验

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0

  5. 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...

  6. 【Knockout.js 学习体验之旅】(1)ko初体验

    前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...

  7. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  8. 百度EChart3初体验

    由于项目需要在首页搞一个订单数量的走势图,经过多方查找,体验,感觉ECharts不错,封装的很细,我们只需要看自己需要那种类型的图表,搞定好自己的json数据就OK.至于说如何体现出来,官网的教程很详 ...

  9. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

随机推荐

  1. WebRTC手记之初探

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4364553.html WebRTC是HTML5支持的重要特性之一,有了它,不再需要借助音视频相关的客户端,直接通过浏 ...

  2. 【DP】HDU 1087

    HDU 1078 Super Jumping! Jumping! Jumping! 题意: 有这么个游戏,从start到end(自己决定在哪停下来)连续跳圈,中间不能空一个圈不跳,圈里的数字必须比你上 ...

  3. 常用的Meta标签写法和作用

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  4. 问题(the question)

    I want to know: 1. 软件开发最主要的目的 是什么? 2. 软件开发是否同样需要模,和架构? 3.软件开发与其他程序的需求分析是否相似,有什么不同之处? 4.软件开发最常用的计算机语言 ...

  5. USACO翻译:USACO 2014 MARCH GOLD P2 Sabotage

    1.破坏{DOLD题2} sabotage.pas/c/cpp [问题描述] 农夫约翰的头号敌人保罗决定破坏农民约翰的挤奶设备.挤奶设备排成一行,共N(3<= N <=100000)台挤奶 ...

  6. SQL Injection(SQL注入漏洞)

    审计前准备: 1.安�php程序(推荐phpStudy) 2.高亮编辑器(推荐 Sublimetext Notepad++) 3.新建一个文本,复制以下变量,这些变量是审计中需要在源码中寻找的 ### ...

  7. Visual Studio 2010编译时总是提示"调用目标发生了异常"的解决

    现象: 无论建立的是Win32 Console的解决方案,还是MFC的解决方案,重新打开Visual Studio 2010之后,编译时总是提示“调用的目标发生了异常” 解决: 1. 关闭Visual ...

  8. requirejs:研究笔记

    模块化历史 模块化异步加载方式 后期维护 查找问题 复用代码 防止全局变量的污染 http://requirejs.cn/ http://requirejs.org/ 我的目录结构 总体步骤 < ...

  9. amCharts图表组件

    amCharts提供了JavaScript/HTML5 Charts.Javascript/HTML5 Stock Chart.JavaScript Maps三种图表组件.amCharts图形效果炫丽 ...

  10. VirtualBox + CentOS 使用 NAT + Host-Only 方式联网

    之前一直使用 VMware 作为虚拟机,这几天看<跟阿铭学Linux>,里面用的是虚拟机是 Oracle VirtualBox,也跟着安装配置一个,但是比较坑的是照着上面的配置折腾了很久才 ...