001 package org.apache.lucene.demo.facet;
002
003 import java.io.IOException;
004 import java.text.ParseException;
005 import java.util.Collections;
006 import java.util.List;
007
008 import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
009 import org.apache.lucene.document.Document;
010 import org.apache.lucene.document.Field.Store;
011 import org.apache.lucene.document.NumericDocValuesField;
012 import org.apache.lucene.document.TextField;
013 import org.apache.lucene.expressions.Expression;
014 import org.apache.lucene.expressions.SimpleBindings;
015 import org.apache.lucene.expressions.js.JavascriptCompiler;
016 import org.apache.lucene.facet.index.FacetFields;
017 import org.apache.lucene.facet.params.FacetSearchParams;
018 import org.apache.lucene.facet.search.FacetResult;
019 import org.apache.lucene.facet.search.FacetsCollector;
020 import org.apache.lucene.facet.search.SumValueSourceFacetRequest;
021 import org.apache.lucene.facet.taxonomy.CategoryPath;
022 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
023 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
024 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
025 import org.apache.lucene.index.DirectoryReader;
026 import org.apache.lucene.index.IndexWriter;
027 import org.apache.lucene.index.IndexWriterConfig;
028 import org.apache.lucene.search.IndexSearcher;
029 import org.apache.lucene.search.MatchAllDocsQuery;
030 import org.apache.lucene.search.SortField;
031 import org.apache.lucene.store.Directory;
032 import org.apache.lucene.store.RAMDirectory;
033
034 /*
035 * Licensed to the Apache Software Foundation (ASF) under one or more
036 * contributor license agreements. See the NOTICE file distributed with
037 * this work for additional information regarding copyright ownership.
038 * The ASF licenses this file to You under the Apache License, Version 2.0
039 * (the "License"); you may not use this file except in compliance with
040 * the License. You may obtain a copy of the License at
041 *
042 * http://www.apache.org/licenses/LICENSE-2.0
043 *
044 * Unless required by applicable law or agreed to in writing, software
045 * distributed under the License is distributed on an "AS IS" BASIS,
046 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
047 * See the License for the specific language governing permissions and
048 * limitations under the License.
049 */
050
051 /** Shows facets aggregation by an expression. */
052 public class ExpressionAggregationFacetsExample {
053
054 private final Directory indexDir = new RAMDirectory();
055 private final Directory taxoDir = new RAMDirectory();
056
057 /** Empty constructor */
058 public ExpressionAggregationFacetsExample() {}
059
060 private void add(IndexWriter indexWriter, FacetFields facetFields, String text, String category, long popularity) throws IOException {
061 Document doc = new Document();
062 doc.add(new TextField("c", text, Store.NO));
063 doc.add(new NumericDocValuesField("popularity", popularity));
064 facetFields.addFields(doc, Collections.singletonList(new CategoryPath(category, '/')));
065 indexWriter.addDocument(doc);
066 }
067
068 /** Build the example index. */
069 private void index() throws IOException {
070 IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
071 new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
072
073 // Writes facet ords to a separate directory from the main index
074 DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
075
076 // Reused across documents, to add the necessary facet fields
077 FacetFields facetFields = new FacetFields(taxoWriter);
078
079 add(indexWriter, facetFields, "foo bar", "A/B", 5L);
080 add(indexWriter, facetFields, "foo foo bar", "A/C", 3L);
081
082 indexWriter.close();
083 taxoWriter.close();
084 }
085
086 /** User runs a query and aggregates facets. */
087 private List<FacetResult> search() throws IOException, ParseException {
088 DirectoryReader indexReader = DirectoryReader.open(indexDir);
089 IndexSearcher searcher = new IndexSearcher(indexReader);
090 TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
091
092 // Aggregate categories by an expression that combines the document's score
093 // and its popularity field
094 Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)");
095 SimpleBindings bindings = new SimpleBindings();
096 bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document
097 bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field
098
099 FacetSearchParams fsp = new FacetSearchParams(
100 new SumValueSourceFacetRequest(new CategoryPath("A"), 10, expr.getValueSource(bindings), true));
101
102 // Aggregates the facet values
103 FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
104
105 // MatchAllDocsQuery is for "browsing" (counts facets
106 // for all non-deleted docs in the index); normally
107 // you'd use a "normal" query, and use MultiCollector to
108 // wrap collecting the "normal" hits and also facets:
109 searcher.search(new MatchAllDocsQuery(), fc);
110
111 // Retrieve results
112 List<FacetResult> facetResults = fc.getFacetResults();
113
114 indexReader.close();
115 taxoReader.close();
116
117 return facetResults;
118 }
119
120 /** Runs the search example. */
121 public List<FacetResult> runSearch() throws IOException, ParseException {
122 index();
123 return search();
124 }
125
126 /** Runs the search and drill-down examples and prints the results. */
127 public static void main(String[] args) throws Exception {
128 System.out.println("Facet counting example:");
129 System.out.println("-----------------------");
130 List<FacetResult> results = new ExpressionAggregationFacetsExample().runSearch();
131 for (FacetResult res : results) {
132 System.out.println(res);
133 }
134 }
135
136 }