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. js获取?后面具体参数的值

    function getURLParam(name) {         return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...

  2. 2016 CCPC长春重现赛

    1.2016中国大学生程序设计竞赛(长春)-重现赛 2.总结:会做的太少,应变能力也不行,或者说猜题目的能力不行 02  水 04  HDU 5914  Triangle 1.题意:1~n,n个数,问 ...

  3. 初学者对于MVC架构模式学习与理解

    理解MVC的工作原理,明白一个网页是如何显示出来的 之前一直盲目的在慕课上看视频,脑袋里想着要理解mvc,看了mvc相关的视频,看完之后就觉得空白白的,M,V,C各代表什么我知道,但是这个究竟有啥意思 ...

  4. android 开发项目笔记1

    1.xml文件中@string/name   @+id/name  @id/name  的用法与区别: @string/name  一般长用于从别的资源中获取键值对 @+id/name  为控件指定名 ...

  5. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...

  6. ORA-12519: TNS:no appropriate service handler found 解决(转)

    可能是数据库上当前的连接数目已经超过了它能够处理的最大值. select count(*) from v$process --当前的连接数 select value from v$parameter  ...

  7. python单元测试unittest

    单元测试作为任何语言的开发者都应该是必要的,因为时隔数月后再回来调试自己的复杂程序时,其实也是很崩溃的事情.虽然会很快熟悉内容,但是修改和 调试将是一件痛苦的事情,如果你在修改了代码后出现问题的话,而 ...

  8. context上下文 php版解释

    context翻译为上下文其实不是很好,只是翻译理解大概的作用,对于开发来说,context是对定义的使用的变量,常量或者说是配置, 部分的函数功能除了缺省值之外,往往需要手动设置一些定义量来配合当前 ...

  9. Apache服务器性能监控

    Apache服务器性能监控 1.使用自带mod_status模块监控 1)加载mod_status.so 模块 在httpd.conf中打开LoadModule status_module modul ...

  10. iOS用户信息单例的创建

    UserInfo.h + (UserInfo *) sharedInstance; UserInfo.m #import "UserInfo.h" static UserInfo ...