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.text.ParseException;
021import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
022import org.apache.lucene.document.Document;
023import org.apache.lucene.document.Field.Store;
024import org.apache.lucene.document.NumericDocValuesField;
025import org.apache.lucene.document.TextField;
026import org.apache.lucene.expressions.Expression;
027import org.apache.lucene.expressions.SimpleBindings;
028import org.apache.lucene.expressions.js.JavascriptCompiler;
029import org.apache.lucene.facet.FacetField;
030import org.apache.lucene.facet.FacetResult;
031import org.apache.lucene.facet.Facets;
032import org.apache.lucene.facet.FacetsCollector;
033import org.apache.lucene.facet.FacetsCollectorManager;
034import org.apache.lucene.facet.FacetsConfig;
035import org.apache.lucene.facet.taxonomy.AssociationAggregationFunction;
036import org.apache.lucene.facet.taxonomy.TaxonomyFacetFloatAssociations;
037import org.apache.lucene.facet.taxonomy.TaxonomyReader;
038import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
039import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
040import org.apache.lucene.index.DirectoryReader;
041import org.apache.lucene.index.IndexWriter;
042import org.apache.lucene.index.IndexWriterConfig;
043import org.apache.lucene.index.IndexWriterConfig.OpenMode;
044import org.apache.lucene.search.DoubleValuesSource;
045import org.apache.lucene.search.IndexSearcher;
046import org.apache.lucene.search.MatchAllDocsQuery;
047import org.apache.lucene.store.ByteBuffersDirectory;
048import org.apache.lucene.store.Directory;
049import org.apache.lucene.util.IOUtils;
050
051/** Shows facets aggregation by an expression. */
052public class ExpressionAggregationFacetsExample {
053
054  private final Directory indexDir = new ByteBuffersDirectory();
055  private final Directory taxoDir = new ByteBuffersDirectory();
056  private final FacetsConfig config = new FacetsConfig();
057
058  /** Empty constructor */
059  public ExpressionAggregationFacetsExample() {}
060
061  /** Build the example index. */
062  private void index() throws IOException {
063    IndexWriter indexWriter =
064        new IndexWriter(
065            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
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    doc.add(new TextField("c", "foo bar", Store.NO));
072    doc.add(new NumericDocValuesField("popularity", 5L));
073    doc.add(new FacetField("A", "B"));
074    indexWriter.addDocument(config.build(taxoWriter, doc));
075
076    doc = new Document();
077    doc.add(new TextField("c", "foo foo bar", Store.NO));
078    doc.add(new NumericDocValuesField("popularity", 3L));
079    doc.add(new FacetField("A", "C"));
080    indexWriter.addDocument(config.build(taxoWriter, doc));
081
082    IOUtils.close(indexWriter, taxoWriter);
083  }
084
085  /** User runs a query and aggregates facets. */
086  private FacetResult search() throws IOException, ParseException {
087    DirectoryReader indexReader = DirectoryReader.open(indexDir);
088    IndexSearcher searcher = new IndexSearcher(indexReader);
089    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
090
091    // Aggregate categories by an expression that combines the document's score
092    // and its popularity field
093    Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)");
094    SimpleBindings bindings = new SimpleBindings();
095    bindings.add("_score", DoubleValuesSource.SCORES); // the score of the document
096    bindings.add(
097        "popularity",
098        DoubleValuesSource.fromLongField("popularity")); // the value of the 'popularity' field
099
100    // Aggregates the facet values
101    FacetsCollectorManager fcm = new FacetsCollectorManager(true);
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 fc =
107        FacetsCollectorManager.search(searcher, new MatchAllDocsQuery(), 10, fcm).facetsCollector();
108
109    // Retrieve results
110    Facets facets =
111        new TaxonomyFacetFloatAssociations(
112            taxoReader,
113            config,
114            fc,
115            AssociationAggregationFunction.SUM,
116            expr.getDoubleValuesSource(bindings));
117    FacetResult result = facets.getTopChildren(10, "A");
118
119    IOUtils.close(indexReader, taxoReader);
120
121    return result;
122  }
123
124  /** Runs the search example. */
125  public FacetResult runSearch() throws IOException, ParseException {
126    index();
127    return search();
128  }
129
130  /** Runs the search and drill-down examples and prints the results. */
131  public static void main(String[] args) throws Exception {
132    System.out.println("Facet counting example:");
133    System.out.println("-----------------------");
134    FacetResult result = new ExpressionAggregationFacetsExample().runSearch();
135    System.out.println(result);
136  }
137}