Web Service for Lucene Search on Tomcat
October 1, 2010 Leave a comment
The SearchUtil class is created to search on Lucene. This also uses a stop words file to eliminate those words and/or numbers. The class returns ids matched to a table for related search results.
public class SearchUtil {
private String stopWordsFile = "/mnt/lucene/bmindex/stopwords.txt";
public int[] search(int clientId, int keywordId, String searchStr, int offset, int hitPerPage) throws IOException, ParseException{
IndexSearcher searcher = getSearcher(clientId);
File stopWords = new File(stopWordsFile);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT, stopWords);
QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "contents", analyzer);
Query query = qp.parse(searchStr);
int limit = offset + hitPerPage;
TopDocs topDocs = null;
if(keywordId > 0){
Filter filter = NumericRangeFilter.newIntRange("keyword_id", keywordId, keywordId, true, true);
topDocs = searcher.search(query, filter, limit);
}else{
topDocs = searcher.search(query, limit);
}
int lastIndex = limit > topDocs.totalHits ? topDocs.totalHits : limit;
int startIndex = offset;
int[] searchedIds = new int[hitPerPage+1];
searchedIds[0] = topDocs.totalHits;
for (int i = startIndex; i < lastIndex; i++) {
Document doc = searcher.doc(topDocs.scoreDocs[i].doc);
searchedIds[i] = Integer.parseInt(doc.get("id"));
}
return searchedIds;
}
private IndexSearcher getSearcher(int clientId) throws IOException {
Directory dir = FSDirectory.open(new File(indexDir(clientId)));
IndexReader reader = IndexReader.open(dir, true);
return new IndexSearcher(reader);
}
private String indexDir(int clientId){
return "/mnt/lucene/bmindex/" + "client_index_" + new Integer(clientId).toString();
}
}
The next class is the web service which takes various search criterion.
@WebService()
public class SearchIndex {
@WebMethod(operationName = "searchLucene")
public int[] searchLucene(
@WebParam(name="clientId")int clientId,
@WebParam(name="keywordId")int keywordId,
@WebParam(name="searchStr")String searchStr,
@WebParam(name="startLimit")int startLimit,
@WebParam(name="hitPerPage")int hitPerPage) throws IOException, ParseException{
SearchUtil su = new SearchUtil();
int[] searchIds = su.search(clientId, keywordId, searchStr, startLimit, hitPerPage);
return searchIds;
}
}
To enable the web service on Tomcat we have to map it as a Servlet in web.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>jax-ws</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jax-ws</servlet-name>
<url-pattern>/SearchIndex</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
There is one more configuration in Tomcat we have to do to make the web service working. Details of that can be found at Running JAX-WS with Tomcat