001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.lucene.demo.facet;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
023import org.apache.lucene.document.Document;
024import org.apache.lucene.facet.DrillDownQuery;
025import org.apache.lucene.facet.DrillSideways;
026import org.apache.lucene.facet.DrillSideways.DrillSidewaysResult;
027import org.apache.lucene.facet.FacetField;
028import org.apache.lucene.facet.FacetResult;
029import org.apache.lucene.facet.Facets;
030import org.apache.lucene.facet.FacetsCollector;
031import org.apache.lucene.facet.FacetsConfig;
032import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
033import org.apache.lucene.facet.taxonomy.TaxonomyReader;
034import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
035import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
036import org.apache.lucene.index.DirectoryReader;
037import org.apache.lucene.index.IndexWriter;
038import org.apache.lucene.index.IndexWriterConfig;
039import org.apache.lucene.index.IndexWriterConfig.OpenMode;
040import org.apache.lucene.search.IndexSearcher;
041import org.apache.lucene.search.MatchAllDocsQuery;
042import org.apache.lucene.store.ByteBuffersDirectory;
043import org.apache.lucene.store.Directory;
044
045/** Shows simple usage of faceted indexing and search. */
046public class SimpleFacetsExample {
047
048  private final Directory indexDir = new ByteBuffersDirectory();
049  private final Directory taxoDir = new ByteBuffersDirectory();
050  private final FacetsConfig config = new FacetsConfig();
051
052  /** Empty constructor */
053  public SimpleFacetsExample() {
054    config.setHierarchical("Publish Date", true);
055  }
056
057  /** Build the example index. */
058  private void index() throws IOException {
059    IndexWriter indexWriter =
060        new IndexWriter(
061            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
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(), 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 return facets for 'Author' */
151  private FacetResult drillDown() throws IOException {
152    DirectoryReader indexReader = DirectoryReader.open(indexDir);
153    IndexSearcher searcher = new IndexSearcher(indexReader);
154    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
155
156    // Passing no baseQuery means we drill down on all
157    // documents ("browse only"):
158    DrillDownQuery q = new DrillDownQuery(config);
159
160    // Now user drills down on Publish Date/2010:
161    q.add("Publish Date", "2010");
162    FacetsCollector fc = new FacetsCollector();
163    FacetsCollector.search(searcher, q, 10, fc);
164
165    // Retrieve results
166    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
167    FacetResult result = facets.getTopChildren(10, "Author");
168
169    indexReader.close();
170    taxoReader.close();
171
172    return result;
173  }
174
175  /**
176   * User drills down on 'Publish Date/2010', and we return facets for both 'Publish Date' and
177   * 'Author', using DrillSideways.
178   */
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 example = new SimpleFacetsExample();
232    List<FacetResult> results1 = example.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    List<FacetResult> results = example.runSearch();
239    System.out.println("Author: " + results.get(0));
240    System.out.println("Publish Date: " + results.get(1));
241
242    System.out.println("Facet drill-down example (Publish Date/2010):");
243    System.out.println("---------------------------------------------");
244    System.out.println("Author: " + example.runDrillDown());
245
246    System.out.println("Facet drill-sideways example (Publish Date/2010):");
247    System.out.println("---------------------------------------------");
248    for (FacetResult result : example.runDrillSideways()) {
249      System.out.println(result);
250    }
251  }
252}