add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / examples / org / apache / lucene / facet / example / multiCL / MultiCLSearcher.java
1 package org.apache.lucene.facet.example.multiCL;
2
3 import java.util.List;
4
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;
12
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.lucene.LuceneTaxonomyReader;
24
25 /**
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
32  *
33  *     http://www.apache.org/licenses/LICENSE-2.0
34  *
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.
40  */
41
42 /**
43  * MultiSearcher searches index with facets over an index with multiple
44  * category lists.
45  * 
46  * @lucene.experimental
47  */
48 public class MultiCLSearcher {
49
50   /**
51    * Search an index with facets.
52    * 
53    * @param indexDir
54    *            Directory of the search index.
55    * @param taxoDir
56    *            Directory of the taxonomy index.
57    * @throws Exception
58    *             on error (no detailed exception handling here for sample
59    *             simplicity
60    * @return facet results
61    */
62   public static List<FacetResult> searchWithFacets(Directory indexDir,
63       Directory taxoDir, FacetIndexingParams iParams) throws Exception {
64     
65     // prepare index reader and taxonomy.
66     IndexReader indexReader = IndexReader.open(indexDir);
67     TaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
68     
69     // Get results
70     List<FacetResult> results = searchWithFacets(indexReader, taxo, iParams);
71     
72     // we're done, close the index reader and the taxonomy.
73     indexReader.close();
74     taxo.close();
75     return results;
76   }
77   
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);
82
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
87
88     // step 1: create a query for finding matching documents for which we
89     // accumulate facets
90     Query q = new TermQuery(new Term(SimpleUtils.TEXT, "Quis"));
91     ExampleUtils.log("Query: " + q);
92
93     TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10,
94         true);
95
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));
104
105     // Facets collector is the simplest interface for faceted search.
106     // It provides faceted search functions that are sufficient to many
107     // application,
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);
114
115     // perform documents search and facets accumulation
116     searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));
117
118     // Obtain facets results and print them
119     List<FacetResult> res = facetsCollector.getFacetResults();
120
121     int i = 0;
122     for (FacetResult facetResult : res) {
123       ExampleUtils.log("Res " + (i++) + ": " + facetResult);
124     }
125     return res;
126   }
127
128 }