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