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, new MatchAllDocsQuery(), 10, fcm).facetsCollector();
103
104    // Retrieve results
105    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
106
107    List<FacetResult> results = new ArrayList<>();
108    results.add(facets.getTopChildren(10, "Author"));
109    results.add(facets.getTopChildren(10, "Publish Year"));
110    indexReader.close();
111
112    return results;
113  }
114
115  /** User drills down on 'Publish Year/2010'. */
116  private FacetResult drillDown() throws IOException {
117    DirectoryReader indexReader = DirectoryReader.open(indexDir);
118    IndexSearcher searcher = new IndexSearcher(indexReader);
119    SortedSetDocValuesReaderState state =
120        new DefaultSortedSetDocValuesReaderState(indexReader, config);
121
122    // Now user drills down on Publish Year/2010:
123    DrillDownQuery q = new DrillDownQuery(config);
124    q.add("Publish Year", "2010");
125    FacetsCollectorManager fcm = new FacetsCollectorManager();
126    FacetsCollector fc = FacetsCollectorManager.search(searcher, q, 10, fcm).facetsCollector();
127
128    // Retrieve results
129    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
130    FacetResult result = facets.getTopChildren(10, "Author");
131    indexReader.close();
132
133    return result;
134  }
135
136  /** Runs the search example. */
137  public List<FacetResult> runSearch() throws IOException {
138    index();
139    return search();
140  }
141
142  /** Runs the drill-down example. */
143  public FacetResult runDrillDown() throws IOException {
144    index();
145    return drillDown();
146  }
147
148  /** Runs the search and drill-down examples and prints the results. */
149  public static void main(String[] args) throws Exception {
150    System.out.println("Facet counting example:");
151    System.out.println("-----------------------");
152    SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample();
153    List<FacetResult> results = example.runSearch();
154    System.out.println("Author: " + results.get(0));
155    System.out.println("Publish Year: " + results.get(1));
156
157    System.out.println("\n");
158    System.out.println("Facet drill-down example (Publish Year/2010):");
159    System.out.println("---------------------------------------------");
160    System.out.println("Author: " + example.runDrillDown());
161  }
162}