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.facet.DrillDownQuery;
025import org.apache.lucene.facet.FacetResult;
026import org.apache.lucene.facet.Facets;
027import org.apache.lucene.facet.FacetsCollector;
028import org.apache.lucene.facet.FacetsConfig;
029import org.apache.lucene.facet.LabelAndValue;
030import org.apache.lucene.facet.taxonomy.AssociationAggregationFunction;
031import org.apache.lucene.facet.taxonomy.FloatAssociationFacetField;
032import org.apache.lucene.facet.taxonomy.IntAssociationFacetField;
033import org.apache.lucene.facet.taxonomy.TaxonomyFacetFloatAssociations;
034import org.apache.lucene.facet.taxonomy.TaxonomyFacetIntAssociations;
035import org.apache.lucene.facet.taxonomy.TaxonomyReader;
036import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
037import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
038import org.apache.lucene.index.DirectoryReader;
039import org.apache.lucene.index.IndexWriter;
040import org.apache.lucene.index.IndexWriterConfig;
041import org.apache.lucene.index.IndexWriterConfig.OpenMode;
042import org.apache.lucene.search.IndexSearcher;
043import org.apache.lucene.search.MatchAllDocsQuery;
044import org.apache.lucene.store.ByteBuffersDirectory;
045import org.apache.lucene.store.Directory;
046import org.apache.lucene.util.IOUtils;
047
048/** Shows example usage of category associations. */
049public class AssociationsFacetsExample {
050
051  private final Directory indexDir = new ByteBuffersDirectory();
052  private final Directory taxoDir = new ByteBuffersDirectory();
053  private final FacetsConfig config;
054
055  /** Empty constructor */
056  public AssociationsFacetsExample() {
057    config = new FacetsConfig();
058    config.setMultiValued("tags", true);
059    config.setIndexFieldName("tags", "$tags");
060    config.setMultiValued("genre", true);
061    config.setIndexFieldName("genre", "$genre");
062  }
063
064  /** Build the example index. */
065  private void index() throws IOException {
066    IndexWriterConfig iwc =
067        new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE);
068    IndexWriter indexWriter = new IndexWriter(indexDir, iwc);
069
070    // Writes facet ords to a separate directory from the main index
071    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
072
073    Document doc = new Document();
074    // 3 occurrences for tag 'lucene'
075    doc.add(new IntAssociationFacetField(3, "tags", "lucene"));
076    // 87% confidence level of genre 'computing'
077    doc.add(new FloatAssociationFacetField(0.87f, "genre", "computing"));
078    indexWriter.addDocument(config.build(taxoWriter, doc));
079
080    doc = new Document();
081    // 1 occurrence for tag 'lucene'
082    doc.add(new IntAssociationFacetField(1, "tags", "lucene"));
083    // 2 occurrence for tag 'solr'
084    doc.add(new IntAssociationFacetField(2, "tags", "solr"));
085    // 75% confidence level of genre 'computing'
086    doc.add(new FloatAssociationFacetField(0.75f, "genre", "computing"));
087    // 34% confidence level of genre 'software'
088    doc.add(new FloatAssociationFacetField(0.34f, "genre", "software"));
089    indexWriter.addDocument(config.build(taxoWriter, doc));
090
091    IOUtils.close(indexWriter, taxoWriter);
092  }
093
094  /** User runs a query and aggregates facets by summing their association values. */
095  private List<FacetResult> sumAssociations() 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    Facets tags =
108        new TaxonomyFacetIntAssociations(
109            "$tags", taxoReader, config, fc, AssociationAggregationFunction.SUM);
110    Facets genre =
111        new TaxonomyFacetFloatAssociations(
112            "$genre", taxoReader, config, fc, AssociationAggregationFunction.SUM);
113
114    // Retrieve results
115    List<FacetResult> results = new ArrayList<>();
116    results.add(tags.getTopChildren(10, "tags"));
117    results.add(genre.getTopChildren(10, "genre"));
118
119    IOUtils.close(indexReader, taxoReader);
120
121    return results;
122  }
123
124  /** User drills down on 'tags/solr'. */
125  private FacetResult drillDown() throws IOException {
126    DirectoryReader indexReader = DirectoryReader.open(indexDir);
127    IndexSearcher searcher = new IndexSearcher(indexReader);
128    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
129
130    // Passing no baseQuery means we drill down on all
131    // documents ("browse only"):
132    DrillDownQuery q = new DrillDownQuery(config);
133
134    // Now user drills down on Publish Date/2010:
135    q.add("tags", "solr");
136    FacetsCollector fc = new FacetsCollector();
137    FacetsCollector.search(searcher, q, 10, fc);
138
139    // Retrieve results
140    Facets facets =
141        new TaxonomyFacetFloatAssociations(
142            "$genre", taxoReader, config, fc, AssociationAggregationFunction.SUM);
143    FacetResult result = facets.getTopChildren(10, "genre");
144
145    IOUtils.close(indexReader, taxoReader);
146
147    return result;
148  }
149
150  /** Runs summing association example. */
151  public List<FacetResult> runSumAssociations() throws IOException {
152    index();
153    return sumAssociations();
154  }
155
156  /** Runs the drill-down example. */
157  public FacetResult runDrillDown() throws IOException {
158    index();
159    return drillDown();
160  }
161
162  /** Runs the sum int/float associations examples and prints the results. */
163  public static void main(String[] args) throws Exception {
164    System.out.println("Sum associations example:");
165    System.out.println("-------------------------");
166    List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations();
167    System.out.println("tags: " + results.get(0));
168    System.out.println("genre: " + results.get(1));
169    System.out.println("-------------------------");
170    System.out.println("Counts per label are also available:");
171    for (FacetResult facetResult : results) {
172      for (LabelAndValue lv : facetResult.labelValues) {
173        System.out.println("\t" + lv.label + ": " + lv.count);
174      }
175    }
176  }
177}