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