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    FacetsCollector fc = new FacetsCollector();
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.search(searcher, new MatchAllDocsQuery(), 10, fc);
108
109    // Retrieve results
110    List<FacetResult> results = new ArrayList<>();
111
112    // Count both "Publish Date" and "Author" dimensions
113    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
114    results.add(facets.getTopChildren(10, "Author"));
115    results.add(facets.getTopChildren(10, "Publish Date"));
116
117    IOUtils.close(indexReader, taxoReader);
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    // MatchAllDocsQuery is for "browsing" (counts facets
129    // for all non-deleted docs in the index); normally
130    // you'd use a "normal" query:
131    FacetsCollector fc = searcher.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
132
133    // Retrieve results
134    List<FacetResult> results = new ArrayList<>();
135
136    // Count both "Publish Date" and "Author" dimensions
137    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
138
139    results.add(facets.getTopChildren(10, "Author"));
140    results.add(facets.getTopChildren(10, "Publish Date"));
141
142    IOUtils.close(indexReader, taxoReader);
143
144    return results;
145  }
146
147  /** User drills down on 'Publish Date/2010', and we return facets for 'Author' */
148  FacetResult drillDown() throws IOException {
149    DirectoryReader indexReader = DirectoryReader.open(indexDir);
150    IndexSearcher searcher = new IndexSearcher(indexReader);
151    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
152
153    // Passing no baseQuery means we drill down on all
154    // documents ("browse only"):
155    DrillDownQuery q = new DrillDownQuery(config);
156
157    // Now user drills down on Publish Date/2010:
158    q.add("Publish Date", "2010");
159    FacetsCollector fc = new FacetsCollector();
160    FacetsCollector.search(searcher, q, 10, fc);
161
162    // Retrieve results
163    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
164    FacetResult result = facets.getTopChildren(10, "Author");
165
166    IOUtils.close(indexReader, taxoReader);
167
168    return result;
169  }
170
171  /**
172   * User drills down on 'Publish Date/2010', and we return facets for both 'Publish Date' and
173   * 'Author', using DrillSideways.
174   */
175  private List<FacetResult> drillSideways() throws IOException {
176    DirectoryReader indexReader = DirectoryReader.open(indexDir);
177    IndexSearcher searcher = new IndexSearcher(indexReader);
178    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
179
180    // Passing no baseQuery means we drill down on all
181    // documents ("browse only"):
182    DrillDownQuery q = new DrillDownQuery(config);
183
184    // Now user drills down on Publish Date/2010:
185    q.add("Publish Date", "2010");
186
187    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
188    DrillSidewaysResult result = ds.search(q, 10);
189
190    // Retrieve results
191    List<FacetResult> facets = result.facets.getAllDims(10);
192
193    IOUtils.close(indexReader, taxoReader);
194
195    return facets;
196  }
197
198  /** Runs the search example. */
199  public List<FacetResult> runFacetOnly() throws IOException {
200    index();
201    return facetsOnly();
202  }
203
204  /** Runs the search example. */
205  public List<FacetResult> runSearch() throws IOException {
206    index();
207    return facetsWithSearch();
208  }
209
210  /** Runs the drill-down example. */
211  public FacetResult runDrillDown() throws IOException {
212    index();
213    return drillDown();
214  }
215
216  /** Runs the drill-sideways example. */
217  public List<FacetResult> runDrillSideways() throws IOException {
218    index();
219    return drillSideways();
220  }
221
222  /** Runs the search and drill-down examples and prints the results. */
223  public static void main(String[] args) throws Exception {
224    System.out.println("Facet counting example:");
225    System.out.println("-----------------------");
226    SimpleFacetsExample example = new SimpleFacetsExample();
227    List<FacetResult> results1 = example.runFacetOnly();
228    System.out.println("Author: " + results1.get(0));
229    System.out.println("Publish Date: " + results1.get(1));
230
231    System.out.println("Facet counting example (combined facets and search):");
232    System.out.println("-----------------------");
233    List<FacetResult> results = example.runSearch();
234    System.out.println("Author: " + results.get(0));
235    System.out.println("Publish Date: " + results.get(1));
236
237    System.out.println("Facet drill-down example (Publish Date/2010):");
238    System.out.println("---------------------------------------------");
239    System.out.println("Author: " + example.runDrillDown());
240
241    System.out.println("Facet drill-sideways example (Publish Date/2010):");
242    System.out.println("---------------------------------------------");
243    for (FacetResult result : example.runDrillSideways()) {
244      System.out.println(result);
245    }
246  }
247}