I am very new to apache Lucene.here I am trying to develop sample full text search application which will search in html files with given input query,if given string found in any file then index's are created
My results jsp page look like this :
if I click on any hyper link that html file need to be open in new tab but I am getting blank page.
This is My Application folder structure
This is my code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String query=request.getParameter("squery");
String path1= "C:/POC/indexs/";
System.out.println(path1);
String path2 = request.getContextPath()+"/WEB-INF/Html-Files";
File indexDir = new File(path1);
int hits = 100;
FilesTextFinder createIndex=new FilesTextFinder();
File dataDir = new File(path2);
String suffix = "html";
try
{
boolean iscreated=isIndexCreated(indexDir, query, hits);
if(!iscreated)
{
System.out.println("no indexs found...");
int numIndex = createIndex.index(indexDir, dataDir, suffix);
System.out.println("Total Indexs: "+numIndex);
}
searchIndex(indexDir, query, hits);
RequestDispatcher rd=request.getRequestDispatcher("/Results.jsp");
request.setAttribute("results", values);
request.setAttribute("query", query);
rd.forward(request, response);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void searchIndex(File indexDir, String queryStr, int maxHits) throws Exception
{
Directory directory = FSDirectory.open(indexDir);
DirectoryReader dreader=DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(dreader);
QueryParser parser = new QueryParser("contents",new SimpleAnalyzer());
Query query = parser.parse(queryStr);
TopDocs topDocs = searcher.search(query, maxHits);
ScoreDoc[] hits = topDocs.scoreDocs;
int count=hits.length;
values=new HashMap<String, String>();
for (int i = 0; i<count; i++)
{
int docId = hits[i].doc;
Document d = searcher.doc(docId);
String cmpt_path=d.get("filename");
int indx=cmpt_path.lastIndexOf("\\");
String name=cmpt_path.substring(indx+1,cmpt_path.length());
if(name.length()>40)
{
name=name.substring(0, 40);
}
System.out.println("name: "+name);
values.put(name, cmpt_path);
}
System.out.println("Found " + hits.length);
}
private boolean isIndexCreated(File indexDir, String queryStr, int maxHits) throws Exception
{
Directory directory = FSDirectory.open(indexDir);
System.out.println("---------"+indexDir);
DirectoryReader dreader=DirectoryReader.open(directory);//here i am getting error
IndexSearcher searcher = new IndexSearcher(dreader);
QueryParser parser = new QueryParser("contents",new SimpleAnalyzer());
Query query = parser.parse(queryStr);
TopDocs topDocs = searcher.search(query, maxHits);
ScoreDoc[] hits = topDocs.scoreDocs;
int count=hits.length;
System.out.println("called..."+count);
directory.close();
if(count>0)
return true;
else
return false;
}
}
how can I pass my html files contained folder path i.e WEB-INF/Html-files to FSDirectory class and when i cick on any hyperlink in my results page corresponding html file need be open in new tab of browser.
How can I achieve this.
Thanks in advance...