You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by jitender ahuja <aj...@aalayance.com> on 2004/04/14 07:15:33 UTC

customising reults.jsp to display results for a searcher calling IndexSearcher class??

Hi,
      In lucene 1.2 demo, the results.jsp file has to be put in a container to run on a web server. I am using Tomcat for the same, but the IndexSearcher has to be called by a seperate class, i.e. SearchDB, that searches for the appropriate data fields of an XML database stored in the Index directory. Now, this is causing problems as the index is not picked up by the results.jsp that directly calls the IndexSearcher class, at line nos. 35/44. Can anyone tell how to circumvent this issue by calling the SearchDB class instead, at line nos. 35/44 of results.jsp.

Also, the code picks up the desired term containing documents at the command line perfectly.

The code for the SearchDB file is also attached for understanding of the class functioning: 
import java.io.*;
import java.util.*;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.*;


public class SearchDB {

  public static void main(String[] args) {

    // search for a single term only
    String searchStr = args[0];
    searchDB(searchStr);
  }

  //search the database for a given search string and return the primary key(s)
  public static String[] searchDB(String searchStr)
  {
    ArrayList results = new ArrayList();
    String arrType[]= new String[0];
    try {
      //search on index directory
      Searcher indexSrch = new IndexSearcher("C:\\Temp\\DBDEX_GT1111");
      Analyzer analyzer = new StandardAnalyzer();

      //create a query to search for the target string in the description field
      Query query = QueryParser.parse(searchStr, "OBJECT_XML", analyzer);

      Hits hits = indexSrch.search(query);

      System.out.println(hits.length() + "total matching documents");

      for (int i = 0; i < hits.length(); i++) {

        // get the lucene document for each hit and then get field details
        Document doc = hits.doc(i);

        String id = doc.get("OBJECT_ID");

        results.add(id);

        String data = doc.get("OBJECT_XML");

        //we need only OBJECT_ID as it serves the purpose of id'ing the row to be returned
        System.out.println("\nResult "+i+"\nOBJECT_ID " + id);

        System.out.println("\nOBJECT_XML " + data);
        // no need to write sql code, to retrieve the contents of the text field, seperately
        // as the data is stored verbatim in the index
      }

    }
    catch (ParseException ex1) {
      ex1.printStackTrace();
      return (String[]) results.toArray(new String[0]);
    }
    catch (IOException ex) {
      ex.printStackTrace();
      return (String[]) results.toArray(new String[0]);
    }

    return (String[]) results.toArray(new String[0]);
  }

}

Thanks
Jitender