1

私はApache Lucene.hereに非常に慣れていません。ここでは、指定された入力クエリでhtmlファイルを検索するサンプルの全文検索アプリケーションを開発しようとしています。指定された文字列が任意のファイルで見つかった場合、インデックスが作成されます

私の結果jspページは次のようになります:

私の結果jspページは次のようになります

ハイパーリンクをクリックすると、その html ファイルを新しいタブで開く必要がありますが、空白のページが表示されます。

これは My Application フォルダ構造です

ここに画像の説明を入力

これは私のコードです:

    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 ファイルをブラウザーの新しいタブで開く必要があります。

どうすればこれを達成できますか。

前もって感謝します...

4

0 に答える 0