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. POJ1830开关问题

    这题答案就是2^自由元的数目,原因是自由元可以取1或者0,所以就是ans<<1 由于只要求自由元的数目,所以高斯消元可以直接消后面的,不做前面的了,对答案没有影响 #include< ...

  2. POJ 2289(多重匹配+二分)

    POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...

  3. 2015 多校联赛 ——HDU5402(模拟)

    For each test case, in the first line, you should print the maximum sum. In the next line you should ...

  4. 【无语凝噎(wordless)】

    ·题目:         西施与范蠡泛舟而去……不对,场景不对,咳咳.在甄嬛前往蓬莱洲之前,她与皇上在碧桐书院告别.为了这可能会长达数月的离别,两个人都似乎有很多话要对对方说,却都无语凝噎.这时,皇上 ...

  5. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. hdu 5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  7. Python3 运算符

    装载自:https://www.cnblogs.com/cisum/p/8064222.html Python3 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 ...

  8. Linked List Cycle II--寻找单链表中环的起始点

    题目要求 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. ...

  9. 解决警告: Setting property 'source' to 'org.eclipse.jst.jee.server_:' did not find a matching property.的方法

    今天第一次搭建struts2框架,跟着网上的教程导入对应的jar包之后就开始写登录的jsp页面,但是运行时出现了问题, 浏览器显示"The requested resource is not ...

  10. [HCNA]VLAN配置Access接口

    实验目的 1.理解VLAN的应用场景 2.掌握VLAN的基本配置 3.掌握Access接口的配置方法 4.掌握Access接口加入相应VLAN的方法 实验仪器 eNSP 实验原理 如网络拓扑图所示 各 ...