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.document.Field;
025import org.apache.lucene.document.KeywordField;
026import org.apache.lucene.document.SortedDocValuesField;
027import org.apache.lucene.facet.FacetResult;
028import org.apache.lucene.facet.Facets;
029import org.apache.lucene.facet.FacetsCollector;
030import org.apache.lucene.facet.FacetsCollectorManager;
031import org.apache.lucene.facet.FacetsConfig;
032import org.apache.lucene.facet.StringDocValuesReaderState;
033import org.apache.lucene.facet.StringValueFacetCounts;
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;
042import org.apache.lucene.util.BytesRef;
043
044/**
045 * Demonstrate the use of {@link StringValueFacetCounts} over {@link SortedDocValuesField} and
046 * {@link KeywordField}.
047 */
048public class StringValueFacetCountsExample {
049
050  private final Directory indexDir = new ByteBuffersDirectory();
051  private final FacetsConfig config = new FacetsConfig();
052
053  /** Empty constructor */
054  public StringValueFacetCountsExample() {}
055
056  /** Build the example index. */
057  private void index() throws IOException {
058    IndexWriter indexWriter =
059        new IndexWriter(
060            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
061
062    Document doc = new Document();
063    doc.add(new KeywordField("Author", "Bob", Field.Store.NO));
064    doc.add(new SortedDocValuesField("Publish Year", new BytesRef("2010")));
065    indexWriter.addDocument(config.build(doc));
066
067    doc = new Document();
068    doc.add(new KeywordField("Author", "Lisa", Field.Store.NO));
069    doc.add(new SortedDocValuesField("Publish Year", new BytesRef("2010")));
070    indexWriter.addDocument(config.build(doc));
071
072    doc = new Document();
073    doc.add(new KeywordField("Author", "Lisa", Field.Store.NO));
074    doc.add(new SortedDocValuesField("Publish Year", new BytesRef("2012")));
075    indexWriter.addDocument(config.build(doc));
076
077    doc = new Document();
078    doc.add(new KeywordField("Author", "Susan", Field.Store.NO));
079    doc.add(new SortedDocValuesField("Publish Year", new BytesRef("2012")));
080    indexWriter.addDocument(config.build(doc));
081
082    doc = new Document();
083    doc.add(new KeywordField("Author", "Frank", Field.Store.NO));
084    doc.add(new SortedDocValuesField("Publish Year", new BytesRef("1999")));
085    indexWriter.addDocument(config.build(doc));
086
087    indexWriter.close();
088  }
089
090  /** User runs a query and counts facets. */
091  private List<FacetResult> search() throws IOException {
092    DirectoryReader indexReader = DirectoryReader.open(indexDir);
093    IndexSearcher searcher = new IndexSearcher(indexReader);
094
095    StringDocValuesReaderState authorState = new StringDocValuesReaderState(indexReader, "Author");
096    StringDocValuesReaderState publishState =
097        new StringDocValuesReaderState(indexReader, "Publish Year");
098
099    // Aggregates the facet counts
100    FacetsCollectorManager fcm = new FacetsCollectorManager();
101
102    // MatchAllDocsQuery is for "browsing" (counts facets
103    // for all non-deleted docs in the index); normally
104    // you'd use a "normal" query:
105    FacetsCollector fc =
106        FacetsCollectorManager.search(searcher, new MatchAllDocsQuery(), 10, fcm).facetsCollector();
107
108    // Retrieve results
109    Facets authorFacets = new StringValueFacetCounts(authorState, fc);
110    Facets publishFacets = new StringValueFacetCounts(publishState, fc);
111
112    List<FacetResult> results = new ArrayList<>();
113    results.add(authorFacets.getTopChildren(10, "Author"));
114    results.add(publishFacets.getTopChildren(10, "Publish Year"));
115    indexReader.close();
116
117    return results;
118  }
119
120  /** Runs the search example. */
121  public List<FacetResult> runSearch() throws IOException {
122    index();
123    return search();
124  }
125
126  /** Runs the search and drill-down examples and prints the results. */
127  public static void main(String[] args) throws Exception {
128    System.out.println("Facet counting example:");
129    System.out.println("-----------------------");
130    StringValueFacetCountsExample example = new StringValueFacetCountsExample();
131    List<FacetResult> results = example.runSearch();
132    System.out.println("Author: " + results.get(0));
133    System.out.println("Publish Year: " + results.get(1));
134  }
135}