根据Lucene 6.1.0 及JDK 1.8最新版本,结合前几篇文章所写的最新Demo.
代码如下:

package HighLightSearch;import java.io.File;
import java.io.FileReader;
import java.nio.file.FileSystems;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.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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;class Index {// 建立索引public void index() {IndexWriter indexWriter = null;try {// 1、创建Directory//JDK 1.7以后 open只能接收PathDirectory directory = FSDirectory.open(FileSystems.getDefault().getPath("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/index"));// 2、创建IndexWriterAnalyzer analyzer = new StandardAnalyzer();IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);indexWriter = new IndexWriter(directory, indexWriterConfig);indexWriter.deleteAll();//清除以前的index//要搜索的File路径File dFile = new File("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/data");File[] files = dFile.listFiles();for (File file : files) {// 3、创建Document对象Document document = new Document();// 4、为Document添加Field// 第三个参数是FieldType 但是定义在TextField中作为静态变量,看API也不好知道怎么写document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED));document.add(new Field("filename", file.getName(), TextField.TYPE_STORED));document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED));// 5、通过IndexWriter添加文档到索引中indexWriter.addDocument(document);}} catch (Exception e) {e.printStackTrace();} finally {try {if (indexWriter != null) {indexWriter.close();}} catch (Exception e) {e.printStackTrace();}}}
}/**  创建Directory创建IndexReader    根据IndexReader创建IndexSearch        创建搜索的Query 根据searcher搜索并且返回TopDocs  根据TopDocs获取ScoreDoc对象        根据searcher和ScoreDoc对象获取具体的Document对象          根据Document对象获取需要的值* */class Search {  /** * 搜索 */  public void search(String keyWord) {  DirectoryReader directoryReader = null;  try {  // 1、创建Directory  Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/index"));// 2、创建IndexReader  directoryReader = DirectoryReader.open(directory);  // 3、根据IndexReader创建IndexSearch  IndexSearcher indexSearcher = new IndexSearcher(directoryReader);  // 4、创建搜索的Query  Analyzer analyzer = new StandardAnalyzer();  // 创建parser来确定要搜索文件的内容,第一个参数为搜索的域  QueryParser queryParser = new QueryParser("content", analyzer);  // 创建Query表示搜索域为content包含UIMA的文档  Query query = queryParser.parse(keyWord);  // 5、根据searcher搜索并且返回TopDocs  TopDocs topDocs = indexSearcher.search(query, 10);  System.out.println("查找到的文档总共有:"+topDocs.totalHits);// 6、根据TopDocs获取ScoreDoc对象  ScoreDoc[] scoreDocs = topDocs.scoreDocs;  for (ScoreDoc scoreDoc : scoreDocs) {  // 7、根据searcher和ScoreDoc对象获取具体的Document对象  Document document = indexSearcher.doc(scoreDoc.doc);  // 8、根据Document对象获取需要的值  System.out.println(document.get("filename") + " " + document.get("filepath"));  }  } catch (Exception e) {  e.printStackTrace();  } finally {  try {  if (directoryReader != null) {  directoryReader.close();  }  } catch (Exception e) {  e.printStackTrace();  }  }  }
}  public class HighLightSearch {public static void main(String args[]) {Index newIndex = new Index();newIndex.index();Search newSearch = new Search();newSearch.search("UIMA");}
}

运行结果及所用JRE如下所图:

Lucene 6.1.0 入门Demo相关推荐

  1. Dubbo入门Demo

    2019独角兽企业重金招聘Python工程师标准>>> 1.Dubbo简单介绍 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方 ...

  2. lucene使用3.0.3_使用Apache Lucene 4.3轻松进行搜索

    lucene使用3.0.3 Lucene是用Java编写的全文搜索引擎,可以为任何应用程序提供强大的搜索功能. Lucene的核心是基于文件的全文本索引. Lucene提供API创建该索引,然后向该索 ...

  3. dubbo web工程示例_分布式开发-Zooker+dubbo入门-Demo

    作者:知了堂-刘阳 1.什么是SOA架构 SOA 是Service-Oriented Architecture的首字母简称,它是一个面向服务的架构模式(俗称:分布式:面服务的分布式) 为什么互联网项目 ...

  4. Away3D4.0入门教程

    好吧,我知道有很多人不会有兴趣一点点把教程全看完,所以,我先在这帖子里集中发布一些基础的,但大家又非常关心的问题, 至于具体细节怎么实现,我会在以后的教程中慢慢完善,这里仅是为了提供给那些有一定动手能 ...

  5. Pytorch1.1.0 入门 自定义op(python)

    因为需求,需要调研tensorRT与ONNX关于自定义层的方法.经过之前的调研,首先,关于onnx,开发者手册中的介绍有限,在已知的demo中没有关于onnx自定义层的,详情见TensorRT 5.1 ...

  6. 分布式事务框架lcn入门demo

    文章目录 简介 实现原理 入门demo 简介 LCN分布式事务框架其本身并不创建事务,而是基于对本地事务的协调从而达到事务一致性的效果. LCN5.0.2有3种模式,分别是LCN模式,TCC模式,TX ...

  7. Lucene 2.2.0发布自带的HTMLParser的使用

    Lucene 2.2.0发行包中自带的org.apache.lucene.demo包中,有一个为HTML和TXT文件建立索引.删除索引以及实现检索的实例,其中在org.apache.lucene.de ...

  8. air调用java,AIR2.0入门教程:与Java应用交互

    在之前的一篇文章中,我介绍了如何使用AIR2.0新增的NativeProcess类与本地进程进行交互和通讯,在那个例子里面我们使用了C++ 的代码,实际上只要是基于命令行的标准输入输出,AIR2.0的 ...

  9. 暑假想打比赛,小白怎么从0入门?

    小白怎么从0入门打比赛?(文末有GPU福利~) 第一步先解决赛题选择的问题:什么样的比赛适合你入门? 我建议想打比赛的同学首先想明白一件事--你想从事什么方向?人的精力是有限的,与其面面俱到,不如在1 ...

最新文章

  1. UBUNTU安装SSH和xrdp
  2. 小程序网络最大并发限制解决思路
  3. css设置并排,CSS并排排列2个div
  4. 【排序算法】计数排序引发的围观风波——一种O(n)的排序
  5. Linux cat指令(用于连接文件并打印到标准输出设备上)
  6. Index of Oracle
  7. 如何清除Windows共享登录的用户名密码记录
  8. ES6 学习笔记(一)let,const和解构赋值
  9. java 内存管理_高性能Java代码之内存管理
  10. 抽象工厂模式_设计模式3之抽象工厂模式
  11. 微信服务号只有域名没有80端口怎么办
  12. 克拉夫斯曼高端定制 刘霞---【YBC中国国际青年创业计划】
  13. 科学家发明创可贴式MP3靠人体热量工作
  14. 百度百科爬虫爬人物信息
  15. 总结知识,提高认知--牛腩总结
  16. node.js学习笔记Day3
  17. 破解不加微信看朋友圈
  18. AMR中的RO,RW和ZI
  19. nvm use不生效
  20. “从零到一“基于Freeswitch二次开发:Freeswitch入门与网络架构 (一)

热门文章

  1. SVN提交代码报错:svnlook: E000002:不能打开文件
  2. 2022个人所得税退税今日开始超过6万不足12万退税吗?退多少?
  3. 高通9008救砖技巧
  4. 循迹小车 。。。。2 为什么步进电机小车很少见呢
  5. JavaScript主要作用是什么?
  6. python怎么换行输出的数字对齐_python中格式化输出和字母大小写转换,对齐填充方式...
  7. 用户界面设计之关于控制UI界面的简单总结
  8. AI可以预测道路交叉口的车辆轨迹
  9. 艾宾浩斯(H.Ebbinghaus)遗忘曲线
  10. win7计算机管理员权限才能删除,Win7系统需要管理员权限才能删除文件夹或复制到此文件夹解决方法...