001package org.apache.lucene.demo.facet;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
025import org.apache.lucene.document.Document;
026import org.apache.lucene.facet.DrillDownQuery;
027import org.apache.lucene.facet.DrillSideways.DrillSidewaysResult;
028import org.apache.lucene.facet.DrillSideways;
029import org.apache.lucene.facet.FacetField;
030import org.apache.lucene.facet.FacetResult;
031import org.apache.lucene.facet.Facets;
032import org.apache.lucene.facet.FacetsCollector;
033import org.apache.lucene.facet.FacetsConfig;
034import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
035import org.apache.lucene.facet.taxonomy.TaxonomyReader;
036import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
037import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
038import org.apache.lucene.index.DirectoryReader;
039import org.apache.lucene.index.IndexWriter;
040import org.apache.lucene.index.IndexWriterConfig;
041import org.apache.lucene.search.IndexSearcher;
042import org.apache.lucene.search.MatchAllDocsQuery;
043import org.apache.lucene.store.Directory;
044import org.apache.lucene.store.RAMDirectory;
045
046/** Shows simple usage of faceted indexing and search. */
047public class SimpleFacetsExample {
048
049  private final Directory indexDir = new RAMDirectory();
050  private final Directory taxoDir = new RAMDirectory();
051  private final FacetsConfig config = new FacetsConfig();
052
053  /** Empty constructor */
054  public SimpleFacetsExample() {
055    config.setHierarchical("Publish Date", true);
056  }
057  
058  /** Build the example index. */
059  private void index() throws IOException {
060    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER, 
061        new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
062
063    // Writes facet ords to a separate directory from the main index
064    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
065
066    Document doc = new Document();
067    doc.add(new FacetField("Author", "Bob"));
068    doc.add(new FacetField("Publish Date", "2010", "10", "15"));
069    indexWriter.addDocument(config.build(taxoWriter, doc));
070
071    doc = new Document();
072    doc.add(new FacetField("Author", "Lisa"));
073    doc.add(new FacetField("Publish Date", "2010", "10", "20"));
074    indexWriter.addDocument(config.build(taxoWriter, doc));
075
076    doc = new Document();
077    doc.add(new FacetField("Author", "Lisa"));
078    doc.add(new FacetField("Publish Date", "2012", "1", "1"));
079    indexWriter.addDocument(config.build(taxoWriter, doc));
080
081    doc = new Document();
082    doc.add(new FacetField("Author", "Susan"));
083    doc.add(new FacetField("Publish Date", "2012", "1", "7"));
084    indexWriter.addDocument(config.build(taxoWriter, doc));
085
086    doc = new Document();
087    doc.add(new FacetField("Author", "Frank"));
088    doc.add(new FacetField("Publish Date", "1999", "5", "5"));
089    indexWriter.addDocument(config.build(taxoWriter, doc));
090    
091    indexWriter.close();
092    taxoWriter.close();
093  }
094
095  /** User runs a query and counts facets. */
096  private List<FacetResult> facetsWithSearch() throws IOException {
097    DirectoryReader indexReader = DirectoryReader.open(indexDir);
098    IndexSearcher searcher = new IndexSearcher(indexReader);
099    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
100
101    FacetsCollector fc = new FacetsCollector();
102
103    // MatchAllDocsQuery is for "browsing" (counts facets
104    // for all non-deleted docs in the index); normally
105    // you'd use a "normal" query:
106    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
107
108    // Retrieve results
109    List<FacetResult> results = new ArrayList<>();
110
111    // Count both "Publish Date" and "Author" dimensions
112    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
113    results.add(facets.getTopChildren(10, "Author"));
114    results.add(facets.getTopChildren(10, "Publish Date"));
115    
116    indexReader.close();
117    taxoReader.close();
118    
119    return results;
120  }
121  
122  /** User runs a query and counts facets only without collecting the matching documents.*/
123  private List<FacetResult> facetsOnly() throws IOException {
124    DirectoryReader indexReader = DirectoryReader.open(indexDir);
125    IndexSearcher searcher = new IndexSearcher(indexReader);
126    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
127
128    FacetsCollector fc = new FacetsCollector();
129
130    // MatchAllDocsQuery is for "browsing" (counts facets
131    // for all non-deleted docs in the index); normally
132    // you'd use a "normal" query:
133    searcher.search(new MatchAllDocsQuery(), null /*Filter */, fc);
134
135    // Retrieve results
136    List<FacetResult> results = new ArrayList<>();
137
138    // Count both "Publish Date" and "Author" dimensions
139    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
140   
141    results.add(facets.getTopChildren(10, "Author"));
142    results.add(facets.getTopChildren(10, "Publish Date"));
143    
144    indexReader.close();
145    taxoReader.close();
146    
147    return results;
148  }
149  
150  /** User drills down on 'Publish Date/2010', and we
151   *  return facets for 'Author' */
152  private FacetResult drillDown() throws IOException {
153    DirectoryReader indexReader = DirectoryReader.open(indexDir);
154    IndexSearcher searcher = new IndexSearcher(indexReader);
155    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
156
157    // Passing no baseQuery means we drill down on all
158    // documents ("browse only"):
159    DrillDownQuery q = new DrillDownQuery(config);
160
161    // Now user drills down on Publish Date/2010:
162    q.add("Publish Date", "2010");
163    FacetsCollector fc = new FacetsCollector();
164    FacetsCollector.search(searcher, q, 10, fc);
165
166    // Retrieve results
167    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
168    FacetResult result = facets.getTopChildren(10, "Author");
169
170    indexReader.close();
171    taxoReader.close();
172    
173    return result;
174  }
175
176  /** User drills down on 'Publish Date/2010', and we
177   *  return facets for both 'Publish Date' and 'Author',
178   *  using DrillSideways. */
179  private List<FacetResult> drillSideways() throws IOException {
180    DirectoryReader indexReader = DirectoryReader.open(indexDir);
181    IndexSearcher searcher = new IndexSearcher(indexReader);
182    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
183
184    // Passing no baseQuery means we drill down on all
185    // documents ("browse only"):
186    DrillDownQuery q = new DrillDownQuery(config);
187
188    // Now user drills down on Publish Date/2010:
189    q.add("Publish Date", "2010");
190
191    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
192    DrillSidewaysResult result = ds.search(q, 10);
193
194    // Retrieve results
195    List<FacetResult> facets = result.facets.getAllDims(10);
196
197    indexReader.close();
198    taxoReader.close();
199    
200    return facets;
201  }
202
203  /** Runs the search example. */
204  public List<FacetResult> runFacetOnly() throws IOException {
205    index();
206    return facetsOnly();
207  }
208  
209  /** Runs the search example. */
210  public List<FacetResult> runSearch() throws IOException {
211    index();
212    return facetsWithSearch();
213  }
214  
215  /** Runs the drill-down example. */
216  public FacetResult runDrillDown() throws IOException {
217    index();
218    return drillDown();
219  }
220
221  /** Runs the drill-sideways example. */
222  public List<FacetResult> runDrillSideways() throws IOException {
223    index();
224    return drillSideways();
225  }
226
227  /** Runs the search and drill-down examples and prints the results. */
228  public static void main(String[] args) throws Exception {
229    System.out.println("Facet counting example:");
230    System.out.println("-----------------------");
231    SimpleFacetsExample example1 = new SimpleFacetsExample();
232    List<FacetResult> results1 = example1.runFacetOnly();
233    System.out.println("Author: " + results1.get(0));
234    System.out.println("Publish Date: " + results1.get(1));
235    
236    System.out.println("Facet counting example (combined facets and search):");
237    System.out.println("-----------------------");
238    SimpleFacetsExample example = new SimpleFacetsExample();
239    List<FacetResult> results = example.runSearch();
240    System.out.println("Author: " + results.get(0));
241    System.out.println("Publish Date: " + results.get(1));
242    
243    System.out.println("\n");
244    System.out.println("Facet drill-down example (Publish Date/2010):");
245    System.out.println("---------------------------------------------");
246    System.out.println("Author: " + example.runDrillDown());
247
248    System.out.println("\n");
249    System.out.println("Facet drill-sideways example (Publish Date/2010):");
250    System.out.println("---------------------------------------------");
251    for(FacetResult result : example.runDrillSideways()) {
252      System.out.println(result);
253    }
254  }
255  
256}