Lucene4.0自帶的Demo存在bug,本文描述如何部署Demo。
前提條件:安裝了JDK、Tomcat、Eclipse (EE),下載了Lucene 4.0壓縮文件和源碼文件。
1. 在Eclipse忠創建一個Dynamic Web Project
2. 將lucene-4.0.0-src\lucene-4.0.0\demo\src\java文件夾中的內容複製到Eclipse工程的src目錄下
3. 將lucene-4.0.0-src\lucene-4.0.0\demo\src\resources\org\apache\lucene\demo\xmlparser中的內容覆蓋Eclipse工程的WebContent文件夾中的內容
4. 將lucene-analyzers-common-4.0.0.jar、lucene-core-4.0.0.jar、lucene-queries-4.0.0.jar、lucene-queryparser-4.0.0.jar、lucene-sandbox-4.0.0.jar複製到Eclipse工程的WebContent\lib文件夾中
5. 修改Eclipse工程的WebContent\WEB-INF\web.xml文件,修改內容爲紅色部分:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
LuceneXmlQueryWebDemo</display-name>
<servlet>
<description>
Servlet demonstrating XMLQueryParser</description>
<display-name>
FormBasedXmlQueryDemo</display-name>
<servlet-name>FormBasedXmlQueryDemo</servlet-name>
<servlet-class>org.apache.lucene.demo.xmlparser.FormBasedXmlQueryDemo</servlet-class>
<init-param>
<description>
Name of query file held in /WEB-INF</description>
<param-name>xslFile</param-name>
<param-value>query.xsl</param-value>
</init-param>
<init-param>
<description>
Default field used in standard Lucene QueryParser used in UserQuery tag</description>
<param-name>defaultStandardQueryParserField</param-name>
<param-value>jobDescription</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FormBasedXmlQueryDemo</servlet-name>
<url-pattern>/FormBasedXmlQueryDemo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
整個工程結構如下圖:
7. 在瀏覽器中輸入http://localhost:8080/lucene/,然後搜索lucene,頁面如下: