2014-12-07 23:39
2623人阅读
评论(0)
收藏
举报

.embody{
padding:10px 10px 10px;
margin:0 -20px;
border-bottom:solid 1px #ededed;
}
.embody_b{
margin:0 ;
padding:10px 0;
}
.embody .embody_t,.embody .embody_c{
display: inline-block;
margin-right:10px;
}
.embody_t{
font-size: 12px;
color:#999;
}
.embody_c{
font-size: 12px;
}
.embody_c img,.embody_c em{
display: inline-block;
vertical-align: middle;
}
.embody_c img{
width:30px;
height:30px;
}
.embody_c em{
margin: 0 20px 0 10px;
color:#333;
font-style: normal;
}


分类:

爬虫(8)

版权声明:本文为博主原创文章,未经博主允许不得转载。



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. 索引的查找过程

0
0

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

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

     1  Lucen目录介绍 2  lucene-core-3.6.2.jar是lucene开发核心jar包 contrib  目录存放,包含一些扩展jar包 3  案例 建立第一个Lucene项目 ...

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

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

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

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

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

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

  5. lucene 全文检索工具的介绍

    Lucene:全文检索工具:这是一种思想,使用的是C语言写出来的 1.Lucene就是apache下的一个全文检索工具,一堆的jar包,我们可以使用lucene做一个谷歌和百度一样的搜索引擎系统 2. ...

  6. Lucene介绍及简单入门案例(集成ik分词器)

    介绍 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和 ...

  7. 第一个lucene程序,把一个信息写入到索引库中、根据关键词把对象从索引库中提取出来、lucene读写过程分析

    新建一个Java Project :LuceneTest 准备lucene的jar包,要加入的jar包至少有: 1)lucene-core-3.1.0.jar     (核心包) 2) lucene- ...

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

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

  9. Fiddler抓包工具详细介绍

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

随机推荐

  1. 页面下载文件方法,post与get

    一般下载文件,常见使用的是window.open('url'):方法进行下载.若需要带参数,直接在url后面拼接参数,进行传递.window.open方法仅可以进行get方法进行参数提交. 若需要进行 ...

  2. 如何让myeclipse左边选中文件后自动关联右边树

    在左侧项目树的右上角下拉菜单里有link with editor 点击即可

  3. linux部分常用命令

    linux的命令挺多的,下面是我常用的,其实也不可能在敲代码的时候把这个博客拿出来对着写,就是想记录一下,刚开始都觉得不好记,多敲几遍就记住了!!! 创建文件夹:mkdir filename 删除当前 ...

  4. WinServer-IIS-woff字体不显示问题

    ASP.NET mvc发布到IIS之后,访问网站的时候,发现woff字体没有加载 百度发现很多博客上的教程是这样的,在IIS管理器中的MIME选项中添加类型 但是重新使用IIS发布后,新添加的字体就会 ...

  5. jquery简直是太酷炫强大了

    链接地址:http://www.yyyweb.com/350.html Web 开发中很实用的10个效果[源码下载] 小鱼 发布于 3年前 (2014-07-15) 分类:前端开发 阅读(303741 ...

  6. System.IO.FileLoadException异常

    未能加载文件或程序集“NHibernate, Version=4.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖 ...

  7. (转载)Android滑动冲突的完美解决

    Android滑动冲突的完美解决 作者:softwindy_brother 字体:[增加 减小] 类型:转载 时间:2017-01-24我要评论 这篇文章主要为大家详细介绍了Android滑动冲突的完 ...

  8. stm8s103 EEPROM烧程序时能否保留

    EEPROM的参数需要再烧录程序时保留,做试验测试是否能够保留 1.在ST Visual Develop中硬件仿真手动修改EEPROM的值. 2.在ST Visual Programmer中读取EEP ...

  9. 优动漫PAINT-草地教程

    非常实用的草地教程,是场景控们绝对要学会的绘画技巧~更有配套草地笔刷~让场景绘画更简易~ 教程是简单,呃.... 没有优动漫PAINT软件肿么办? 别着急,╭(╯^╰)╮ 小编给你送来了 齐全的哟? ...

  10. GCD - Extreme (II) UVA - 11426 欧拉函数_数学推导

    Code: #include<cstdio> using namespace std; const int maxn=4000005; const int R=4000002; const ...