1 package org.apache.lucene.facet.example.adaptive;
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.Term;
7 import org.apache.lucene.search.IndexSearcher;
8 import org.apache.lucene.search.Query;
9 import org.apache.lucene.search.TermQuery;
10 import org.apache.lucene.search.TopScoreDocCollector;
11 import org.apache.lucene.store.Directory;
13 import org.apache.lucene.search.MultiCollector;
14 import org.apache.lucene.facet.example.ExampleUtils;
15 import org.apache.lucene.facet.example.simple.SimpleUtils;
16 import org.apache.lucene.facet.search.AdaptiveFacetsAccumulator;
17 import org.apache.lucene.facet.search.ScoredDocIdCollector;
18 import org.apache.lucene.facet.search.params.CountFacetRequest;
19 import org.apache.lucene.facet.search.params.FacetSearchParams;
20 import org.apache.lucene.facet.search.results.FacetResult;
21 import org.apache.lucene.facet.taxonomy.CategoryPath;
22 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
23 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
26 * Licensed to the Apache Software Foundation (ASF) under one or more
27 * contributor license agreements. See the NOTICE file distributed with
28 * this work for additional information regarding copyright ownership.
29 * The ASF licenses this file to You under the Apache License, Version 2.0
30 * (the "License"); you may not use this file except in compliance with
31 * the License. You may obtain a copy of the License at
33 * http://www.apache.org/licenses/LICENSE-2.0
35 * Unless required by applicable law or agreed to in writing, software
36 * distributed under the License is distributed on an "AS IS" BASIS,
37 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
43 * Search with facets through the {@link AdaptiveFacetsAccumulator}
45 * @lucene.experimental
47 public class AdaptiveSearcher {
50 * Search with facets through the {@link AdaptiveFacetsAccumulator}
51 * @param indexDir Directory of the search index.
52 * @param taxoDir Directory of the taxonomy index.
53 * @throws Exception on error (no detailed exception handling here for sample simplicity
54 * @return facet results
56 public static List<FacetResult> searchWithFacets (Directory indexDir, Directory taxoDir) throws Exception {
57 // prepare index reader and taxonomy.
58 TaxonomyReader taxo = new DirectoryTaxonomyReader(taxoDir);
59 IndexReader indexReader = IndexReader.open(indexDir);
61 // prepare searcher to search against
62 IndexSearcher searcher = new IndexSearcher(indexReader);
64 // faceted search is working in 2 steps:
65 // 1. collect matching documents
66 // 2. aggregate facets for collected documents and
67 // generate the requested faceted results from the aggregated facets
69 // step 1: collect matching documents into a collector
70 Query q = new TermQuery(new Term(SimpleUtils.TEXT,"white"));
71 ExampleUtils.log("Query: "+q);
73 // regular collector for scoring matched documents
74 TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true);
76 // docids collector for guiding facets accumulation (scoring disabled)
77 ScoredDocIdCollector docIdsCollecor = ScoredDocIdCollector.create(indexReader.maxDoc(), false);
79 // Faceted search parameters indicate which facets are we interested in
80 FacetSearchParams facetSearchParams = new FacetSearchParams();
81 facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));
83 // search, into both collectors. note: in case only facets accumulation
84 // is required, the topDocCollector part can be totally discarded
85 searcher.search(q, MultiCollector.wrap(topDocsCollector, docIdsCollecor));
87 // Obtain facets results and print them
88 AdaptiveFacetsAccumulator accumulator = new AdaptiveFacetsAccumulator(facetSearchParams, indexReader, taxo);
89 List<FacetResult> res = accumulator.accumulate(docIdsCollecor.getScoredDocIDs());
92 for (FacetResult facetResult : res) {
93 ExampleUtils.log("Res "+(i++)+": "+facetResult);
96 // we're done, close the index reader and the taxonomy.