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;
021
022import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
023import org.apache.lucene.document.Document;
024import org.apache.lucene.document.Field.Store;
025import org.apache.lucene.document.NumericDocValuesField;
026import org.apache.lucene.document.TextField;
027import org.apache.lucene.expressions.Expression;
028import org.apache.lucene.expressions.SimpleBindings;
029import org.apache.lucene.expressions.js.JavascriptCompiler;
030import org.apache.lucene.facet.FacetField;
031import org.apache.lucene.facet.FacetResult;
032import org.apache.lucene.facet.Facets;
033import org.apache.lucene.facet.FacetsCollector;
034import org.apache.lucene.facet.FacetsConfig;
035import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource;
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.Directory;
047import org.apache.lucene.store.RAMDirectory;
048
049
050/** Shows facets aggregation by an expression. */
051public class ExpressionAggregationFacetsExample {
052
053  private final Directory indexDir = new RAMDirectory();
054  private final Directory taxoDir = new RAMDirectory();
055  private final FacetsConfig config = new FacetsConfig();
056
057  /** Empty constructor */
058  public ExpressionAggregationFacetsExample() {}
059  
060  /** Build the example index. */
061  private void index() throws IOException {
062    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
063        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("popularity", DoubleValuesSource.fromLongField("popularity")); // the value of the 'popularity' field
096
097    // Aggregates the facet values
098    FacetsCollector fc = new FacetsCollector(true);
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    // Retrieve results
106    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, expr.getDoubleValuesSource(bindings));
107    FacetResult result = facets.getTopChildren(10, "A");
108    
109    indexReader.close();
110    taxoReader.close();
111    
112    return result;
113  }
114  
115  /** Runs the search example. */
116  public FacetResult runSearch() throws IOException, ParseException {
117    index();
118    return search();
119  }
120  
121  /** Runs the search and drill-down examples and prints the results. */
122  public static void main(String[] args) throws Exception {
123    System.out.println("Facet counting example:");
124    System.out.println("-----------------------");
125    FacetResult result = new ExpressionAggregationFacetsExample().runSearch();
126    System.out.println(result);
127  }
128}