在了解了Lucene以后,我打算亲手来做一个Lucene的小例子,这个例子只是Lucene最简单的应用:使用Lucene实现标准的英文搜索;

1、下载Lucene

下载Lucene,到Lucene的官方下载http://lucene.apache.org/;

2、新建项目

新建一个Java Project 然后引入Lucene的jar 包:

因为要实现的功能非常简单,所以Jar包只引入了一部分,当然Lucene的jar包远远不止这些;

core包:Lucene的核心包

analyzers包:主要进行对采集的内容和用户输入的内容进行分词;

highlighter包:主要对搜索的结果进行高亮显示,就像百度搜索结果标红一样;

queries和queryparser包:搜索查询包,根据用户输入关键定去检索内容;

主要用到这三个包;

3、准备数据源文件

要让用户搜索结果,首先得有数据源, 我准备了几个文本文档,里面全是英文内容:

将这些文本文件放在一个全英文的目录里面,同时还要建一些纯英文的目录用来存放索引文件;

4、对数据源进行索引

在用户进行搜索前,系统得先对数据源进行分析,排序,分词,创建索引;这是一步很关键的工作:

新建一个CreateIndex类,代码如下:

package com.lucene;
import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
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.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test; public class CreateIndex {
/** 数据源目录 **/
public static final String DATA_DIR="E:/data/lucene/en/data";
/** 索引目录 **/
public static final String INDEX_DIR="E:/data/lucene/en/index";
@Test
public void create(){
try {
Directory dir = FSDirectory.open(new File(INDEX_DIR));
//4. 通过CharArraySet可以向分词中追加一些停止词(即排除检索的词)
CharArraySet arrSet = new CharArraySet(Version.LUCENE_4_9, 0, false);
//3. Analyzer 用于对数据源进行分词
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9, arrSet);
//2. IndexWriter的配置信息都存放在IndexWriterConfig中
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9,analyzer);
// OpenMode.CREATE_OR_APPEND 指定,该创建索引是可以在以后通过追加的方式向里面添加内容
config.setOpenMode(OpenMode.CREATE_OR_APPEND);
//1. 创建索引的入口,创建索引必须用IndexWriter进行创建或者追加
IndexWriter writer = new IndexWriter(dir,config);
File dataDir = new File(DATA_DIR);
//5.得到数据源中所有的文件
Collection<File> files = FileUtils.listFiles(dataDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for(File file : files){
//6. 通过向Writer追加Document的方式添加内容
Document doc = new Document();
doc.add(new StringField("filename",file.getName(), Store.YES));
String content = FileUtils.readFileToString(file);
doc.add(new TextField("content",content,Store.YES));
writer.addDocument(doc);
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

在新建完CreateIndex类以后,可以使用Test运行一下,然后在索引目录就会生成一些这样的文件:

这就是Lucene创建完索引的索引数据库了;

5、创建检索

创建一个SearchIndex类,主要作用是通过用户输入内容分词,然后检索出用户想要的结果:

import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test; public class SearchIndex {
@Test
public void search(){
try {
String keyword = "java";
// 在这里进行检索的时候,需要加载的目录就是创建索引的目录,创建索引以后,那些原数据源在Lucene上就暂时用不到了
Directory directory = FSDirectory.open(new File(CreateIndex.INDEX_DIR));
IndexReader reader = DirectoryReader.open(directory);
// IndexSearcher 是Lucene的检索的入口点,所有检索都从这里入口
IndexSearcher searcher = new IndexSearcher(reader);
// 通过analyzer对用户输入的词进行分词
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
// 构建检索条件
QueryParser parser = new QueryParser(Version.LUCENE_4_9, "content",analyzer);
Query query = parser.parse(keyword);
// 最后使用searcher.search检索,search方法的参数很多,还可以根据需求,取出相应的条数
TopDocs topDocs = searcher.search(query, 20);
// topDocs.totalHits 返回的是所有检索到记录的条数的总和
ScoreDoc[] docs = topDocs.scoreDocs;
System.out.println("关键词\" "+keyword+" \"共检索到 "+topDocs.totalHits+" 条相关的记录");
System.out.println("被检索到记录,他们分别放在以下的文件中:");
for(ScoreDoc doc : docs){
int docId = doc.doc;
Document document = reader.document(docId);
System.out.println(document.get("filename"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

为了方便,我摸拟了一个搜索词“java” 看能查询出多少条数据,运行单元测试:

小结:这只是Lucene的最简单的用法,还有很多高深的用法,可以查看Lucene的官方文档,Lucene用来检索中文同样很厉害,大家快去试试吧;下小节我会贴出我写的使用Lucene来做一个千度搜索;

2、Lucene 最简单的使用(小例子)的更多相关文章

  1. 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子

    原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...

  2. Android ExpandableListActivity的简单介绍及小例子

    Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表: 下面用一个例子来 ...

  3. C#网络编程简单实现通信小例子-1

    1.主界面 2.源程序 Send public partial class formUdpSend : Form { //声明一个UdpClient对象 UdpClient udpClient; pu ...

  4. C#网络编程简单实现通信小例子-2

    1.主界面  2.源代码                                                         Client public partial class For ...

  5. spring小例子-springMVC+mybits整合的小例子

    这段时间没更博,找房去了...   吐槽一下,自如太坑了...承诺的三年不涨房租,结果今年一看北京房租都在涨也跟着涨了... 而且自如太贵了,租不起了.. 突然有点理解女生找对象要房了..   搬家太 ...

  6. python2.7练习小例子(十)

        10):古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?     程序分析:兔子的规律为数列1,1 ...

  7. ASP.NET Cookie对象到底是毛啊?(简单小例子)

    记得刚接触asp.net的时候,就被几个概念搞的头痛不已,比如Request,Response,Session和Cookie.然后还各种在搜索引擎搜,各种问同事的,但是结果就是自己还是很懵的节奏. 那 ...

  8. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)

    lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能)   添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...

  9. php+jquery+ajax+json简单小例子

    直接贴代码: <html> <title>php+jquery+ajax+json简单小例子</title> <?php header("Conte ...

随机推荐

  1. Word Ladder 解答

    Question Given two words (beginWord and endWord), and a dictionary's word list, find the length of s ...

  2. Course Schedule II 解答

    Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...

  3. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  4. ACM学习-POJ-1143-Number Game

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1143-Number Game Number Game Time Limit: 1000MS   Memory ...

  5. 国际C语言混乱代码大赛代码赏析(一)【转】

    本文转载自:http://blog.csdn.net/ce123_zhouwei/article/details/9073869 国际C语言混乱代码大赛代码赏析(一) 近段时间在看<C专家编程& ...

  6. 遍历父视图上的button

    for (UIView * thebtn in [self.view subviews]) { if ([thebtn isKindOfClass:[UIButton class]]) { //*** ...

  7. 第一个Spring MVC程序

    最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...

  8. 目标管理剖析与实践– 献给追梦的人 (转)

      好久没写日志了. 最近总算在忙碌的日子中小小的松了一口气, 过来补起这几个月的空缺. 上次写的Cover Letter & Resume 重点诠释 - 深度剖析没想到居然超过了一万的阅读量 ...

  9. django学习笔记一

    django作为一个python的开源项目发布,其web框架采用了mtv设计模式 在目前一些较为成熟的大型网站中有不少网站的应用基于django开发,django作为一个重量型的web框架提供了以下的 ...

  10. 导出Eclipse环境配置

    第一种方法: Eclipse的 File -> Export(导出), 在窗口中展开 General(常规) -> Perferences(首选项)-->Export all(全部导 ...