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.FacetsCollectorManager;
032import org.apache.lucene.facet.FacetsConfig;
033import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
034import org.apache.lucene.facet.taxonomy.TaxonomyReader;
035import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
036import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
037import org.apache.lucene.index.DirectoryReader;
038import org.apache.lucene.index.IndexWriter;
039import org.apache.lucene.index.IndexWriterConfig;
040import org.apache.lucene.index.IndexWriterConfig.OpenMode;
041import org.apache.lucene.search.IndexSearcher;
042import org.apache.lucene.search.MatchAllDocsQuery;
043import org.apache.lucene.store.ByteBuffersDirectory;
044import org.apache.lucene.store.Directory;
045import org.apache.lucene.util.IOUtils;
046
047/** Shows simple usage of faceted indexing and search. */
048public class SimpleFacetsExample {
049
050  private final Directory indexDir = new ByteBuffersDirectory();
051  private final Directory taxoDir = new ByteBuffersDirectory();
052  private final FacetsConfig config = new FacetsConfig();
053
054  /** Empty constructor */
055  public SimpleFacetsExample() {
056    config.setHierarchical("Publish Date", true);
057  }
058
059  /** Build the example index. */
060  void index() throws IOException {
061    IndexWriter indexWriter =
062        new IndexWriter(
063            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
064
065    // Writes facet ords to a separate directory from the main index
066    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
067
068    Document doc = new Document();
069    doc.add(new FacetField("Author", "Bob"));
070    doc.add(new FacetField("Publish Date", "2010", "10", "15"));
071    indexWriter.addDocument(config.build(taxoWriter, doc));
072
073    doc = new Document();
074    doc.add(new FacetField("Author", "Lisa"));
075    doc.add(new FacetField("Publish Date", "2010", "10", "20"));
076    indexWriter.addDocument(config.build(taxoWriter, doc));
077
078    doc = new Document();
079    doc.add(new FacetField("Author", "Lisa"));
080    doc.add(new FacetField("Publish Date", "2012", "1", "1"));
081    indexWriter.addDocument(config.build(taxoWriter, doc));
082
083    doc = new Document();
084    doc.add(new FacetField("Author", "Susan"));
085    doc.add(new FacetField("Publish Date", "2012", "1", "7"));
086    indexWriter.addDocument(config.build(taxoWriter, doc));
087
088    doc = new Document();
089    doc.add(new FacetField("Author", "Frank"));
090    doc.add(new FacetField("Publish Date", "1999", "5", "5"));
091    indexWriter.addDocument(config.build(taxoWriter, doc));
092
093    IOUtils.close(indexWriter, taxoWriter);
094  }
095
096  /** User runs a query and counts facets. */
097  List<FacetResult> facetsWithSearch() throws IOException {
098    DirectoryReader indexReader = DirectoryReader.open(indexDir);
099    IndexSearcher searcher = new IndexSearcher(indexReader);
100    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
101
102    FacetsCollectorManager fcm = new FacetsCollectorManager();
103
104    // MatchAllDocsQuery is for "browsing" (counts facets
105    // for all non-deleted docs in the index); normally
106    // you'd use a "normal" query:
107    FacetsCollector fc =
108        FacetsCollectorManager.search(searcher, MatchAllDocsQuery.INSTANCE, 10, fcm)
109            .facetsCollector();
110
111    // Retrieve results
112    List<FacetResult> results = new ArrayList<>();
113
114    // Count both "Publish Date" and "Author" dimensions
115    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
116    results.add(facets.getTopChildren(10, "Author"));
117    results.add(facets.getTopChildren(10, "Publish Date"));
118
119    IOUtils.close(indexReader, taxoReader);
120
121    return results;
122  }
123
124  /** User runs a query and counts facets only without collecting the matching documents. */
125  private List<FacetResult> facetsOnly() throws IOException {
126    DirectoryReader indexReader = DirectoryReader.open(indexDir);
127    IndexSearcher searcher = new IndexSearcher(indexReader);
128    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
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    FacetsCollector fc = searcher.search(MatchAllDocsQuery.INSTANCE, new FacetsCollectorManager());
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    IOUtils.close(indexReader, taxoReader);
145
146    return results;
147  }
148
149  /** User drills down on 'Publish Date/2010', and we return facets for 'Author' */
150  FacetResult drillDown() throws IOException {
151    DirectoryReader indexReader = DirectoryReader.open(indexDir);
152    IndexSearcher searcher = new IndexSearcher(indexReader);
153    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
154
155    // Passing no baseQuery means we drill down on all
156    // documents ("browse only"):
157    DrillDownQuery q = new DrillDownQuery(config);
158
159    // Now user drills down on Publish Date/2010:
160    q.add("Publish Date", "2010");
161    FacetsCollectorManager fcm = new FacetsCollectorManager();
162    FacetsCollector fc = FacetsCollectorManager.search(searcher, q, 10, fcm).facetsCollector();
163
164    // Retrieve results
165    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
166    FacetResult result = facets.getTopChildren(10, "Author");
167
168    IOUtils.close(indexReader, taxoReader);
169
170    return result;
171  }
172
173  /**
174   * User drills down on 'Publish Date/2010', and we return facets for both 'Publish Date' and
175   * 'Author', using DrillSideways.
176   */
177  private List<FacetResult> drillSideways() throws IOException {
178    DirectoryReader indexReader = DirectoryReader.open(indexDir);
179    IndexSearcher searcher = new IndexSearcher(indexReader);
180    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
181
182    // Passing no baseQuery means we drill down on all
183    // documents ("browse only"):
184    DrillDownQuery q = new DrillDownQuery(config);
185
186    // Now user drills down on Publish Date/2010:
187    q.add("Publish Date", "2010");
188
189    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
190    DrillSidewaysResult result = ds.search(q, 10);
191
192    // Retrieve results
193    List<FacetResult> facets = result.facets.getAllDims(10);
194
195    IOUtils.close(indexReader, taxoReader);
196
197    return facets;
198  }
199
200  /** Runs the search example. */
201  public List<FacetResult> runFacetOnly() throws IOException {
202    index();
203    return facetsOnly();
204  }
205
206  /** Runs the search example. */
207  public List<FacetResult> runSearch() throws IOException {
208    index();
209    return facetsWithSearch();
210  }
211
212  /** Runs the drill-down example. */
213  public FacetResult runDrillDown() throws IOException {
214    index();
215    return drillDown();
216  }
217
218  /** Runs the drill-sideways example. */
219  public List<FacetResult> runDrillSideways() throws IOException {
220    index();
221    return drillSideways();
222  }
223
224  /** Runs the search and drill-down examples and prints the results. */
225  public static void main(String[] args) throws Exception {
226    System.out.println("Facet counting example:");
227    System.out.println("-----------------------");
228    SimpleFacetsExample example = new SimpleFacetsExample();
229    List<FacetResult> results1 = example.runFacetOnly();
230    System.out.println("Author: " + results1.get(0));
231    System.out.println("Publish Date: " + results1.get(1));
232
233    System.out.println("Facet counting example (combined facets and search):");
234    System.out.println("-----------------------");
235    List<FacetResult> results = example.runSearch();
236    System.out.println("Author: " + results.get(0));
237    System.out.println("Publish Date: " + results.get(1));
238
239    System.out.println("Facet drill-down example (Publish Date/2010):");
240    System.out.println("---------------------------------------------");
241    System.out.println("Author: " + example.runDrillDown());
242
243    System.out.println("Facet drill-sideways example (Publish Date/2010):");
244    System.out.println("---------------------------------------------");
245    for (FacetResult result : example.runDrillSideways()) {
246      System.out.println(result);
247    }
248  }
249}