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.Closeable;
020import java.io.IOException;
021import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
022import org.apache.lucene.document.Document;
023import org.apache.lucene.document.LongPoint;
024import org.apache.lucene.document.NumericDocValuesField;
025import org.apache.lucene.facet.DrillDownQuery;
026import org.apache.lucene.facet.DrillSideways;
027import org.apache.lucene.facet.FacetResult;
028import org.apache.lucene.facet.Facets;
029import org.apache.lucene.facet.FacetsCollector;
030import org.apache.lucene.facet.FacetsConfig;
031import org.apache.lucene.facet.range.LongRange;
032import org.apache.lucene.facet.range.LongRangeFacetCounts;
033import org.apache.lucene.index.DirectoryReader;
034import org.apache.lucene.index.IndexWriter;
035import org.apache.lucene.index.IndexWriterConfig;
036import org.apache.lucene.index.IndexWriterConfig.OpenMode;
037import org.apache.lucene.search.IndexSearcher;
038import org.apache.lucene.search.MatchAllDocsQuery;
039import org.apache.lucene.search.TopDocs;
040import org.apache.lucene.store.ByteBuffersDirectory;
041import org.apache.lucene.store.Directory;
042import org.apache.lucene.util.IOUtils;
043
044/** Shows simple usage of dynamic range faceting. */
045public class RangeFacetsExample implements Closeable {
046
047  private final Directory indexDir = new ByteBuffersDirectory();
048  private IndexSearcher searcher;
049  private final long nowSec = System.currentTimeMillis() / 1000L;
050
051  final LongRange PAST_HOUR = new LongRange("Past hour", nowSec - 3600, true, nowSec, true);
052  final LongRange PAST_SIX_HOURS =
053      new LongRange("Past six hours", nowSec - 6 * 3600, true, nowSec, true);
054  final LongRange PAST_DAY = new LongRange("Past day", nowSec - 24 * 3600, true, nowSec, true);
055
056  /** Empty constructor */
057  public RangeFacetsExample() {}
058
059  /** Build the example index. */
060  public void index() throws IOException {
061    IndexWriter indexWriter =
062        new IndexWriter(
063            indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
064
065    // Add documents with a fake timestamp, 1000 sec before
066    // "now", 2000 sec before "now", ...:
067    for (int i = 0; i < 100; i++) {
068      Document doc = new Document();
069      long then = nowSec - i * 1000L;
070      // Add as doc values field, so we can compute range facets:
071      doc.add(new NumericDocValuesField("timestamp", then));
072      // Add as numeric field so we can drill-down:
073      doc.add(new LongPoint("timestamp", then));
074      indexWriter.addDocument(doc);
075    }
076
077    // Open near-real-time searcher
078    searcher = new IndexSearcher(DirectoryReader.open(indexWriter));
079    indexWriter.close();
080  }
081
082  private FacetsConfig getConfig() {
083    return new FacetsConfig();
084  }
085
086  /** User runs a query and counts facets. */
087  public FacetResult search() throws IOException {
088
089    // Aggregates the facet counts
090    FacetsCollector fc = new FacetsCollector();
091
092    // MatchAllDocsQuery is for "browsing" (counts facets
093    // for all non-deleted docs in the index); normally
094    // you'd use a "normal" query:
095    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
096
097    Facets facets = new LongRangeFacetCounts("timestamp", fc, PAST_HOUR, PAST_SIX_HOURS, PAST_DAY);
098    return facets.getTopChildren(10, "timestamp");
099  }
100
101  /** User drills down on the specified range. */
102  public TopDocs drillDown(LongRange range) throws IOException {
103
104    // Passing no baseQuery means we drill down on all
105    // documents ("browse only"):
106    DrillDownQuery q = new DrillDownQuery(getConfig());
107
108    q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));
109    return searcher.search(q, 10);
110  }
111
112  /** User drills down on the specified range, and also computes drill sideways counts. */
113  public DrillSideways.DrillSidewaysResult drillSideways(LongRange range) throws IOException {
114    // Passing no baseQuery means we drill down on all
115    // documents ("browse only"):
116    DrillDownQuery q = new DrillDownQuery(getConfig());
117    q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));
118
119    // DrillSideways only handles taxonomy and sorted set drill facets by default; to do range
120    // facets we must subclass and override the
121    // buildFacetsResult method.
122    DrillSideways.DrillSidewaysResult result =
123        new DrillSideways(searcher, getConfig(), null, null) {
124          @Override
125          protected Facets buildFacetsResult(
126              FacetsCollector drillDowns,
127              FacetsCollector[] drillSideways,
128              String[] drillSidewaysDims)
129              throws IOException {
130            // If we had other dims we would also compute their drill-down or drill-sideways facets
131            // here:
132            assert drillSidewaysDims[0].equals("timestamp");
133            return new LongRangeFacetCounts(
134                "timestamp", drillSideways[0], PAST_HOUR, PAST_SIX_HOURS, PAST_DAY);
135          }
136        }.search(q, 10);
137
138    return result;
139  }
140
141  @Override
142  public void close() throws IOException {
143    IOUtils.close(searcher.getIndexReader(), indexDir);
144  }
145
146  /** Runs the search and drill-down examples and prints the results. */
147  public static void main(String[] args) throws Exception {
148    RangeFacetsExample example = new RangeFacetsExample();
149    example.index();
150
151    System.out.println("Facet counting example:");
152    System.out.println("-----------------------");
153    System.out.println(example.search());
154
155    System.out.println("\n");
156    System.out.println("Facet drill-down example (timestamp/Past six hours):");
157    System.out.println("---------------------------------------------");
158    TopDocs hits = example.drillDown(example.PAST_SIX_HOURS);
159    System.out.println(hits.totalHits + " totalHits");
160
161    System.out.println("\n");
162    System.out.println("Facet drill-sideways example (timestamp/Past six hours):");
163    System.out.println("---------------------------------------------");
164    DrillSideways.DrillSidewaysResult sideways = example.drillSideways(example.PAST_SIX_HOURS);
165    System.out.println(sideways.hits.totalHits + " totalHits");
166    System.out.println(sideways.facets.getTopChildren(10, "timestamp"));
167
168    example.close();
169  }
170}