How to filter search results using facets in Apache Lucene
<p>I am new to Apache Lucene and trying to learn how Lucene Facets can be used for my need. This is my scenario.</p>

<p>1) I did a text search and got documents D1, D2, D3, D4 and D5. 
2) The facets it created F1, F2 and F3. 
3)Based on the search result let's assume the facets point to the corresponding documents along with counts. F1 (2 ->D1, D5) , F2 (3->D2,D4,D5) and F3 (2->D2,D3)</p>

<p>4) Now let's assume there are other documents exists like D6, D7 which are not part of the search results, but exist in index and they also contain facet F1. </p>

<p>Now the issue I am facing is the following. 
1) When I searched with text and got documents from D1 to D5, I want to further filter (narrow) the search result using F1 facet. That means, Ideally from the already found search results when I use the facet F1, I should get Dcoument D1 and D5. But actually when I used Facet F1, I am getting D1, D5, D6 and D7 documents as part of facet F1. </p>

<p>So, can I not use the facet as a filtering criteria to narrow down further on an already searched results. If so, please help me with the code.  If my understanding wrong also, <strong>please guide me how to use facets to narrow down further on an already searched results if at all it is possible.</strong> The code I am using to do is the following. I am using Apache Lucene 6.2.1.</p>

<pre><code>        System.out.println("Enter query string:");
        String queryString = reader.readLine();
        QueryParser parser = new QueryParser("contents",
                new StandardAnalyzer());
        Query query = parser.parse(queryString);

        // TopDocs search = FacetsCollector.search(is, query, 10, srt, fc);
        TopDocs td1 = FacetsCollector.search(is, query, 10, fc);
        System.out.println("Total hits " + td1.totalHits);
        for (ScoreDoc scoreDoc : td1.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(
                    "Score-&gt; " + scoreDoc.score + "::" + doc.get("price"));
        }
        Facets fcCount = new FastTaxonomyFacetCounts(tr, facetConfig_, fc);
        List&lt;FacetResult&gt; allDims = fcCount.getAllDims(100);
        for (int i = 0; i &lt; allDims.size(); i++) {
            FacetResult fr = allDims.get(i);
            System.out.println("Printing for dimension - " + fr.dim);
            LabelAndValue[] labelValues = fr.labelValues;
            for (int j = 0; j &lt; labelValues.length; j++) {
                System.out.println(labelValues[j].label + "::count-&gt;"
                        + labelValues[j].value);
                System.out.println("Docs matching for dimension " + fr.dim
                        + " with value " + labelValues[j].label);

                 /*Here I am trying to search using Facet-DIM, but have no way to connect it to already found search result.*/
                DrillDownQuery dq = new DrillDownQuery(facetConfig_);
                dq.add(fr.dim, labelValues[j].label);

                FacetsCollector fc1 = new FacetsCollector();
                TopDocs td2 = FacetsCollector.search(is, dq, 10, fc1);
                System.out.println("Total hits - " + td2.totalHits);
                for (ScoreDoc scoreDoc : td2.scoreDocs) {
                    Document doc = is.doc(scoreDoc.doc);
                    System.out.println("Score-&gt; " + scoreDoc.score + "::"
                            + doc.get("price"));
                }
                System.out.println("===DRILL DOWN END===");
            }
        } 
</code></pre>