1 package org.apache.lucene.facet.example.multiCL;
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.index.params.FacetIndexingParams;
17 import org.apache.lucene.facet.search.FacetsCollector;
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 * MultiSearcher searches index with facets over an index with multiple
46 * @lucene.experimental
48 public class MultiCLSearcher {
51 * Search an index with facets.
54 * Directory of the search index.
56 * Directory of the taxonomy index.
58 * on error (no detailed exception handling here for sample
60 * @return facet results
62 public static List<FacetResult> searchWithFacets(Directory indexDir,
63 Directory taxoDir, FacetIndexingParams iParams) throws Exception {
65 // prepare index reader and taxonomy.
66 IndexReader indexReader = IndexReader.open(indexDir);
67 TaxonomyReader taxo = new DirectoryTaxonomyReader(taxoDir);
70 List<FacetResult> results = searchWithFacets(indexReader, taxo, iParams);
72 // we're done, close the index reader and the taxonomy.
78 public static List<FacetResult> searchWithFacets(IndexReader indexReader,
79 TaxonomyReader taxo, FacetIndexingParams iParams) throws Exception {
80 // prepare searcher to search against
81 IndexSearcher searcher = new IndexSearcher(indexReader);
83 // faceted search is working in 2 steps:
84 // 1. collect matching documents
85 // 2. aggregate facets for collected documents and
86 // generate the requested faceted results from the aggregated facets
88 // step 1: create a query for finding matching documents for which we
90 Query q = new TermQuery(new Term(SimpleUtils.TEXT, "Quis"));
91 ExampleUtils.log("Query: " + q);
93 TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10,
96 // Faceted search parameters indicate which facets are we interested in
97 FacetSearchParams facetSearchParams = new FacetSearchParams(iParams);
98 facetSearchParams.addFacetRequest(new CountFacetRequest(
99 new CategoryPath("5"), 10));
100 facetSearchParams.addFacetRequest(new CountFacetRequest(
101 new CategoryPath("5", "5"), 10));
102 facetSearchParams.addFacetRequest(new CountFacetRequest(
103 new CategoryPath("6", "2"), 10));
105 // Facets collector is the simplest interface for faceted search.
106 // It provides faceted search functions that are sufficient to many
108 // although it is insufficient for tight control on faceted search
109 // behavior - in those
110 // situations other, more low-level interfaces are available, as
111 // demonstrated in other search examples.
112 FacetsCollector facetsCollector = new FacetsCollector(
113 facetSearchParams, indexReader, taxo);
115 // perform documents search and facets accumulation
116 searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));
118 // Obtain facets results and print them
119 List<FacetResult> res = facetsCollector.getFacetResults();
122 for (FacetResult facetResult : res) {
123 ExampleUtils.log("Res " + (i++) + ": " + facetResult);