在Lucene对文本进行处理的过程中,可以大致分为三大部分:

1、索引文件:提取文档内容并分析,生成索引

2、搜索内容:搜索索引内容,根据搜索关键字得出搜索结果

3、分析内容:对搜索词汇进行分析,生成Quey对象。

注:事实上,除了最基本的完全匹配搜索以外,其它都需要在搜索前进行分析。

如不加分析步骤,则搜索JAVA,是没有结果的,因为在索引过程中已经将词汇均转化为小写,而此处搜索时则要求关键字完全匹配。

使用了QueryParser类以后,则根据Analyzer的具体实现类,对搜索词汇进行分析,如大小写转换,java and ant等的搜索词解释等。

一、索引文件

基本步骤如下:

1、创建索引库IndexWriter

2、根据文件创建文档Document

3、向索引库中写入文档内容

package com.ljh.search.index;import java.io.File;
import java.io.FileReader;
import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
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;
import org.apache.lucene.util.Version;// 1、创建索引库IndexWriter
// 2、根据文件创建文档Document
// 3、向索引库中写入文档内容public class IndexFiles {public static void main(String[] args) throws IOException {String usage = "java IndexFiles"+ " [-index INDEX_PATH] [-docs DOCS_PATH] \n\n"+ "This indexes the documents in DOCS_PATH, creating a Lucene index"+ "in INDEX_PATH that can be searched with SearchFiles";String indexPath = null;String docsPath = null;for (int i = 0; i < args.length; i++) {if ("-index".equals(args[i])) {indexPath = args[i + 1];i++;} else if ("-docs".equals(args[i])) {docsPath = args[i + 1];i++;}}if (docsPath == null) {System.err.println("Usage: " + usage);System.exit(1);}final File docDir = new File(docsPath);if (!docDir.exists() || !docDir.canRead()) {System.out.println("Document directory '"+ docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");System.exit(1);}IndexWriter writer = null;try {// 1、创建索引库IndexWriterwriter = getIndexWriter(indexPath);index(writer, docDir);} catch (IOException e) {e.printStackTrace();} finally {writer.close();}}private static IndexWriter getIndexWriter(String indexPath)throws IOException {Directory indexDir = FSDirectory.open(new File(indexPath));IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48,new StandardAnalyzer(Version.LUCENE_48));IndexWriter writer = new IndexWriter(indexDir, iwc);return writer;}private static void index(IndexWriter writer, File file) throws IOException {if (file.isDirectory()) {String[] files = file.list();if (files != null) {for (int i = 0; i < files.length; i++) {index(writer, new File(file, files[i]));}}} else {// 2、根据文件创建文档DocumentDocument doc = new Document();Field pathField = new StringField("path", file.getPath(),Field.Store.YES);doc.add(pathField);doc.add(new LongField("modified", file.lastModified(),Field.Store.NO));doc.add(new TextField("contents", new FileReader(file)));System.out.println("Indexing " + file.getName());// 3、向索引库中写入文档内容writer.addDocument(doc);}}}

(1)使用“java indexfiles -index d:/index -docs d:/tmp”运行程序,索引d:/tmp中的文件,并将索引文件放置到d:/index。

(2)上述生成的索引文件可以使用Luke进行查看。目前Luke已迁移至github进行托管。

二、搜索文件

1、打开索引库IndexSearcher
2、根据关键词进行搜索
3、遍历结果并处理

