簡體   English   中英

如何使用apache lucene索引優化搜索

[英]how to refine the search using apache lucene index

我正在使用由apache lucene創建的索引搜索關鍵字,它返回包含給定關鍵字的文件的名稱,我現在只想在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();}

我使用as contents和filename在lucene索引中添加了文檔。

你想對這樣的東西使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM