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