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.FacetResult;
027    import org.apache.lucene.facet.Facets;
028    import org.apache.lucene.facet.FacetsCollector;
029    import org.apache.lucene.facet.FacetsConfig;
030    import org.apache.lucene.facet.taxonomy.FloatAssociationFacetField;
031    import org.apache.lucene.facet.taxonomy.IntAssociationFacetField;
032    import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumFloatAssociations;
033    import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumIntAssociations;
034    import org.apache.lucene.facet.taxonomy.TaxonomyReader;
035    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
036    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
037    import org.apache.lucene.index.DirectoryReader;
038    import org.apache.lucene.index.IndexWriter;
039    import org.apache.lucene.index.IndexWriterConfig;
040    import org.apache.lucene.search.IndexSearcher;
041    import org.apache.lucene.search.MatchAllDocsQuery;
042    import org.apache.lucene.store.Directory;
043    import org.apache.lucene.store.RAMDirectory;
044    
045    /** Shows example usage of category associations. */
046    public class AssociationsFacetsExample {
047    
048      private final Directory indexDir = new RAMDirectory();
049      private final Directory taxoDir = new RAMDirectory();
050      private final FacetsConfig config;
051    
052      /** Empty constructor */
053      public AssociationsFacetsExample() {
054        config = new FacetsConfig();
055        config.setMultiValued("tags", true);
056        config.setIndexFieldName("tags", "$tags");
057        config.setMultiValued("genre", true);
058        config.setIndexFieldName("genre", "$genre");
059      }
060      
061      /** Build the example index. */
062      private void index() throws IOException {
063        IndexWriterConfig iwc = new IndexWriterConfig(FacetExamples.EXAMPLES_VER, 
064                                                      new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER));
065        IndexWriter indexWriter = new IndexWriter(indexDir, iwc);
066    
067        // Writes facet ords to a separate directory from the main index
068        DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
069    
070        Document doc = new Document();
071        // 3 occurrences for tag 'lucene'
072        doc.add(new IntAssociationFacetField(3, "tags", "lucene"));
073        // 87% confidence level of genre 'computing'
074        doc.add(new FloatAssociationFacetField(0.87f, "genre", "computing"));
075        indexWriter.addDocument(config.build(taxoWriter, doc));
076    
077        doc = new Document();
078        // 1 occurrence for tag 'lucene'
079        doc.add(new IntAssociationFacetField(1, "tags", "lucene"));
080        // 2 occurrence for tag 'solr'
081        doc.add(new IntAssociationFacetField(2, "tags", "solr"));
082        // 75% confidence level of genre 'computing'
083        doc.add(new FloatAssociationFacetField(0.75f, "genre", "computing"));
084        // 34% confidence level of genre 'software'
085        doc.add(new FloatAssociationFacetField(0.34f, "genre", "software"));
086        indexWriter.addDocument(config.build(taxoWriter, doc));
087    
088        indexWriter.close();
089        taxoWriter.close();
090      }
091    
092      /** User runs a query and aggregates facets by summing their association values. */
093      private List<FacetResult> sumAssociations() throws IOException {
094        DirectoryReader indexReader = DirectoryReader.open(indexDir);
095        IndexSearcher searcher = new IndexSearcher(indexReader);
096        TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
097        
098        FacetsCollector fc = new FacetsCollector();
099        
100        // MatchAllDocsQuery is for "browsing" (counts facets
101        // for all non-deleted docs in the index); normally
102        // you'd use a "normal" query:
103        FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
104        
105        Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc);
106        Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);
107    
108        // Retrieve results
109        List<FacetResult> results = new ArrayList<FacetResult>();
110        results.add(tags.getTopChildren(10, "tags"));
111        results.add(genre.getTopChildren(10, "genre"));
112    
113        indexReader.close();
114        taxoReader.close();
115        
116        return results;
117      }
118      
119      /** Runs summing association example. */
120      public List<FacetResult> runSumAssociations() throws IOException {
121        index();
122        return sumAssociations();
123      }
124      
125      /** Runs the sum int/float associations examples and prints the results. */
126      public static void main(String[] args) throws Exception {
127        System.out.println("Sum associations example:");
128        System.out.println("-------------------------");
129        List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations();
130        System.out.println("tags: " + results.get(0));
131        System.out.println("genre: " + results.get(1));
132      }
133    }