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. Unity3d与Android交互

    先看下效果 你一定会说,然并卵! 没错,这里只是一个最简单的例子,unity与android activity 互相传参数. 玩过手游的都知道,在你要为你心爱的游戏角色准备花钱买钻石,点击购买的时候, ...

  2. ZeroMQ接口函数之 :zmq_strerror - 获取ZMQ错误描述字符串

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_strerror zmq_strerror(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_ ...

  3. 读书笔记 —— 《css秘密花园》

    浏览器兼容性有效性信息查询 : Can I Use? http://caniuse.com/ 自动为css添加浏览器厂商前缀 https://autoprefixer.github.io/ 在线编辑H ...

  4. Android-adb指令

    adb概念: adb的全称为Android Debug Bridge(调试桥):通过adb我们可以在Eclipse中方便通过DDMS来调试Android程序.当我们运行Eclipse时ADB进程   ...

  5. 第十周 psp

    团队项目PSP 一:表格     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论用户界面 8:45 10:55 40 35 90 分析与 ...

  6. 【iCore3应用开发平台】发布 iCore3 应用开发平台使用说明

    PDF下载地址:http://pan.baidu.com/s/1c2ca2lU

  7. NC57,NC63-NC二开经验总结

    版主2010级市场营销专业本科生 2013年8月入达内培训Java相关技术 12月入职,做用友NC的二次开发工作 2015年4月离职,4中下旬入职一家互联网金融企业 下面是做NC二开期间积累的一些常用 ...

  8. php-fpm重启关闭等操作

    php-fpm 启动:/usr/sbin/php-fpmphp-fpm 关闭:kill -INT `cat /var/run/php-fpm.pid`php-fpm 重启:kill -USR2 `ca ...

  9. 带你玩转JavaWeb开发之六-mysql基本语法详解及实例(2)

    1.1.1    对数据库中表的记录进行操作(*****) 1.1.1.1   对数据库中表记录插入操作 [语法] 向数据库表中插入某些列:insert into 表名 (列名1,列名2,列名3-) ...

  10. Mysql 获取年级每个班前十学生的信息

    select * from Table1 a where 10>(select count(*) from Table1 where ClsNo=a.ClsNo and Score>a.S ...