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

lucene-3.6.1简单安装与demo运行 for mac

2013年10月22日 ⁄ 综合 ⁄ 共 1817字 ⁄ 字号 评论关闭

今天刚到新公司入职,第一天没有什么特别具体的任务,于是就在mac上研究了一下lucene的安装和运行。

安装:

去官网下载lucene的最新发行版lucene-3.6.1,放到usr/local目录下,解压。这时,看docs文件夹下的index.html,里面有个getting start,内容如下:

Setting your CLASSPATH

First, you should download the latest Lucene distribution and then extract it to a working directory.

You need two JARs: the Lucene JAR, and the Lucene demo JAR. You should see the Lucene JAR file in the directory you created when you extracted the archive -- it should be named something like lucene-core-{version}.jar.
You should also see a file called contrib/demo/lucene-demo-{version}.jar.

Put both of these files in your Java CLASSPATH.

Indexing Files

Once you've gotten this far you're probably itching to go. Let's build an index! Assuming you've set your CLASSPATH correctly, just type:

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

This will produce a subdirectory called index which will contain an index of all of the Lucene source code.

To search the index type:

    java org.apache.lucene.demo.SearchFiles

You'll be prompted for a query. Type in a swear word and press the enter key. You'll see that the Lucene developers are very well mannered and get no results. Now try entering the word "string". That should return a whole bunch of documents. The results will
page at every tenth result and ask you whether you want more results.

demo运行:

首先要设置classpath

mac下设命令如下:

cd ~;

vi .bash_profile;

进行编辑,添加内容如下:

export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.1;/usr/local/lucene-3.6.1/contrib/demo;

编辑完成后,保存并重新启动terminal;

查看CLASSPATH是否设置echo $CLASSPATH,正确后,运行以下命令建议索引:

java org.apache.lucene.demo.IndexFiles -docs /usr/local/lucene-3.6.1

这时,lucene会为/usr/local/lucene-3.6.1下的文件建立索引。会看到一系列的adding xxx

再运行:

    java org.apache.lucene.demo.SearchFiles
要求输入查询关键字,按回车进行查询。
注:如果上面一切正常,则可运行。如果出现ClassNotFoundError的问题,则将lucene-core-{version}.jar和contrib/demo/lucene-demo-{version}.jar两个jar包以压缩包解压方式进行解压。
PS:直观查看lucene的全文索引功能方法如下。打开changes.txt,随意输入字符串,最好保证唯一性,如你的改名,保存,重新建立索引,再进行查询时,输入你的名字,你就可以看到结果把changes.txt这个文档查出来了!这个方法是不是很直观?嘿嘿

抱歉!评论已关闭.