001    package org.apache.lucene.demo.facet;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *     http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import java.io.IOException;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
025    import org.apache.lucene.document.Document;
026    import org.apache.lucene.facet.FacetField;
027    import org.apache.lucene.facet.FacetResult;
028    import org.apache.lucene.facet.Facets;
029    import org.apache.lucene.facet.FacetsCollector;
030    import org.apache.lucene.facet.FacetsConfig;
031    import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
032    import org.apache.lucene.facet.taxonomy.TaxonomyReader;
033    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
034    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
035    import org.apache.lucene.index.DirectoryReader;
036    import org.apache.lucene.index.IndexWriter;
037    import org.apache.lucene.index.IndexWriterConfig;
038    import org.apache.lucene.search.IndexSearcher;
039    import org.apache.lucene.search.MatchAllDocsQuery;
040    import org.apache.lucene.store.Directory;
041    import org.apache.lucene.store.RAMDirectory;
042    
043    /** Demonstrates indexing categories into different indexed fields. */
044    public class MultiCategoryListsFacetsExample {
045    
046      private final Directory indexDir = new RAMDirectory();
047      private final Directory taxoDir = new RAMDirectory();
048      private final FacetsConfig config = new FacetsConfig();
049    
050      /** Creates a new instance and populates the catetory list params mapping. */
051      public MultiCategoryListsFacetsExample() {
052        config.setIndexFieldName("Author", "author");
053        config.setIndexFieldName("Publish Date", "pubdate");
054        config.setHierarchical("Publish Date", true);
055      }
056    
057      /** Build the example index. */
058      private void index() throws IOException {
059        IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER, 
060            new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
061    
062        // Writes facet ords to a separate directory from the main index
063        DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
064    
065        Document doc = new Document();
066        doc.add(new FacetField("Author", "Bob"));
067        doc.add(new FacetField("Publish Date", "2010", "10", "15"));
068        indexWriter.addDocument(config.build(taxoWriter, doc));
069    
070        doc = new Document();
071        doc.add(new FacetField("Author", "Lisa"));
072        doc.add(new FacetField("Publish Date", "2010", "10", "20"));
073        indexWriter.addDocument(config.build(taxoWriter, doc));
074    
075        doc = new Document();
076        doc.add(new FacetField("Author", "Lisa"));
077        doc.add(new FacetField("Publish Date", "2012", "1", "1"));
078        indexWriter.addDocument(config.build(taxoWriter, doc));
079    
080        doc = new Document();
081        doc.add(new FacetField("Author", "Susan"));
082        doc.add(new FacetField("Publish Date", "2012", "1", "7"));
083        indexWriter.addDocument(config.build(taxoWriter, doc));
084    
085        doc = new Document();
086        doc.add(new FacetField("Author", "Frank"));
087        doc.add(new FacetField("Publish Date", "1999", "5", "5"));
088        indexWriter.addDocument(config.build(taxoWriter, doc));
089        
090        indexWriter.close();
091        taxoWriter.close();
092      }
093    
094      /** User runs a query and counts facets. */
095      private List<FacetResult> search() throws IOException {
096        DirectoryReader indexReader = DirectoryReader.open(indexDir);
097        IndexSearcher searcher = new IndexSearcher(indexReader);
098        TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
099    
100        FacetsCollector fc = new FacetsCollector();
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.search(searcher, new MatchAllDocsQuery(), 10, fc);
106    
107        // Retrieve results
108        List<FacetResult> results = new ArrayList<FacetResult>();
109    
110        // Count both "Publish Date" and "Author" dimensions
111        Facets author = new FastTaxonomyFacetCounts("author", taxoReader, config, fc);
112        results.add(author.getTopChildren(10, "Author"));
113    
114        Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, fc);
115        results.add(pubDate.getTopChildren(10, "Publish Date"));
116        
117        indexReader.close();
118        taxoReader.close();
119        
120        return results;
121      }
122    
123      /** Runs the search example. */
124      public List<FacetResult> runSearch() throws IOException {
125        index();
126        return search();
127      }
128      
129      /** Runs the search example and prints the results. */
130      public static void main(String[] args) throws Exception {
131        System.out.println("Facet counting over multiple category lists example:");
132        System.out.println("-----------------------");
133        List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
134        System.out.println("Author: " + results.get(0));
135        System.out.println("Publish Date: " + results.get(1));
136      }
137    }