现在的位置: 首页 > 综合 > 正文

lucene-4.5.0版本在window7上的环境搭建

2017年12月07日 ⁄ 综合 ⁄ 共 2225字 ⁄ 字号 评论关闭

因为这个版本比较新,在网上也没找到合适的文章,全靠试一试的态度和网上参考文章终于把环境搭建起来了。

一:环境准备

到官网下载lucene包http://lucene.apache.org/,虽然4.6.0已经出来了,但是我下的是4.5.0版本lucene-4.5.0.zip。解压缩出来,随便放在哪个目录下就可以,和以往老版本的不同,这个文件中包含了很多文件,jar包也分布在这些文件中,如:

其实你已经完成了一半了。

二:demo程序演示

此时分成两种情况:1:在控制台中运行lucene提供的demo程序。2:在eclipse/myeclipse中运行demo程序。

先来看看第一种情况:在控制台中运行lucene提供的demo程序

首先需要四个jar文件,以下是官网的一段话:

You need four JARs: the Lucene JAR, the queryparser JAR, the common analysis JAR, and the Lucene demo JAR. You should see the Lucene JAR file in the core/ directory you created when you extracted the archive -- it should be named
something like lucene-core-{version}.jar. You should also see files called lucene-queryparser-{version}.jar, lucene-analyzers-common-{version}.jar and lucene-demo-{version}.jar under
queryparser, analysis/common/ and demo/, respectively.

Put all four of these files in your Java CLASSPATH.

只需要好好看看那四个jar文件的名字就很容易的猜出这个四个jar文件在哪里:在你下载的lucene包中的analysis/common,core,demo,queryparser文件夹中

然后将这四个jar文件设置到classpath中。

然后在控制台中输入:

java org.apache.lucene.demo.IndexFiles -docs {path-to-lucene}

这是在创建索引,-index指定索引文件放在哪里,-docs指定文件来源在哪里(就是你要搜索的文件位置),还有一个-update就是指定已经存在的哪个索引不需要删除。

-index如果不写,那么默认索引文件在你控制台当前位置的index文件夹下。你可以这样写:-docs  c:/temp,那么你的文件就来源于c:/temp下的文本文件。

现在你的索引已经创建好了,接下来就是搜索了。

继续在控制台中输入:

java org.apache.lucene.demo.SearchFiles

控制台会提示你输入你要搜索的数据。这样就完成了demo程序。

第二种情况:在eclipse/myeclipse中运行demo程序

你在IDE中新建一个java project,将上面的那四个jar文件加入到工程中。完成了!!!!你现在可以自己写程序了。什么indexwriter都可以找到了。但是源码还是找不到,你必须把源码也加入到工程中。去官网下载一个lucene-4.5.0-src包,这就是源码包,解压之后,分别为每个jar文件导入源码,以core为例:lucene-4.5.0-src\lucene-4.5.0\core\src\java。

你运行demo中的IndexFiles.class。设置arguments:-index d:/index/test -docs G:/lucene/lucene-4.5.0/src。这里我指定了索引文件在d:/index/test,搜索文件在G:/lucene/lucene-4.5.0/src(这个文件必须存在)。运行之后,控制台会输出:

Indexing to directory 'd:/index/test'...
adding G:\lucene\lucene-4.5.0\src\1.txt
adding G:\lucene\lucene-4.5.0\src\2.txt
adding G:\lucene\lucene-4.5.0\src\3.txt
1634 total milliseconds
我这里是在G:/lucene/lucene-4.5.0/src文件夹中放了三个文本文件。

目前索引创建完成。接下来是搜索。

运行SearchFiles.class。运行结果是:

Enter query:
a
Searching for:
0 total matching documents
Enter query:
string
Searching for: string
1 total matching documents
1. G:\lucene\lucene-4.5.0\src\3.txt
Press (q)uit or enter number to jump to a page.

随便提一下:输入a时说没有找到文档,但事实上我文本文件中有这个词,只是在lucene中有一种stop word,这类词没什么含义,不能作为关键词,像这样的词还有this,the等。

这样就完成了demo程序的演示。lucene版本的那个伟大的程序HelloWorld也就完成啦。

抱歉!评论已关闭.