    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;
    045
    046/** Shows simple usage of faceted indexing and search. */
    047public class SimpleFacetsExample {
    048
    049  private final Directory indexDir = new ByteBuffersDirectory();
    050  private final Directory taxoDir = new ByteBuffersDirectory();
    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 =
    061        new IndexWriter(
    062            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    063
    064    // Writes facet ords to a separate directory from the main index
    065    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    066
    067    Document doc = new Document();
    068    doc.add(new FacetField("Author", "Bob"));
    069    doc.add(new FacetField("Publish Date", "2010", "10", "15"));
    070    indexWriter.addDocument(config.build(taxoWriter, doc));
    071
    072    doc = new Document();
    073    doc.add(new FacetField("Author", "Lisa"));
    074    doc.add(new FacetField("Publish Date", "2010", "10", "20"));
    075    indexWriter.addDocument(config.build(taxoWriter, doc));
    076
    077    doc = new Document();
    078    doc.add(new FacetField("Author", "Lisa"));
    079    doc.add(new FacetField("Publish Date", "2012", "1", "1"));
    080    indexWriter.addDocument(config.build(taxoWriter, doc));
    081
    082    doc = new Document();
    083    doc.add(new FacetField("Author", "Susan"));
    084    doc.add(new FacetField("Publish Date", "2012", "1", "7"));
    085    indexWriter.addDocument(config.build(taxoWriter, doc));
    086
    087    doc = new Document();
    088    doc.add(new FacetField("Author", "Frank"));
    089    doc.add(new FacetField("Publish Date", "1999", "5", "5"));
    090    indexWriter.addDocument(config.build(taxoWriter, doc));
    091
    092    indexWriter.close();
    093    taxoWriter.close();
    094  }
    095
    096  /** User runs a query and counts facets. */
    097  private 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    indexReader.close();
    118    taxoReader.close();
    119
    120    return results;
    121  }
    122
    123  /** User runs a query and counts facets only without collecting the matching documents. */
    124  private List<FacetResult> facetsOnly() throws IOException {
    125    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    126    IndexSearcher searcher = new IndexSearcher(indexReader);
    127    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    128
    129    // MatchAllDocsQuery is for "browsing" (counts facets
    130    // for all non-deleted docs in the index); normally
    131    // you'd use a "normal" query:
    132    FacetsCollector fc = searcher.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
    133
    134    // Retrieve results
    135    List<FacetResult> results = new ArrayList<>();
    136
    137    // Count both "Publish Date" and "Author" dimensions
    138    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    139
    140    results.add(facets.getTopChildren(10, "Author"));
    141    results.add(facets.getTopChildren(10, "Publish Date"));
    142
    143    indexReader.close();
    144    taxoReader.close();
    145
    146    return results;
    147  }
    148
    149  /** User drills down on 'Publish Date/2010', and we return facets for 'Author' */
    150  private 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    FacetsCollector fc = new FacetsCollector();
    162    FacetsCollector.search(searcher, q, 10, fc);
    163
    164    // Retrieve results
    165    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    166    FacetResult result = facets.getTopChildren(10, "Author");
    167
    168    indexReader.close();
    169    taxoReader.close();
    170
    171    return result;
    172  }
    173
    174  /**
    175   * User drills down on 'Publish Date/2010', and we return facets for both 'Publish Date' and
    176   * 'Author', using DrillSideways.
    177   */
    178  private List<FacetResult> drillSideways() throws IOException {
    179    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    180    IndexSearcher searcher = new IndexSearcher(indexReader);
    181    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    182
    183    // Passing no baseQuery means we drill down on all
    184    // documents ("browse only"):
    185    DrillDownQuery q = new DrillDownQuery(config);
    186
    187    // Now user drills down on Publish Date/2010:
    188    q.add("Publish Date", "2010");
    189
    190    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    191    DrillSidewaysResult result = ds.search(q, 10);
    192
    193    // Retrieve results
    194    List<FacetResult> facets = result.facets.getAllDims(10);
    195
    196    indexReader.close();
    197    taxoReader.close();
    198
    199    return facets;
    200  }
    201
    202  /** Runs the search example. */
    203  public List<FacetResult> runFacetOnly() throws IOException {
    204    index();
    205    return facetsOnly();
    206  }
    207
    208  /** Runs the search example. */
    209  public List<FacetResult> runSearch() throws IOException {
    210    index();
    211    return facetsWithSearch();
    212  }
    213
    214  /** Runs the drill-down example. */
    215  public FacetResult runDrillDown() throws IOException {
    216    index();
    217    return drillDown();
    218  }
    219
    220  /** Runs the drill-sideways example. */
    221  public List<FacetResult> runDrillSideways() throws IOException {
    222    index();
    223    return drillSideways();
    224  }
    225
    226  /** Runs the search and drill-down examples and prints the results. */
    227  public static void main(String[] args) throws Exception {
    228    System.out.println("Facet counting example:");
    229    System.out.println("-----------------------");
    230    SimpleFacetsExample example = new SimpleFacetsExample();
    231    List<FacetResult> results1 = example.runFacetOnly();
    232    System.out.println("Author: " + results1.get(0));
    233    System.out.println("Publish Date: " + results1.get(1));
    234
    235    System.out.println("Facet counting example (combined facets and search):");
    236    System.out.println("-----------------------");
    237    List<FacetResult> results = example.runSearch();
    238    System.out.println("Author: " + results.get(0));
    239    System.out.println("Publish Date: " + results.get(1));
    240
    241    System.out.println("Facet drill-down example (Publish Date/2010):");
    242    System.out.println("---------------------------------------------");
    243    System.out.println("Author: " + example.runDrillDown());
    244
    245    System.out.println("Facet drill-sideways example (Publish Date/2010):");
    246    System.out.println("---------------------------------------------");
    247    for (FacetResult result : example.runDrillSideways()) {
    248      System.out.println(result);
    249    }
    250  }
    251}