001package org.apache.lucene.demo.facet;
002
003import java.io.IOException;
004import java.text.ParseException;
005
006import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
007import org.apache.lucene.document.Document;
008import org.apache.lucene.document.Field.Store;
009import org.apache.lucene.document.NumericDocValuesField;
010import org.apache.lucene.document.TextField;
011import org.apache.lucene.expressions.Expression;
012import org.apache.lucene.expressions.SimpleBindings;
013import org.apache.lucene.expressions.js.JavascriptCompiler;
014import org.apache.lucene.facet.FacetField;
015import org.apache.lucene.facet.FacetResult;
016import org.apache.lucene.facet.Facets;
017import org.apache.lucene.facet.FacetsCollector;
018import org.apache.lucene.facet.FacetsConfig;
019import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource;
020import org.apache.lucene.facet.taxonomy.TaxonomyReader;
021import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
022import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
023import org.apache.lucene.index.DirectoryReader;
024import org.apache.lucene.index.IndexWriter;
025import org.apache.lucene.index.IndexWriterConfig;
026import org.apache.lucene.index.IndexWriterConfig.OpenMode;
027import org.apache.lucene.search.IndexSearcher;
028import org.apache.lucene.search.MatchAllDocsQuery;
029import org.apache.lucene.search.SortField;
030import org.apache.lucene.store.Directory;
031import org.apache.lucene.store.RAMDirectory;
032
033/*
034 * Licensed to the Apache Software Foundation (ASF) under one or more
035 * contributor license agreements.  See the NOTICE file distributed with
036 * this work for additional information regarding copyright ownership.
037 * The ASF licenses this file to You under the Apache License, Version 2.0
038 * (the "License"); you may not use this file except in compliance with
039 * the License.  You may obtain a copy of the License at
040 *
041 *     http://www.apache.org/licenses/LICENSE-2.0
042 *
043 * Unless required by applicable law or agreed to in writing, software
044 * distributed under the License is distributed on an "AS IS" BASIS,
045 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
046 * See the License for the specific language governing permissions and
047 * limitations under the License.
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(new SortField("_score", SortField.Type.SCORE)); // the score of the document
095    bindings.add(new SortField("popularity", SortField.Type.LONG)); // 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.getValueSource(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}