You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by ashwin kumar <gv...@gmail.com> on 2007/03/11 06:08:40 UTC
help!!!!
hi all my name is ashwin i am trying to connect my servlet front end to my
backend lucene search program
these r the two programs
<<frontend servlet program>>
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.lang.*;
//import java.io.*;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;
//import frontend.mainpage;
import java.lang.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searchable;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
public class mainpage extends HttpServlet
{
static int numRequest=0;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
try
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><head><title>testing</title></head>");
out.println("<body>");
out.println("<h1>Distrubuted Desktop Search using
lucene</h1>");
TextFileIndexer index=new TextFileIndexer(out);
out.println("<br><form method=\"post\">");
out.println("<input type=\"text\" name =\"toSearch\" size=100
maxlength=100>");
out.println(" <input type=\"submit\" name=\"searchbutton\"
value=\"search\"><br>");
out.println("</form>");
out.println("</body></html>");
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String getServletInfo()
{
return "good";
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws
ServletException, IOException
{
try
{
res.setContentType("text/html");
String searchString = req.getParameter("toSearch");
PrintWriter out=res.getWriter();
out.println("<head><title>testing</title></head>");
out.println("<body>");
out.println("<h1>"+searchString+"</h1>");
TextFileMatch match=new TextFileMatch(searchString,out);
out.println("</body></html>");
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void destroy()
{
}
}
<<backend lucene search program>>
import java.io.*;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;
//import frontend.mainpage;
import java.lang.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searchable;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
class TextFileMatch
{
public TextFileMatch(String searchString,PrintWriter out)throws
Exception
{
Searcher searcher = new IndexSearcher("C:\\luceneIndex");
Analyzer analyzer = new StandardAnalyzer();
String toSearch=searchString;
String buffer=new String("");
System.out.println("\n"+toSearch+"\n");
Query query = QueryParser.parse(toSearch,"content",analyzer);
out.println("Searching for: " + query.toString("text"));
Hits hits = searcher.search(query);
System.out.println("Number of matching documents = " + hits.length
());
for (int i = 0; i < hits.length(); i++)
{
Document doc = hits.doc(i);
//buffer=buffer+"\n"+"file :"+doc.get("path")+"\n";
out.println("File: " + doc.get("path") + " , score: " +
hits.score(i));
}
}
}
class TextFileIndexer
{
public TextFileIndexer(PrintWriter out) throws Exception
{
out.println("*************");
File fileDir= new File("C:\\files");
File indexDir= new File("C:\\luceneIndex");
Boolean flag=false;
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
File[] textFiles= fileDir.listFiles();
long startTime = new Date().getTime();
System.out.println("\n the total number of files in the directory is
"+textFiles.length+"\n");
for(int i = 0; i < textFiles.length; i++)
{
if(textFiles[i].getName().endsWith(".txt"))
{
System.out.println("\n File " +
textFiles[i].getCanonicalPath()+" is being indexed");
Reader textReader = new FileReader(textFiles[i]);
Document document = new Document();
document.add(Field.Text("content",textReader));
document.add(Field.Text("path",textFiles[i].getPath()));
indexWriter.addDocument(document);
}
else if(textFiles[i].getName().endsWith(".pdf"))
{
}
}
indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("It took " + (endTime - startTime) + " milliseconds to
create an index for the files in the directory "+ fileDir.getPath());
}
}
<<note>>
all programs and class files are inside a same folder
the error msg i am getting is
HTTP Status 500 -
------------------------------
*type* Exception report
*message*
*description* *The server encountered an internal error () that prevented it
from fulfilling this request.*
*exception*
javax.servlet.ServletException: Servlet execution threw an exception
filters.ExampleFilter.doFilter(ExampleFilter.java:101)
*root cause*
java.lang.NoClassDefFoundError: org/apache/lucene/analysis/Analyzer
mainpage.doGet(mainpage.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
filters.ExampleFilter.doFilter(ExampleFilter.java:101)
*note* *The full stack trace of the root cause is available in the Apache
Tomcat/5.5.8 logs.*
------------------------------
Apache Tomcat/5.5.8
pls help me debug this error
thanks
regards
ashwin
Re: help!!!!
Posted by Grant Ingersoll <gr...@gmail.com>.
Looks like the Lucene libraries are not in your classpath.
On Mar 11, 2007, at 12:08 AM, ashwin kumar wrote:
> hi all my name is ashwin i am trying to connect my servlet front
> end to my
> backend lucene search program
>
> these r the two programs
>
>
> <<frontend servlet program>>
>
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.lang.*;
>
> //import java.io.*;
> import java.io.FileReader;
> import java.io.Reader;
> import java.util.Date;
> //import frontend.mainpage;
> import java.lang.*;
>
> import org.apache.lucene.analysis.Analyzer;
> import org.apache.lucene.analysis.standard.StandardAnalyzer;
> import org.apache.lucene.document.Document;
> import org.apache.lucene.document.Field;
> import org.apache.lucene.index.IndexWriter;
> import org.apache.lucene.search.IndexSearcher;
> import org.apache.lucene.search.Searchable;
> import org.apache.lucene.search.Searcher;
> import org.apache.lucene.queryParser.QueryParser;
> import org.apache.lucene.search.Hits;
> import org.apache.lucene.search.Query;
>
> public class mainpage extends HttpServlet
> {
> static int numRequest=0;
>
> public void init(ServletConfig config)throws ServletException
> {
> super.init(config);
> }
>
>
>
> public void doGet(HttpServletRequest req,HttpServletResponse res)
> throws
> ServletException,IOException
> {
> try
> {
> res.setContentType("text/html");
>
>
> PrintWriter out=res.getWriter();
>
>
>
>
> out.println("<html><head><title>testing</title></head>");
>
> out.println("<body>");
>
> out.println("<h1>Distrubuted Desktop Search using
> lucene</h1>");
>
> TextFileIndexer index=new TextFileIndexer(out);
>
> out.println("<br><form method=\"post\">");
>
> out.println("<input type=\"text\" name =\"toSearch\"
> size=100
> maxlength=100>");
>
> out.println(" <input type=\"submit\" name=\"searchbutton\"
> value=\"search\"><br>");
>
> out.println("</form>");
>
> out.println("</body></html>");
> out.close();
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
>
>
> }
>
> public String getServletInfo()
> {
> return "good";
> }
>
> public void doPost(HttpServletRequest req,HttpServletResponse
> res)throws
> ServletException, IOException
> {
> try
> {
> res.setContentType("text/html");
>
> String searchString = req.getParameter("toSearch");
>
> PrintWriter out=res.getWriter();
>
> out.println("<head><title>testing</title></head>");
>
> out.println("<body>");
>
> out.println("<h1>"+searchString+"</h1>");
>
> TextFileMatch match=new TextFileMatch(searchString,out);
>
> out.println("</body></html>");
>
> out.close();
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
>
> }
> public void destroy()
> {
>
> }
> }
>
>
> <<backend lucene search program>>
>
> import java.io.*;
> import java.io.FileReader;
> import java.io.Reader;
> import java.util.Date;
> //import frontend.mainpage;
> import java.lang.*;
>
> import org.apache.lucene.analysis.Analyzer;
> import org.apache.lucene.analysis.standard.StandardAnalyzer;
> import org.apache.lucene.document.Document;
> import org.apache.lucene.document.Field;
> import org.apache.lucene.index.IndexWriter;
> import org.apache.lucene.search.IndexSearcher;
> import org.apache.lucene.search.Searchable;
> import org.apache.lucene.search.Searcher;
> import org.apache.lucene.queryParser.QueryParser;
> import org.apache.lucene.search.Hits;
> import org.apache.lucene.search.Query;
>
> class TextFileMatch
> {
> public TextFileMatch(String searchString,PrintWriter out)throws
> Exception
> {
> Searcher searcher = new IndexSearcher("C:\\luceneIndex");
> Analyzer analyzer = new StandardAnalyzer();
>
> String toSearch=searchString;
> String buffer=new String("");
>
> System.out.println("\n"+toSearch+"\n");
>
> Query query = QueryParser.parse
> (toSearch,"content",analyzer);
> out.println("Searching for: " + query.toString("text"));
> Hits hits = searcher.search(query);
> System.out.println("Number of matching documents = " +
> hits.length
> ());
> for (int i = 0; i < hits.length(); i++)
> {
> Document doc = hits.doc(i);
> //buffer=buffer+"\n"+"file :"+doc.get("path")+"\n";
> out.println("File: " + doc.get("path") + " ,
> score: " +
> hits.score(i));
> }
> }
> }
>
> class TextFileIndexer
> {
> public TextFileIndexer(PrintWriter out) throws Exception
> {
>
> out.println("*************");
> File fileDir= new File("C:\\files");
>
> File indexDir= new File("C:\\luceneIndex");
>
> Boolean flag=false;
>
> Analyzer luceneAnalyzer = new StandardAnalyzer();
>
>
> IndexWriter indexWriter = new IndexWriter
> (indexDir,luceneAnalyzer,true);
>
> File[] textFiles= fileDir.listFiles();
>
> long startTime = new Date().getTime();
>
> System.out.println("\n the total number of files in the directory is
> "+textFiles.length+"\n");
>
> for(int i = 0; i < textFiles.length; i++)
> {
>
> if(textFiles[i].getName().endsWith(".txt"))
> {
> System.out.println("\n File " +
> textFiles[i].getCanonicalPath()+" is being indexed");
> Reader textReader = new FileReader(textFiles[i]);
> Document document = new Document();
> document.add(Field.Text("content",textReader));
> document.add(Field.Text("path",textFiles[i].getPath()));
> indexWriter.addDocument(document);
>
> }
> else if(textFiles[i].getName().endsWith(".pdf"))
> {
>
>
> }
>
> }
>
> indexWriter.optimize();
> indexWriter.close();
> long endTime = new Date().getTime();
>
> System.out.println("It took " + (endTime - startTime) + "
> milliseconds to
> create an index for the files in the directory "+ fileDir.getPath());
> }
> }
>
>
>
> <<note>>
> all programs and class files are inside a same folder
>
> the error msg i am getting is
>
> HTTP Status 500 -
> ------------------------------
>
> *type* Exception report
>
> *message*
>
> *description* *The server encountered an internal error () that
> prevented it
> from fulfilling this request.*
>
> *exception*
>
> javax.servlet.ServletException: Servlet execution threw an exception
> filters.ExampleFilter.doFilter(ExampleFilter.java:101)
>
> *root cause*
>
> java.lang.NoClassDefFoundError: org/apache/lucene/analysis/Analyzer
> mainpage.doGet(mainpage.java:54)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> filters.ExampleFilter.doFilter(ExampleFilter.java:101)
>
> *note* *The full stack trace of the root cause is available in the
> Apache
> Tomcat/5.5.8 logs.*
> ------------------------------
> Apache Tomcat/5.5.8
> pls help me debug this error
>
> thanks
> regards
> ashwin
------------------------------------------------------
Grant Ingersoll
http://www.grantingersoll.com/
http://lucene.grantingersoll.com
http://www.paperoftheweek.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: help!!!!
Posted by Michael Wechner <mi...@wyona.com>.
ashwin kumar wrote:
>
>
> java.lang.NoClassDefFoundError: org/apache/lucene/analysis/Analyzer
> mainpage.doGet(mainpage.java:54)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> filters.ExampleFilter.doFilter(ExampleFilter.java:101)
>
> *note* *The full stack trace of the root cause is available in the Apache
> Tomcat/5.5.8 logs.*
> ------------------------------
> Apache Tomcat/5.5.8
> pls help me debug this error
have you got the library containing the class above within your lib dir
(WEB-INF/lib)?
HTH
Michael
>
> thanks
> regards
> ashwin
>
--
Michael Wechner
Wyona - Open Source Content Management - Apache Lenya
http://www.wyona.com http://lenya.apache.org
michael.wechner@wyona.com michi@apache.org
+41 44 272 91 61
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org