1  Lucen目录介绍


lucene-core-3.6.2.jar是lucene开发核心jar包

contrib  目录存放,包含一些扩展jar包


案例

建立第一个Lucene项目:lucene3_day1

(1)需要先将数据转换成为Document对象,每一个数据信息转换成为Field(String
name, String value, Field.Store store, Field.Indexindex)

(2)指定索引库位置Directorydirectory = FSDirectory.open(new
File("index"));// 当前Index目录

(3)分词器Analyzeranalyzer =
new StandardAnalyzer(Version.LUCENE_36);

(4)写入索引:

IndexWriterConfig indexWriterConfig =
new
IndexWriterConfig(

Version.LUCENE_36, analyzer);

IndexWriter indexWriter =
new IndexWriter(directory,indexWriterConfig);

//将document数据写入索引库

indexWriter.addDocument(document);

//关闭索引

indexWriter.close();

案例编写:

案例目录:

Article.java

package cn.toto.lucene.quickstart;

public
class Article {

private
int
id;

private String
title;

private String
content;

/**

* @return the
id

*/

public
int getId() {

return
id;

}

/**

* @param id
the id to set

*/

public
void setId(int
id) {

this.id
= id;

}

/**

* @return the
title

*/

public String getTitle() {

return
title;

}

/**

* @param title
the title to set

*/

public
void setTitle(String title) {

this.title
= title;

}

/**

* @return the
content

*/

public String getContent() {

return
content;

}

/**

* @param content
the content to set

*/

public
void setContent(String content) {

this.content
= content;

}

}

package cn.toto.lucene.quickstart;

import java.io.File;

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.Field.Index;

import org.apache.lucene.document.Field.Store;

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;

import org.junit.Test;

/**

*
@brief LuceneTest.java
测试Lucene的案例

*
@attention

*
@author
toto-pc

*
@date 2014-12-7

*
@note begin modify by
涂作权 2014/12/07 null

*/

public
class LuceneTest {

@Test

public
void buildIndex()
throws Exception {

Article article = new Article();

article.setId(100);

article.setTitle("Lucene快速入门");

article.setContent("Lucene是提供了一个简单却强大的应用程式接口,"

+ "能够做全文检索索引和搜寻,在Java开发环境里Lucene是"
+

"一个成熟的免费的开放源代码工具。");

//
将索引数据转换成为Document对象(Lucene要求)

Document document = new Document();

document.add(new Field("id",
//
字段

article.getId() + "", Store.YES,
//
是否建立索引

Index.ANALYZED
//
表示使用分词索引

));

document.add(new Field("title",
article.getTitle(), Store.YES,Index.ANALYZED));

document.add(new Field("content",
article.getContent(), Store.YES, Index.ANALYZED));

//
建立索引库

//
索引目录位置

Directory directory = FSDirectory.open(new
File("index"));//
当前Index目录

//
分词器

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

//
写入索引

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

Version.LUCENE_36, analyzer);

IndexWriter indexWriter = new IndexWriter(directory,
indexWriterConfig);

//
将document数据写入索引库

indexWriter.addDocument(document);

//
关闭索引

indexWriter.close();

}

}

运行单元测试后的结果:

运行后index目录下的结果:

4
 可以通过luke工具查看索引库中内容(它是一个jar包)

下载网址:http://code.google.com/p/luke/

打开方式:

如果用这种方式打不可以,可以用命令的方式打开文件,进入这个目录,选中Shift+鼠标右键—>此处打开命令窗口—>输入命令:java
-jar lukeall-3.5.0.jar

工具的截图如下:

点击OK后的结果:

通过overview可以查看到索引信息,通过Document可以查看文档对象信息


查找

和上面的并集的query代码如下:

@Test

public
void searchIndex()
throws Exception

{

//建立Query对象--根据标题

String queryString = "Lucene";

//第一个参数,版本号

//第二个参数,字段

//第三个参数,分词器

Analyzer analyzer = new
StandardAnalyzer(Version.LUCENE_36);

QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title",analyzer);

Query query = queryParser.parse(queryString);

//根据Query查找

//
索引目录位置

Directory directory = FSDirectory.open(new
File("index"));

IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(directory));

//条数据

TopDocs topDocs = indexSearcher.search(query, 100);

System.out.println("满足结果记录条数:"
+ topDocs.totalHits);

//获取结果

ScoreDoc[] scoreDocs = topDocs.scoreDocs;

for (int
i = 0; i < scoreDocs.length; i++) {

//先获得Document下标

int docID = scoreDocs[i].doc;

Document document = indexSearcher.doc(docID);

System.out.println("id:"
+ document.get("id"));

System.out.println("title:"
+ document.get("title"));

System.out.println("content:"
+ document.get("content"));

}

