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