今天刚到新公司入职,第一天没有什么特别具体的任务,于是就在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}/srcThis 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.SearchFilesYou'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