2、Lucene 最简单的使用(小例子)
在了解了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 最简单的使用(小例子)的更多相关文章
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...
- Android ExpandableListActivity的简单介绍及小例子
Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表: 下面用一个例子来 ...
- C#网络编程简单实现通信小例子-1
1.主界面 2.源程序 Send public partial class formUdpSend : Form { //声明一个UdpClient对象 UdpClient udpClient; pu ...
- C#网络编程简单实现通信小例子-2
1.主界面 2.源代码 Client public partial class For ...
- spring小例子-springMVC+mybits整合的小例子
这段时间没更博,找房去了... 吐槽一下,自如太坑了...承诺的三年不涨房租,结果今年一看北京房租都在涨也跟着涨了... 而且自如太贵了,租不起了.. 突然有点理解女生找对象要房了.. 搬家太 ...
- python2.7练习小例子(十)
10):古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 程序分析:兔子的规律为数列1,1 ...
- ASP.NET Cookie对象到底是毛啊?(简单小例子)
记得刚接触asp.net的时候,就被几个概念搞的头痛不已,比如Request,Response,Session和Cookie.然后还各种在搜索引擎搜,各种问同事的,但是结果就是自己还是很懵的节奏. 那 ...
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)
lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能) 添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...
- php+jquery+ajax+json简单小例子
直接贴代码: <html> <title>php+jquery+ajax+json简单小例子</title> <?php header("Conte ...
随机推荐
- Linux下查看Web服务器当前的并发连接数和TCP连接状态
对于web服务器(Nginx.Apache等)来说,并发连接数是一个比较重要的参数,下面就通过netstat命令和awk来查看web服务器的并发连接数以及TCP连接状态. $ netstat -n | ...
- 【剑指offer】面试题22:栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列 ...
- 网站SEO优化中内部链接的优化
重要性:内链有效的优化能够间接的提高某页面的权重达到搜索排名靠前的效果.同时有效的带领搜索引擎蜘蛛对整站进行抓取. 网站头部导航: 这个导航称为'网站主导航',当用户来到网站需要给他们看到的内容.也就 ...
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg
1 游戏逻辑架构 具体介绍 A 一个导演同一时间仅仅能执行一个场景,场景其中,能够同一时候载入多个层,一个层能够可载多个精灵.层中亦能够加层. B 场景切换 sceneàaddChild(la ...
- poj1664 放苹果(递归)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1664 ------ ...
- mysql sql limit where having order
SQL语句执行顺序及MySQL中limit的用法 . 分类: MySql2013-09-02 09:1315人阅读评论(0)收藏举报 写的顺序:select ... from... where.... ...
- linux下挂载iso镜像文件(转)
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o optio ...
- 第二章实例:ArrayAdapter结合ListView列表视图
package mydefault.packge; import com.example.codeview.R; import android.app.Activity; import android ...
- UIImageView~动画播放的内存优化
我目前学到的知识,播放动画的步骤就是下面的几个步骤,把照片资源放到数组里面,通过动画animationImage加载数组,设置动画播放的 时间和次数完成播放. 后来通过看一些视频了解到:当需要播放多个 ...