Web Service for Lucene Search on Tomcat

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);
            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.

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"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"



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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: