adesh singh
adesh singh

Reputation: 1727

how to refine the search using apache lucene index

I am searching a keyword using index created by apache lucene , it returns the name of files which contains the given keyword now i want to refine the search again only in the files returned by lucene search . How is it possible to refine the search using apache lucene. I am using the following code.

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

I have added documents in the lucene index using as contents and filename.

Upvotes: 0

Views: 509

Answers (1)

torquestomp
torquestomp

Reputation: 3334

You want to use a BooleanQuery for something like this. That will let you AND the original search constraints with the refined search constraints.

Example:

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

Upvotes: 2

Related Questions