    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
    019
    020import java.io.IOException;
    021import java.util.ArrayList;
    022import java.util.List;
    023
    024import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
    025import org.apache.lucene.document.Document;
    026import org.apache.lucene.facet.FacetField;
    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.taxonomy.FastTaxonomyFacetCounts;
    032import org.apache.lucene.facet.taxonomy.TaxonomyReader;
    033import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
    034import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
    035import org.apache.lucene.index.DirectoryReader;
    036import org.apache.lucene.index.IndexWriter;
    037import org.apache.lucene.index.IndexWriterConfig;
    038import org.apache.lucene.index.IndexWriterConfig.OpenMode;
    039import org.apache.lucene.search.IndexSearcher;
    040import org.apache.lucene.search.MatchAllDocsQuery;
    041import org.apache.lucene.store.Directory;
    042import org.apache.lucene.store.RAMDirectory;
    043
    044/** Demonstrates indexing categories into different indexed fields. */
    045public class MultiCategoryListsFacetsExample {
    046
    047  private final Directory indexDir = new RAMDirectory();
    048  private final Directory taxoDir = new RAMDirectory();
    049  private final FacetsConfig config = new FacetsConfig();
    050
    051  /** Creates a new instance and populates the category list params mapping. */
    052  public MultiCategoryListsFacetsExample() {
    053    config.setIndexFieldName("Author", "author");
    054    config.setIndexFieldName("Publish Date", "pubdate");
    055    config.setHierarchical("Publish Date", true);
    056  }
    057
    058  /** Build the example index. */
    059  private void index() throws IOException {
    060    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
    061        new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    062
    063    // Writes facet ords to a separate directory from the main index
    064    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    065
    066    Document doc = new Document();
    067    doc.add(new FacetField("Author", "Bob"));
    068    doc.add(new FacetField("Publish Date", "2010", "10", "15"));
    069    indexWriter.addDocument(config.build(taxoWriter, doc));
    070
    071    doc = new Document();
    072    doc.add(new FacetField("Author", "Lisa"));
    073    doc.add(new FacetField("Publish Date", "2010", "10", "20"));
    074    indexWriter.addDocument(config.build(taxoWriter, doc));
    075
    076    doc = new Document();
    077    doc.add(new FacetField("Author", "Lisa"));
    078    doc.add(new FacetField("Publish Date", "2012", "1", "1"));
    079    indexWriter.addDocument(config.build(taxoWriter, doc));
    080
    081    doc = new Document();
    082    doc.add(new FacetField("Author", "Susan"));
    083    doc.add(new FacetField("Publish Date", "2012", "1", "7"));
    084    indexWriter.addDocument(config.build(taxoWriter, doc));
    085
    086    doc = new Document();
    087    doc.add(new FacetField("Author", "Frank"));
    088    doc.add(new FacetField("Publish Date", "1999", "5", "5"));
    089    indexWriter.addDocument(config.build(taxoWriter, doc));
    090
    091    indexWriter.close();
    092    taxoWriter.close();
    093  }
    094
    095  /** User runs a query and counts facets. */
    096  private List<FacetResult> search() throws IOException {
    097    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    098    IndexSearcher searcher = new IndexSearcher(indexReader);
    099    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    100
    101    FacetsCollector fc = new FacetsCollector();
    102
    103    // MatchAllDocsQuery is for "browsing" (counts facets
    104    // for all non-deleted docs in the index); normally
    105    // you'd use a "normal" query:
    106    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
    107
    108    // Retrieve results
    109    List<FacetResult> results = new ArrayList<>();
    110
    111    // Count both "Publish Date" and "Author" dimensions
    112    Facets author = new FastTaxonomyFacetCounts("author", taxoReader, config, fc);
    113    results.add(author.getTopChildren(10, "Author"));
    114
    115    Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, fc);
    116    results.add(pubDate.getTopChildren(10, "Publish Date"));
    117
    118    indexReader.close();
    119    taxoReader.close();
    120
    121    return results;
    122  }
    123
    124  /** Runs the search example. */
    125  public List<FacetResult> runSearch() throws IOException {
    126    index();
    127    return search();
    128  }
    129
    130  /** Runs the search example and prints the results. */
    131  public static void main(String[] args) throws Exception {
    132    System.out.println("Facet counting over multiple category lists example:");
    133    System.out.println("-----------------------");
    134    List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
    135    System.out.println("Author: " + results.get(0));
    136    System.out.println("Publish Date: " + results.get(1));
    137  }
    138}