indexSearcher.close();

}

运行结果:

  1. Luke查看的索引库内容:

索引库中信息,包括两大部分:

A
索引词条信息

B
文档对象信息

  1. 每个Field中都存在一个Store和一个Index

  2. 索引内容和Document内容有什么关系

查找时,通过索引内容 
查找 
文档对象信息

  1. 索引的查找过程

2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程的更多相关文章

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

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

  2. top命令查看线程信息和jstack使用介绍

    top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jstack 线程ID 可以查看某个线程的堆栈情况,特别对于hung挂死的线程,可以使用选项-F强制打印dump信 ...

  3. 一个简单好用的zabbix告警信息发送工具

    之前使用邮件和短信发送zabbix告警信息,但告警信息无法实时查看或者无法发送,故障无法及时通知运维人员. 后来使用第三方微信接口发送信息,愉快地用了一年多,突然收费了. zabbix告警一直是我的痛 ...

  4. [置顶] 一个简单好用的zabbix告警信息发送工具

    之前使用邮件和短信发送zabbix告警信息,但告警信息无法实时查看或者无法发送,故障无法及时通知运维人员. 后来使用第三方微信接口发送信息,愉快地用了一年多,突然收费了. zabbix告警一直是我的痛 ...

  5. Dubbo入门介绍---搭建一个最简单的Demo框架

    Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...

  6. Fiddler抓包工具详细介绍

    本文转自:http://www.cnblogs.com/Chilam007/p/6985379.html 一.Fiddler与其他抓包工具的区别 1.Firebug虽然可以抓包,但是对于分析http请 ...

  7. Java语言Lang包下常用的工具类介绍_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...

  8. EQueue - 一个C#写的开源分布式消息队列的总体介绍(转)

    源: EQueue - 一个C#写的开源分布式消息队列的总体介绍 EQueue - 一个纯C#写的分布式消息队列介绍2 EQueue - 详细谈一下消息持久化以及消息堆积的设计

  9. 在存放源程序的文件夹中建立一个子文件夹 myPackage。例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage)。在 myPackage 包中创建一个YMD类,该类具有计算今年的年份、可以输出一个带有年月日的字符串的功能。设计程序SY31.java,给定某人姓名和出生日期,计算该人年龄,并输出该人姓名、年龄、出生日期。程序使用YM

    题目补充: 在存放源程序的文件夹中建立一个子文件夹 myPackage.例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage).在 m ...

随机推荐

  1. [CQOI2010]内部白点

    Description 无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点).每秒钟,所有内部白点同时变黑,直到不存在内部白点为止.你的任务是统计最后网格 ...

  2. [SHOI2016]黑暗前的幻想乡

    Description 四年一度的幻想乡大选开始了,最近幻想乡最大的问题是很多来历不明的妖 怪涌入了幻想乡,扰乱了幻想乡昔日的秩序.但是幻想乡的建制派妖怪(人类) 博丽灵梦和八云紫等人整日高谈所有妖怪 ...

  3. 为了异常安全(swap,share_ptr)——Effecive C++

    互斥锁: 假设我们要在多线程中实现背景图片的控制: class PrettyMenu{ public: -- void changeBackground(std::istream& imgSr ...

  4. Codeforces Round #460 D. Karen and Cards

    Description Karen just got home from the supermarket, and is getting ready to go to sleep. After tak ...

  5. 【LSGDOJ 1408】邮局

    题目描述 一些村庄被建在一条笔直的高边公路边上.我们用一条坐标轴来描述这条高边公路,每一个村庄的坐标都是整数.没有两个村庄坐标相同.两个村庄问的距离,定义为它们坐标值差的绝对值. 我们需要在一些村庄建 ...

  6. Codeforces Round #438 C. Qualification Rounds

    Description Snark and Philip are preparing the problemset for the upcoming pre-qualification round f ...

  7. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. HDU2256(矩阵)

    求(sqrt(2) + sqrt(3)) ^ 2n MOD 1024 强行盗图- -,这公式推得 .. #include <iostream> #include <cstdio> ...

  9. gulp填坑记(一)

    gulp是基于Node.js的自动任务运行器.可以自动完成html.image.css和js等文件的检测.检查.合并.压缩.格式化等,并监听文件在改动后重复指定的这些步骤. 一.首先,我全局安装了gu ...

  10. SVN提交时显示:Path is not a working copy directory

    说明你地址没有checkout啊 先checkout,才能add和commi. 要是在一个已有的项目出现这个错误,就是包含这个地址的文件夹没添加进去,去上一层再试一次. 总之,养成在项目根目录提交的习 ...