package com.ljh.search.search;//1、打开索引库IndexSearcher
//2、根据关键词进行搜索
//3、遍历结果并处理
import java.io.File;
import java.io.IOException;import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;public class Searcher {public static void main(String[] args) throws IOException {String indexPath = null;String term = null;for (int i = 0; i < args.length; i++) {if ("-index".equals(args[i])) {indexPath = args[i + 1];i++;} else if ("-term".equals(args[i])) {term = args[i + 1];i++;}}System.out.println("Searching " + term + " in " + indexPath);// 1、打开索引库Directory indexDir = FSDirectory.open(new File(indexPath));IndexReader ir = DirectoryReader.open(indexDir);IndexSearcher searcher = new IndexSearcher(ir);// 2、根据关键词进行搜索TopDocs docs = searcher.search(new TermQuery(new Term("contents", term)), 20);// 3、遍历结果并处理ScoreDoc[] hits = docs.scoreDocs;System.out.println(hits.length);for (ScoreDoc hit : hits) {System.out.println("doc: " + hit.doc + " score: " + hit.score);}ir.close();}}

三、分析

事实上,除了最基本的完全匹配搜索以外,其它都需要在搜索前进行分析。

如不加分析步骤,则搜索JAVA,是没有结果的,因为在索引过程中已经将词汇均转化为小写,而此处搜索时则要求关键字完全匹配。

使用了QueryParser类以后,则根据Analyzer的具体实现类,对搜索词汇进行分析,如大小写转换,java and ant等的搜索词解释等。

分析过程有2个基本步骤:

1、生成QueryParser对象

2、调用QueryParser.parse()生成Query()对象。

具体代码,将下述代码:

     // 2、根据关键词进行搜索TopDocs docs = searcher.search(new TermQuery(new Term("contents", term)), 20);

用以下代替:

     // 2、根据关键词进行搜索/*TopDocs docs = searcher.search(new TermQuery(new Term("contents", term)), 10);*/QueryParser parser = new QueryParser(Version.LUCENE_48, "contents", new SimpleAnalyzer(Version.LUCENE_48));Query query = null;try {query = parser.parse(term);} catch (ParseException e) {e.printStackTrace();}TopDocs docs = searcher.search(query, 30);

【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作相关推荐

  1. 【Lucene4.8教程之中的一个】使用Lucene4.8进行索引及搜索的基本操作

    版权声明:本文为博主原创文章.转载请注明来自http://blog.csdn.net/jediael_lu/ https://blog.csdn.net/jediael_lu/article/deta ...

  2. 【Lucene4.8教程之二】索引

    一.基础内容 0.官方文档说明 (1)org.apache.lucene.index provides two primary classes: IndexWriter, which creates ...

  3. 【Lucene4.8教程之三】搜索

    1.关键类 Lucene的搜索过程中涉及的主要类有以下几个: (1)IndexSearcher:执行search()方法的类 (2)IndexReader:对索引文件进行读操作,并为IndexSear ...

  4. Elasticsearch教程-从入门到精通-ES索引迁移

    一.关于搜索引擎 各位知道,搜索程序一般由索引链及搜索组件组成. 索引链功能的实现需要按照几个独立的步骤依次完成:检索原始内容.根据原始内容来创建对应的文档.对创建的文档进行索引. 搜索组件用于接收用 ...

  5. 数据库系统原理与应用教程(037)—— MySQL 的索引(三):删除索引

    数据库系统原理与应用教程(037)-- MySQL 的索引(三):删除索引 目录 数据库系统原理与应用教程(037)-- MySQL 的索引(三):删除索引 一.删除索引的命令 二.使用 alter ...

  6. 【Lucene4.8教程之四】分析

    1.基础内容 (1)相关概念 分析(Analysis),在Lucene中指的是将域(Field)文本转换成最基本的索引表示单元--项(Term)的过程.在搜索过程中,这些项用于决定什么样的文档能够匹配 ...

  7. 【Lucene4.8教程之五】Luke

    一.Luke基本内容 1.Luke简介 Luke可用于查看Lucene创建的索引,并对其进行基本操作. 2.创建Luke (1)从Github上下载源文件 https://github.com/tar ...

  8. 【Lucene4.8教程之六】QueryParser与Query子类:如何生成Query对象

    一.概述 1.对于一个搜索而言,其核心语句为: searcher.search(query, 10); 此时,其最重要的参数为一个Qeury对象.构造一个Query对象有2种方法: (1)使用Quer ...

  9. Lucene-4.8.1+paoding-analysis菜鸟试验:中文索引和查询

    庖丁中文分词库是一个使用Java开发的,可结合到Lucene应用中的,为互联网.企业内部网使用的中文搜索引擎分词组件.Paoding填补了国内中文分词方面开源组件的空白,致力于此并希翼成为互联网网站首 ...

最新文章

  1. 利用mysql建立随机森林_随机森林算法实例 - osc_4imme0wh的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. 基于队列的生产消费设计java_生产者-消费者设计模式
  3. 5.3.1计算机网络传输层之TCP可靠传输
  4. 「Python」python调用单个C++文件生成的动态库(.so)Part I
  5. 2022年终结版WPF项目实战合集发布
  6. 局域网聊天的本质是函数
  7. Ceres和g2o的配置和使用
  8. TaskBarProgress(任务栏进度条)
  9. 读书笔记:非营利组织的管理
  10. 2019-07-10
  11. 35行的山寨版jQuery
  12. 老板突然出现,游戏飞速隐藏,开源神器在手,摸鱼不怕被抓包
  13. 详解汽车外饰行业如何利用MES系统进行生产防错?
  14. python制作二维码生成器3.0
  15. C语言中puts跟printf的区别
  16. cmd静默运行_如何在Win10上静默运行批处理文件
  17. jenkins使用时遇到“using GIT_ASKPASS to set credentials”
  18. django+vue全
  19. codeblocks20版本无法找到编译器的解决方法!
  20. POLARDB:向着更快、更高、更强不断前行!

热门文章

  1. 【完整可运行源码+GIF动画演示】十大经典排序算法系列——冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序、计数排序、桶排序、基数排序
  2. 16行代码AC_Keeping Rabbits Gym - 102394K(附超时原因)
  3. python连接阿里云数据库_python连接阿里云数据库
  4. 网络的分层思想和数据封装与解封装概论
  5. devexpress能开发出html,DevExpress推出HTML5 JavaScript控件集
  6. 查看mysql SQL物理读_Oracle查看逻辑读、物理读资源占用排行的SQL语句
  7. web站点放入html页面,HTML
  8. android点击通知跳转到服务,Android 接收推送消息跳转到指定页面的方法
  9. jersey球衣是什么_球衣知识------关于NIKE球衣(Jersey)的介绍
  10. mysql的函数用途_MYSQL小函数大用途之-------FIND_IN_SET