1. package com.cmy.lucene.lucene;
  2.  
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.nio.file.Paths;
  6.  
  7. import org.apache.lucene.analysis.Analyzer;
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  9. import org.apache.lucene.document.Document;
  10. import org.apache.lucene.document.Field;
  11. import org.apache.lucene.document.TextField;
  12. import org.apache.lucene.index.IndexWriter;
  13. import org.apache.lucene.index.IndexWriterConfig;
  14. import org.apache.lucene.store.Directory;
  15. import org.apache.lucene.store.FSDirectory;
  16.  
  17. public class Indexer {
  18.  
  19. private IndexWriter writer;
  20.  
  21. /**
  22. * 构造方法,实例化indexwriter
  23. * @param indexDir
  24. * @throws Exception
  25. */
  26. public Indexer(String indexDir) throws Exception{
  27. Directory directory = FSDirectory.open(Paths.get(indexDir));
  28. Analyzer analyzer = new StandardAnalyzer();//标准分词器
  29. IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
  30. writer = new IndexWriter(directory, indexWriterConfig);
  31. }
  32.  
  33. /**
  34. *
  35. * @throws Exception
  36. */
  37. public void close() throws Exception{
  38. writer.close();
  39. }
  40.  
  41. /**
  42. *
  43. * @param dataDir
  44. * @throws Exception
  45. */
  46. public int index(String dataDir) throws Exception{
  47. File []files = new File(dataDir).listFiles();
  48. for(File file:files){
  49. IndexFile(file);
  50. }
  51. return writer.numDocs();//返回索引文件的数量
  52. }
  53.  
  54. /**
  55. * 索引指定文件
  56. * @param file
  57. * @throws Exception
  58. */
  59. private void IndexFile(File file) throws Exception {
  60. System.out.println("索引文件:"+file.getCanonicalPath());//返回规范化的绝对路径
  61. Document document = getDocument(file);
  62. writer.addDocument(document);;
  63. }
  64.  
  65. /**
  66. * 获取文档,文档里再设置每个字段
  67. * @param file
  68. * @return
  69. */
  70. private Document getDocument(File file) throws Exception{
  71. Document document = new Document();//定义文档对象
  72. document.add(new TextField("contents",new FileReader(file)));//在文档中引入字段(key,value)形式
  73. document.add(new TextField("fileName",file.getName(),Field.Store.YES));
  74. document.add(new TextField("fullPath",file.getCanonicalPath(),Field.Store.YES));
  75. return document;
  76. }
  77.  
  78. public static void main(String[] args) {
  79. String indexDir = "D:\\lucene";
  80. String dataDir = "E:\\JavaEE\\luceneData";
  81. Indexer indexer = null;
  82. int numIndexed = 0;
  83. long start = System.currentTimeMillis();
  84. try {
  85. indexer = new Indexer(indexDir);
  86. numIndexed = indexer.index(dataDir);
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. e.printStackTrace();
  90. }finally {
  91. try {
  92. indexer.close();
  93. } catch (Exception e2) {
  94. e2.printStackTrace();
  95. }
  96. }
  97. long end = System.currentTimeMillis();
  98. System.out.println("索引: "+numIndexed+" 个文件,话费了"+(end-start)+" s");
  99. }
  100. }

  1. package com.cmy.lucene.lucene;
  2.  
  3. import java.nio.channels.ScatteringByteChannel;
  4. import java.nio.file.Paths;
  5.  
  6. import org.apache.lucene.analysis.Analyzer;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.index.DirectoryReader;
  10. import org.apache.lucene.index.IndexReader;
  11. import org.apache.lucene.queryparser.classic.QueryParser;
  12. import org.apache.lucene.search.IndexSearcher;
  13. import org.apache.lucene.search.Query;
  14. import org.apache.lucene.search.ScoreDoc;
  15. import org.apache.lucene.search.TopDocs;
  16. import org.apache.lucene.store.Directory;
  17. import org.apache.lucene.store.FSDirectory;
  18.  
  19. public class Searcher {
  20.  
  21. public static void search(String indexDir,String qString) throws Exception{
  22.  
  23. Directory directory = FSDirectory.open(Paths.get(indexDir));
  24. IndexReader reader = DirectoryReader.open(directory);//读取完整路径下的reader
  25. IndexSearcher iSearcher = new IndexSearcher(reader);//索引查询器,参数是Indexreader
  26. Analyzer analyzer = new StandardAnalyzer();//标准分词器
  27. QueryParser parser = new QueryParser("contents", analyzer);//解析制定内容,使用制定分词器
  28. Query query = parser.parse(qString);
  29. long start = System.currentTimeMillis();
  30. TopDocs hits = iSearcher.search(query, 10);//传入query对象,返回的数据数量,此处返回前十条,哎,那总该有个顺序吧,怎么搞
  31. long end = System.currentTimeMillis();
  32. System.out.println("匹配"+qString+",总共花费"+(end-start)+" 毫秒");
  33. //遍历结果集,获取文档
  34. for(ScoreDoc scoreDoc:hits.scoreDocs){
  35. Document document = iSearcher.doc(scoreDoc.doc);//获取结果集中的doc主键(id)并据此查询获取文档对象
  36. System.out.println("fullPath: "+document.get("fullPath"));//获取完整的fullPath,
  37.  
  38. }
  39. reader.close();
  40. }
  41. public static void main(String[] args) {
  42. String indexDir = "D:\\lucene";
  43. String dataDir = "Zygmunt Saloni";
  44. try {
  45. search(indexDir,dataDir);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }

一: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. db2无法force掉备份连接的处理办法

    在数据库在线备份的时候会与Load和ALTER TABLE <表名> ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE发生冲突导致这两种操作被挂 ...

  2. python字符串

    字符串格式化 字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典) >>> format = "hello, %s. % ...

  3. java分享第七天-01(Hashmap和Hashtable的区别&Property)

    一.Hashmap和Hashtable的区别 1 主要:Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,非同步,效率相对高 2 父类:Hashtable是Dictionary ...

  4. Lua字符串库

    1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结 ...

  5. javaScript代码执行顺序

    javaScript是一种描述型脚本语言,由浏览器进行动态的解析和执行. 页面加载过程中,浏览器会对页面上载入的每个js代码块进行扫描. JavaScript是一段一段的分析执行的,在分析执行同一段代 ...

  6. Java POI导入导出Excel

    1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...

  7. Apache Spark技术实战之8:Standalone部署模式下的临时文件清理

    未经本人同意严禁转载,徽沪一郎. 概要 在Standalone部署模式下,Spark运行过程中会创建哪些临时性目录及文件,这些临时目录和文件又是在什么时候被清理,本文将就这些问题做深入细致的解答. 从 ...

  8. 中科院开源协会镜像站 Android SDK镜像

    中科院开源协会镜像站 Android SDK镜像测试发布 https://forum.opencas.org/t/184

  9. javascript入门:prototype和面向对象的实现

    由于工作需要,需要大量使用javascript,于是对其进行了一下学习. 学习任何一个语言,最重要的是掌握其和其他语言不同的关键特性.对javascript来说,我总结就是prototype.就像me ...

  10. a chip multiprocessor

    COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A multicore computer ...