我对 apache Lucene 很陌生。这里我正在尝试开发示例全文搜索应用程序,它将使用给定的输入查询在 html 文件中搜索,如果在任何文件中找到给定的字符串,则创建索引
我的结果 jsp 页面如下所示:
如果我单击需要在新选项卡中打开 html 文件的任何超链接,但我得到的是空白页。
这是我的应用程序文件夹结构
这是我的代码:
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;
}
}
如何将包含文件夹路径(即 WEB-INF/Html-files)的 html 文件传递给 FSDirectory 类,当我点击结果页面中的任何超链接时,需要在浏览器的新选项卡中打开相应的 html 文件。
我怎样才能做到这一点。
提前致谢...