如何使用apache lucene为fsdirectory提供web-inf文件夹路径以进行全文搜索

时间:2014-09-22 06:44:20

标签: java jsp lucene web-inf

我是apache Lucene的新手。我正在尝试开发示例全文搜索应用程序,它将使用给定的输入查询搜索html文件,如果在任何文件中找到给定的字符串,则创建索引

我的结果jsp页面如下所示:

My results jsp page look like this

如果我点击任何超链接,html文件需要在新标签页中打开,但我得到空白页。

这是我的应用程序文件夹结构

enter image description here

这是我的代码:

    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;
    }

}

如何将我的html文件包含的文件夹路径即WEB-INF / Html文件传递给FSDirectory类,当我点击我的结果页面中的任何超链接时,相应的html文件需要在浏览器的新选项卡中打开。

我怎样才能做到这一点。

提前致谢...

0 个答案:

没有答案