2013-04-06 76 views
0

我正在使用由apache lucene創建的索引搜索關鍵字,它將返回包含給定關鍵字的文件的名稱現在我想僅在由lucene返回的文件中再次優化搜索搜索。如何使用apache lucene改進搜索。 我正在使用以下代碼。如何使用apache lucene索引優化搜索

    try 
        { 

        File indexDir=new File("path upto the index directory"); 
        Directory directory = FSDirectory.open(indexDir); 

        IndexSearcher searcher = new IndexSearcher(directory, true); 

        QueryParser parser = new QueryParser(Version.LUCENE_36, "contents", new SimpleAnalyzer(Version.LUCENE_36)); 
        Query query = parser.parse(qu); 
        query.setBoost((float) 1.5); 
        TopDocs topDocs = searcher.search(query, maxhits); 
        ScoreDoc[] hits = topDocs.scoreDocs; 
        len = hits.length; 
        int docId = 0; 
        Document d; 


       for (i = 0; i<len; i++) { 
       docId = hits[i].doc; 
       d = searcher.doc(docId); 
       filename= d.get(("filename")); 

        } 



        } 


     catch(Exception ex){ex.printStackTrace();} 

我已經使用內容和文件名在lucene索引中添加了文檔。

回答

2

你想使用BooleanQuery這樣的東西。這將讓你和原始的搜索約束與精確的搜索約束。

例子:

BooleanQuery query = new BooleanQuery(); 
    Query origSearch = getOrigSearch(searchString); 
    Query refinement = makeRefinement(); 
    query.add(origSearch, Occur.MUST); 
    query.add(refinement, Occur.MUST); 
    TopDocs topDocs = searcher.search(query, maxHits); 
+0

我不明白這裏getOrigSearch()和makeRefinement()方法 – 2013-04-08 03:47:02

+0

是它可以搜索字符串數組,而不是一個簡單的字符串 – 2013-04-08 06:17:37

+0

這些方法都是基於你的問題只是僞代碼。您的QueryParser和分析器應該處理字符串數組搜索就好了。 – torquestomp 2013-04-08 14:06:05