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.FacetResult;
026import org.apache.lucene.facet.Facets;
027import org.apache.lucene.facet.FacetsCollector;
028import org.apache.lucene.facet.FacetsCollectorManager;
029import org.apache.lucene.facet.FacetsConfig;
030import org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState;
031import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts;
032import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
033import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState;
034import org.apache.lucene.index.DirectoryReader;
035import org.apache.lucene.index.IndexWriter;
036import org.apache.lucene.index.IndexWriterConfig;
037import org.apache.lucene.index.IndexWriterConfig.OpenMode;
038import org.apache.lucene.search.IndexSearcher;
039import org.apache.lucene.search.MatchAllDocsQuery;
040import org.apache.lucene.store.ByteBuffersDirectory;
041import org.apache.lucene.store.Directory;
042
043/**
044 * Shows simple usage of faceted indexing and search, using {@link SortedSetDocValuesFacetField} and
045 * {@link SortedSetDocValuesFacetCounts}.
046 */
047public class SimpleSortedSetFacetsExample {
048
049  private final Directory indexDir = new ByteBuffersDirectory();
050  private final FacetsConfig config = new FacetsConfig();
051
052  /** Empty constructor */
053  public SimpleSortedSetFacetsExample() {}
054
055  /** Build the example index. */
056  private void index() throws IOException {
057    IndexWriter indexWriter =
058        new IndexWriter(
059            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
060    Document doc = new Document();
061    doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
062    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
063    indexWriter.addDocument(config.build(doc));
064
065    doc = new Document();
066    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
067    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
068    indexWriter.addDocument(config.build(doc));
069
070    doc = new Document();
071    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
072    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
073    indexWriter.addDocument(config.build(doc));
074
075    doc = new Document();
076    doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
077    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
078    indexWriter.addDocument(config.build(doc));
079
080    doc = new Document();
081    doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
082    doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
083    indexWriter.addDocument(config.build(doc));
084
085    indexWriter.close();
086  }
087
088  /** User runs a query and counts facets. */
089  private List<FacetResult> search() throws IOException {
090    DirectoryReader indexReader = DirectoryReader.open(indexDir);
091    IndexSearcher searcher = new IndexSearcher(indexReader);
092    SortedSetDocValuesReaderState state =
093        new DefaultSortedSetDocValuesReaderState(indexReader, config);
094
095    // Aggregates the facet counts
096    FacetsCollectorManager fcm = new FacetsCollectorManager();
097
098    // MatchAllDocsQuery is for "browsing" (counts facets
099    // for all non-deleted docs in the index); normally
100    // you'd use a "normal" query:
101    FacetsCollector fc =
102        FacetsCollectorManager.search(searcher, MatchAllDocsQuery.INSTANCE, 10, fcm)
103            .facetsCollector();
104
105    // Retrieve results
106    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
107
108    List<FacetResult> results = new ArrayList<>();
109    results.add(facets.getTopChildren(10, "Author"));
110    results.add(facets.getTopChildren(10, "Publish Year"));
111    indexReader.close();
112
113    return results;
114  }
115
116  /** User drills down on 'Publish Year/2010'. */
117  private FacetResult drillDown() throws IOException {
118    DirectoryReader indexReader = DirectoryReader.open(indexDir);
119    IndexSearcher searcher = new IndexSearcher(indexReader);
120    SortedSetDocValuesReaderState state =
121        new DefaultSortedSetDocValuesReaderState(indexReader, config);
122
123    // Now user drills down on Publish Year/2010:
124    DrillDownQuery q = new DrillDownQuery(config);
125    q.add("Publish Year", "2010");
126    FacetsCollectorManager fcm = new FacetsCollectorManager();
127    FacetsCollector fc = FacetsCollectorManager.search(searcher, q, 10, fcm).facetsCollector();
128
129    // Retrieve results
130    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
131    FacetResult result = facets.getTopChildren(10, "Author");
132    indexReader.close();
133
134    return result;
135  }
136
137  /** Runs the search example. */
138  public List<FacetResult> runSearch() throws IOException {
139    index();
140    return search();
141  }
142
143  /** Runs the drill-down example. */
144  public FacetResult runDrillDown() throws IOException {
145    index();
146    return drillDown();
147  }
148
149  /** Runs the search and drill-down examples and prints the results. */
150  public static void main(String[] args) throws Exception {
151    System.out.println("Facet counting example:");
152    System.out.println("-----------------------");
153    SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample();
154    List<FacetResult> results = example.runSearch();
155    System.out.println("Author: " + results.get(0));
156    System.out.println("Publish Year: " + results.get(1));
157
158    System.out.println("\n");
159    System.out.println("Facet drill-down example (Publish Year/2010):");
160    System.out.println("---------------------------------------------");
161    System.out.println("Author: " + example.runDrillDown());
162  }
163}