Lucene的大致结构图:

信息写入索引库的过程:

读取信息的过程:

以下是一个向索引库写入信息与读取信息的样例:

public void testCreateIndex() throws Exception{
/**
* 1、创建一个student对象,而且把信息存放进去
* 2、调用indexWriter的API把数据存放在索引库中
* 3、关闭indexWriter
*/
// 创建一个Student对象。而且把信息存放进去
Student student = new Student();
student.setId(1L);
student.setName("张三");
// 调用indexWriter的API把数据存放在索引库中
/**
* 创建一个IndexWriter
* 參数三个 1、索引库, 指向索引库的位置 2、分词器
*/
// 创建索引库
Directory directory = FSDirectory.open(new File("./indexDir"));
// 创建分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
// 把一个student对象转化成document
Document document = new Document();
Field idField = new Field("id",student.getId().toString(),Store.YES,Index.NOT_ANALYZED);
Field nameField = new Field("name",student.getName(),Store.YES,Index.ANALYZED);
document.add(idField);
document.add(nameField);
indexWriter.addDocument(document);
// 关闭indexWriter
indexWriter.close();
}

public void testSearchIndex() throws Exception{
/**
* 1、创建一个IndexSearch对象
* 2、调用search方法进行检索
* 3、输出内容
*/
// 创建一个 IndexSearch对象
Directory directory = FSDirectory.open(new File("./indexDir"));
IndexSearcher indexSearcher = new IndexSearcher(directory);
// 调用search方法进行检索
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30,"name",analyzer);
Query query = queryParser.parse("张"); // 要查找的关键词
TopDocs topDocs = indexSearcher.search(query, 2); // 前两条
int count = topDocs.totalHits; // 依据关键词查询出来的总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Student> studentList = new ArrayList<Student>();
for(ScoreDoc scoreDoc:scoreDocs){
float score = scoreDoc.score; // 关键词得分
int index = scoreDoc.doc; // 索引的下标
Document document = indexSearcher.doc(index);
// 把document转化成Student
Student student = new Student();
student.setId(Long.parseLong(document.get("id"))); // document.getField("id").stringValue()
student.setTitle(document.get("name"));
studentList.add(student);
} for(Student student:studentList){
System.out.println(student.getId());
System.out.println(student.getName());
}
}
说明:
1、索引库的增、删、改是由indexWriter来操作的
2、同一个时刻内。同一个索引库,仅仅能同意一个indexWriter操作
3、当IndexWriter创建完毕以后,indexwriter所指向的索引库就被占据了。仅仅有当indexWriter.close时。才干释放锁的资源
4、当一个新的indexWriter想拥有索引库时,原来的indexWriter必须释放锁
5、仅仅要索引库中存在write.lock文件,说明上锁了
6、indexWriter.close有两层含义:1. 关闭IO资源; 2.释放锁

文件索引库和内存索引库的结合 :
1、能不能设置非常多个索引库 
       能够设置非常多个索引库
2、索引库能不能合并起来
       假设是内存索引库
       Directory ramDirectory = new RamDirectory(Directory d);
       这样就能够把一个索引库放入到内存索引库中
       利用IndexWriter.addIndexesNoOptimize方法能够把非常多个索引库进行合并操作
3、应用程序能不能在内存中和索引库进行交互

Author:顾故

Sign:别输给以前的自己

【Lucene】具体解释Lucene全文检索的信息写入与读取的更多相关文章

  1. Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

    2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程 2014-12-07 23:39 2623人阅读 评论(0) ...

  2. Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息

    Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息 在此回复牛妞的关于程序中分词器的问题,其实可以直接很简单的在词库中配置就好了,Lucene中分词的所有信息我们都可以从 ...

  3. Lucene 01 - 初步认识全文检索和Lucene

    目录 1 搜索简介 1.1 搜索实现方案 1.2 数据查询方法 1.2.1 顺序扫描法 1.2.2 倒排索引法(反向索引) 1.3 搜索技术应用场景 2 Lucene简介 2.1 Lucene是什么 ...

  4. Lucene核心--构建Lucene搜索(上篇,理论篇)

    2.1构建Lucene搜索 2.1.1 Lucene内容模型 一个文档(document)就是Lucene建立索引和搜索的原子单元,它由一个或者多个字段(field)组成,字段才是Lucene的真实内 ...

  5. Lucene4.6 把时间信息写入倒排索引的Offset偏移量中,并实现按时间位置查询

    有个新的技术需求,需要对Lucene4.x的源码进行扩展,把如下的有时间位置的文本写入倒排索引,为此,我扩展了一个TimeTokenizer分词器,在这个分词器里将时间信息写入 偏移量Offset中. ...

  6. Lucene系列二:Lucene(Lucene介绍、Lucene架构、Lucene集成)

    一.Lucene介绍 1. Lucene简介 最受欢迎的java开源全文搜索引擎开发工具包.提供了完整的查询引擎和索引引擎,部分文本分词引擎(英文与德文两种西方语言).Lucene的目的是为软件开发人 ...

  7. java将错误信息写入文件

    第一种办法可以通过字符串,也就是先把错误信息写入字符串,再将字符串写入文件 import java.io.*; public class Demo { public static void main( ...

  8. ZH奶酪:PHP error_log()将错误信息写入日志文件

    error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段. bool error_log ( string $message [, int $messag ...

  9. 将不同级别的logging 日志信息写入不同文件

    将不同级别的logging 日志信息写入不同文件 # -*- coding: utf-8 -*- import os import time from logging.handlers import ...

随机推荐

  1. hihoCoder-1089-Floyd

    我们读入的时候,要考虑重边的问题,我们只取边的最小值就可以了. #include <cstdio> #include <cstring> const int INF = 0x3 ...

  2. 递归函数&二分查找

    一.递归函数 1)定义 在函数中调用函数本身,就是递归 在python中递归的深度最大为1000,但实际达不到1000 def func(): print("-----func-----&q ...

  3. mat 和IPIImage之间的转换

    opencv2.3.1 Mat::operator IplImageCreates the IplImage header for the matrix.C++: Mat::operator IplI ...

  4. Luogu3195 [HNOI2008]玩具装箱TOY (方程变形 + 斜率优化 )

    题意: 给出一个序列 {a[i]} 把其分成若干个区间,每个区间的价值为 W = (j − i + ∑ak(i<=k<=j) - L)​2 ,求所有分割方案中价值之和的最小值. 细节: 仔 ...

  5. addEvenListener('DOMContentLoaded',function(){})

  6. Java-构造一个字符串

    实用StringBuffer构造字符串 package com.tj; public class MyClass implements Cloneable { public static void m ...

  7. Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  8. python3--__repr_和__str__会返回字符串表达形式

    __repr_和__str__会返回字符串表达形式 下一个例子是已经见过的init构造方法和add重载方法,本例也会定义返回实例的字符串表达形式的__repr__方法.字符串格式把self.data对 ...

  9. Leetcode 332.重新安排行程

    重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...

  10. CF802D

    D. Marmots (easy) time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...