shaie commented on code in PR #841:
URL: https://github.com/apache/lucene/pull/841#discussion_r903277452


##########
lucene/demo/src/java/org/apache/lucene/demo/facet/CustomFacetSetExample.java:
##########
@@ -0,0 +1,303 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.demo.facet;
+
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.ZoneOffset;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
+import org.apache.lucene.document.*;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.FacetsCollectorManager;
+import org.apache.lucene.facet.facetset.*;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.IndexWriterConfig.OpenMode;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.store.ByteBuffersDirectory;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.NumericUtils;
+
+/**
+ * Shows usage of indexing and searching {@link FacetSetsField} with a custom 
{@link FacetSet}
+ * implementation. Unlike the out of the box {@link FacetSet} implementations, 
this example shows
+ * how to mix and match dimensions of different types, as well as implementing 
a custom {@link
+ * FacetSetMatcher}.
+ */
+public class CustomFacetSetExample {
+
+  private static final long MAY_SECOND_2022 = date("2022-05-02");
+  private static final long JUNE_SECOND_2022 = date("2022-06-02");
+  private static final long JULY_SECOND_2022 = date("2022-07-02");
+  private static final float HUNDRED_TWENTY_DEGREES = fahrenheitToCelsius(120);
+  private static final float HUNDRED_DEGREES = fahrenheitToCelsius(100);
+  private static final float EIGHTY_DEGREES = fahrenheitToCelsius(80);
+
+  private final Directory indexDir = new ByteBuffersDirectory();
+
+  /** Empty constructor */
+  public CustomFacetSetExample() {}
+
+  /** Build the example index. */
+  private void index() throws IOException {
+    IndexWriter indexWriter =
+        new IndexWriter(
+            indexDir, new IndexWriterConfig(new 
WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
+
+    // Every document holds the temperature measures for a City by Date
+
+    Document doc = new Document();
+    doc.add(new StringField("city", "city1", Field.Store.YES));
+    doc.add(
+        FacetSetsField.create(
+            "temperature",
+            new TemperatureReadingFacetSet(MAY_SECOND_2022, HUNDRED_DEGREES),
+            new TemperatureReadingFacetSet(JUNE_SECOND_2022, EIGHTY_DEGREES),
+            new TemperatureReadingFacetSet(JULY_SECOND_2022, 
HUNDRED_TWENTY_DEGREES)));
+    indexWriter.addDocument(doc);
+
+    doc = new Document();
+    doc.add(new StringField("city", "city2", Field.Store.YES));
+    doc.add(
+        FacetSetsField.create(
+            "temperature",
+            new TemperatureReadingFacetSet(MAY_SECOND_2022, EIGHTY_DEGREES),
+            new TemperatureReadingFacetSet(JUNE_SECOND_2022, HUNDRED_DEGREES),
+            new TemperatureReadingFacetSet(JULY_SECOND_2022, 
HUNDRED_TWENTY_DEGREES)));
+    indexWriter.addDocument(doc);
+
+    indexWriter.close();
+  }
+
+  /** Counting documents which exactly match a given {@link FacetSet}. */
+  private List<FacetResult> exactMatching() throws IOException {
+    DirectoryReader indexReader = DirectoryReader.open(indexDir);
+    IndexSearcher searcher = new IndexSearcher(indexReader);
+
+    // MatchAllDocsQuery is for "browsing" (counts facets
+    // for all non-deleted docs in the index); normally
+    // you'd use a "normal" query:
+    FacetsCollector fc = searcher.search(new MatchAllDocsQuery(), new 
FacetsCollectorManager());
+
+    // Count both "Publish Date" and "Author" dimensions

Review Comment:
   Indeed :), I copied the simple faceting example and didn't cover up my 
tracks very well :D.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to