add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / examples / org / apache / lucene / facet / example / simple / SimpleSearcher.java
1 package org.apache.lucene.facet.example.simple;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.Term;
8 import org.apache.lucene.search.IndexSearcher;
9 import org.apache.lucene.search.Query;
10 import org.apache.lucene.search.TermQuery;
11 import org.apache.lucene.search.TopScoreDocCollector;
12
13 import org.apache.lucene.search.MultiCollector;
14 import org.apache.lucene.facet.example.ExampleUtils;
15 import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;
16 import org.apache.lucene.facet.index.params.FacetIndexingParams;
17 import org.apache.lucene.facet.search.DrillDown;
18 import org.apache.lucene.facet.search.FacetsCollector;
19 import org.apache.lucene.facet.search.params.CountFacetRequest;
20 import org.apache.lucene.facet.search.params.FacetRequest;
21 import org.apache.lucene.facet.search.params.FacetSearchParams;
22 import org.apache.lucene.facet.search.results.FacetResult;
23 import org.apache.lucene.facet.search.results.FacetResultNode;
24 import org.apache.lucene.facet.taxonomy.CategoryPath;
25 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
26
27 /**
28  * Licensed to the Apache Software Foundation (ASF) under one or more
29  * contributor license agreements.  See the NOTICE file distributed with
30  * this work for additional information regarding copyright ownership.
31  * The ASF licenses this file to You under the Apache License, Version 2.0
32  * (the "License"); you may not use this file except in compliance with
33  * the License.  You may obtain a copy of the License at
34  *
35  *     http://www.apache.org/licenses/LICENSE-2.0
36  *
37  * Unless required by applicable law or agreed to in writing, software
38  * distributed under the License is distributed on an "AS IS" BASIS,
39  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40  * See the License for the specific language governing permissions and
41  * limitations under the License.
42  */
43
44 /**
45  * SampleSearcer searches index with facets. 
46  * 
47  * @lucene.experimental
48  */
49 public class SimpleSearcher {
50   
51   /**
52    * Search an index with facets.
53    * @param indexReader index reader.
54    * @param taxoReader taxonomy reader.
55    * @throws Exception on error (no detailed exception handling here for sample simplicity
56    * @return facet results
57    */
58   public static List<FacetResult> searchWithFacets (IndexReader indexReader,
59       TaxonomyReader taxoReader) throws Exception {
60     CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("root","a"), 10);
61     return searchWithRequest(indexReader, taxoReader, null, facetRequest);
62   }
63   
64   /**
65    * Search an index with facets for given facet requests.
66    * @param indexReader index reader.
67    * @param taxoReader taxonomy reader.
68    * @param indexingParams the facet indexing params
69    * @param facetRequests facet requests of interest
70    * @throws Exception on error (no detailed exception handling here for sample simplicity
71    * @return facet results
72    */
73   public static List<FacetResult> searchWithRequest(IndexReader indexReader,
74       TaxonomyReader taxoReader, FacetIndexingParams indexingParams,
75       FacetRequest... facetRequests) throws Exception {
76     Query q = new TermQuery(new Term(SimpleUtils.TEXT, "white"));
77     return searchWithRequestAndQuery(q, indexReader, taxoReader,
78         indexingParams, facetRequests);
79   }
80   
81   /**
82    * Search an index with facets for given query and facet requests.
83    * @param q query of interest
84    * @param indexReader index reader.
85    * @param taxoReader taxonomy reader.
86    * @param indexingParams the facet indexing params
87    * @param facetRequests facet requests of interest
88    * @throws Exception on error (no detailed exception handling here for sample simplicity
89    * @return facet results
90    */
91   public static List<FacetResult> searchWithRequestAndQuery(Query q,
92       IndexReader indexReader, TaxonomyReader taxoReader,
93       FacetIndexingParams indexingParams, FacetRequest... facetRequests)
94       throws Exception {
95     
96     ExampleUtils.log("Query: " + q);
97     // prepare searcher to search against
98     IndexSearcher searcher = new IndexSearcher(indexReader);
99
100     // collect matching documents into a collector
101     TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true);
102
103     if (indexingParams == null) {
104       indexingParams = new DefaultFacetIndexingParams();
105     }
106     
107     // Faceted search parameters indicate which facets are we interested in
108     FacetSearchParams facetSearchParams = new FacetSearchParams(indexingParams);
109     
110     // Add the facet requests of interest to the search params
111     for (FacetRequest frq : facetRequests) {
112       facetSearchParams.addFacetRequest(frq);
113     }
114
115     FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);
116
117     // perform documents search and facets accumulation
118     searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));
119
120     // Obtain facets results and print them
121     List<FacetResult> res = facetsCollector.getFacetResults();
122
123     int i = 0;
124     for (FacetResult facetResult : res) {
125       ExampleUtils.log("Res " + (i++) + ": " + facetResult);
126     }
127
128     return res;
129   }
130
131   /**
132    * Search an index with facets drill-down.
133    * @param indexReader index reader.
134    * @param taxoReader taxonomy reader.
135    * @throws Exception on error (no detailed exception handling here for sample simplicity
136    * @return facet results
137    */
138   public static List<FacetResult> searchWithDrillDown(IndexReader indexReader,
139       TaxonomyReader taxoReader) throws Exception {
140
141     // base query the user is interested in
142     Query baseQuery = new TermQuery(new Term(SimpleUtils.TEXT, "white"));
143
144     // facet of interest
145     CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("root","a"), 10);
146     
147     // initial search - all docs matching the base query will contribute to the accumulation 
148     List<FacetResult> res1 = searchWithRequest(indexReader, taxoReader, null, facetRequest);
149     
150     // a single result (because there was a single request) 
151     FacetResult fres = res1.get(0);
152     
153     // assume the user is interested in the second sub-result
154     // (just take the second sub-result returned by the iterator - we know there are 3 results!)
155     Iterator<? extends FacetResultNode> resIterator = fres.getFacetResultNode().getSubResults().iterator();
156     resIterator.next(); // skip first result
157     CategoryPath categoryOfInterest = resIterator.next().getLabel();
158     
159     // drill-down preparation: turn the base query into a drill-down query for the category of interest
160     Query q2 = DrillDown.query(baseQuery, categoryOfInterest);
161     
162     // that's it - search with the new query and we're done!
163     // only documents both matching the base query AND containing the 
164     // category of interest will contribute to the new accumulation
165     return searchWithRequestAndQuery(q2, indexReader, taxoReader, null, facetRequest);
166   }
167   
168 }