You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2022/12/09 20:00:54 UTC

[GitHub] [lucene] mdmarshmallow commented on a diff in pull request #11901: Github#11869: Add RangeOnRangeFacetCounts

mdmarshmallow commented on code in PR #11901:
URL: https://github.com/apache/lucene/pull/11901#discussion_r1044690705


##########
lucene/core/src/java/org/apache/lucene/document/LongRangeDocValuesFacetField.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.document;
+
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * A subclass of LongRangeDocValuesField that only allows single dimension range representation.
+ * Meant to be used with RangeOnRange faceting
+ */
+public class LongRangeDocValuesFacetField extends LongRangeDocValuesField {

Review Comment:
   Sounds good, I will just modify `LongRangeDocValuesField` and `DoubleRangeDocValuesField`.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/LongRangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.facet.rangeonrange;
+
+import static org.apache.lucene.document.LongRange.verifyAndEncode;
+
+import java.io.IOException;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.search.Query;
+
+/** Represents counts for long range on range faceting */
+public class LongRangeOnRangeFacetCounts extends RangeOnRangeFacetCounts {
+
+  /**
+   * Represents counts for long range on range faceting (inclusive)
+   *
+   * @param field document's field
+   * @param hits hits we want the counts of
+   * @param queryType type of intersection we want to count
+   * @param ranges ranges we want the counts of
+   * @throws IOException low level exception
+   */
+  public LongRangeOnRangeFacetCounts(
+      String field, FacetsCollector hits, RangeFieldQuery.QueryType queryType, LongRange... ranges)
+      throws IOException {
+    super(field, hits, queryType, null, ranges);
+  }
+
+  /**
+   * Represents counts for long range on range faceting
+   *
+   * @param field document's field
+   * @param hits hits we want the counts of
+   * @param queryType type of intersection we want to count
+   * @param fastMatchQuery query to quickly discard hits
+   * @param ranges ranges we want the counts of
+   * @throws IOException low level exception
+   */
+  public LongRangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      LongRange... ranges)
+      throws IOException {
+    super(field, hits, queryType, fastMatchQuery, ranges);
+  }
+
+  @Override
+  public byte[] getEncodedRange(Range range) {

Review Comment:
   Hmm, yeah I could do that, I think the only concern is that I didn't want to pass in `ranges` twice, but I guess there's no real reason to avoiding that. I changed it.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;

Review Comment:
   Yeah we don't need this array. Removed in next revision. I'll also remove some other "useless" params.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+  private final int numRanges;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      Range... ranges)
+      throws IOException {
+    super(fastMatchQuery);
+    this.field = field;
+    this.ranges = ranges;
+    this.numEncodedValueBytes = ranges[0].getEncodedValueBytes();
+    this.dims = ranges[0].dims;
+    this.labels = getLabels(ranges);
+    this.numRanges = ranges.length;
+    this.queryType = queryType;
+    counts = new int[numRanges];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> matchingDocs)
+      throws IOException {
+
+    int missingCount = 0;
+
+    for (int i = 0; i < matchingDocs.size(); i++) {
+
+      FacetsCollector.MatchingDocs hits = matchingDocs.get(i);
+      BinaryRangeDocValues binaryRangeDocValues =
+          new BinaryRangeDocValues(
+              DocValues.getBinary(hits.context.reader(), field), dims, numEncodedValueBytes);
+
+      final DocIdSetIterator it = createIterator(hits);
+      if (it == null) {
+        continue;
+      }
+
+      totCount += hits.totalHits;
+      for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
+        if (binaryRangeDocValues.advanceExact(doc)) {
+          boolean hasValidRange = false;
+          for (int range = 0; range < numRanges; range++) {
+            byte[] encodedRange = getEncodedRange(ranges[range]);
+            byte[] packedRange = binaryRangeDocValues.getPackedValue();
+            assert encodedRange.length == packedRange.length;
+            boolean matchesRange = true;
+            for (int dim = 0; dim < dims; dim++) {
+              if (queryType.matches(encodedRange, packedRange, dims, numEncodedValueBytes, dim)
+                  == false) {
+                matchesRange = false;
+                break;
+              }
+            }
+            if (matchesRange) {
+              counts[range]++;
+              hasValidRange = true;
+            }
+          }
+          if (hasValidRange == false) {
+            missingCount++;
+          }
+        } else {
+          missingCount++;
+        }
+        doc = it.nextDoc();
+      }
+    }
+    totCount -= missingCount;
+  }
+
+  @Override
+  public FacetResult getAllChildren(String dim, String... path) throws IOException {

Review Comment:
   I just used the same java doc that `RangeFacetCounts` uses for this method. Hopefully that works. 



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+  private final int numRanges;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      Range... ranges)
+      throws IOException {
+    super(fastMatchQuery);
+    this.field = field;
+    this.ranges = ranges;
+    this.numEncodedValueBytes = ranges[0].getEncodedValueBytes();
+    this.dims = ranges[0].dims;
+    this.labels = getLabels(ranges);
+    this.numRanges = ranges.length;
+    this.queryType = queryType;
+    counts = new int[numRanges];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> matchingDocs)

Review Comment:
   Added in next revision.



##########
lucene/core/src/java/org/apache/lucene/document/LongRange.java:
##########
@@ -61,6 +61,17 @@ public LongRange(String name, final long[] min, final long[] max) {
     setRangeValues(min, max);
   }
 
+  /**
+   * Create a new LongRange type with one dimension
+   *
+   * @param name field name. must not be null.
+   * @param min min value
+   * @param max max value
+   */
+  public LongRange(String name, final long min, final long max) {

Review Comment:
   Makes sense, I'll remove these then



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+  private final int numRanges;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      Range... ranges)
+      throws IOException {
+    super(fastMatchQuery);
+    this.field = field;
+    this.ranges = ranges;
+    this.numEncodedValueBytes = ranges[0].getEncodedValueBytes();
+    this.dims = ranges[0].dims;
+    this.labels = getLabels(ranges);
+    this.numRanges = ranges.length;
+    this.queryType = queryType;
+    counts = new int[numRanges];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> matchingDocs)
+      throws IOException {
+
+    int missingCount = 0;
+
+    for (int i = 0; i < matchingDocs.size(); i++) {
+
+      FacetsCollector.MatchingDocs hits = matchingDocs.get(i);
+      BinaryRangeDocValues binaryRangeDocValues =
+          new BinaryRangeDocValues(
+              DocValues.getBinary(hits.context.reader(), field), dims, numEncodedValueBytes);
+
+      final DocIdSetIterator it = createIterator(hits);
+      if (it == null) {
+        continue;
+      }
+
+      totCount += hits.totalHits;
+      for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
+        if (binaryRangeDocValues.advanceExact(doc)) {
+          boolean hasValidRange = false;
+          for (int range = 0; range < numRanges; range++) {
+            byte[] encodedRange = getEncodedRange(ranges[range]);
+            byte[] packedRange = binaryRangeDocValues.getPackedValue();
+            assert encodedRange.length == packedRange.length;
+            boolean matchesRange = true;
+            for (int dim = 0; dim < dims; dim++) {
+              if (queryType.matches(encodedRange, packedRange, dims, numEncodedValueBytes, dim)

Review Comment:
   Ah yeah, forgot about that one, thanks for catching that! Changed in the next revision.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+  private final int numRanges;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      Range... ranges)
+      throws IOException {
+    super(fastMatchQuery);
+    this.field = field;
+    this.ranges = ranges;
+    this.numEncodedValueBytes = ranges[0].getEncodedValueBytes();
+    this.dims = ranges[0].dims;
+    this.labels = getLabels(ranges);
+    this.numRanges = ranges.length;
+    this.queryType = queryType;
+    counts = new int[numRanges];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> matchingDocs)
+      throws IOException {
+
+    int missingCount = 0;
+
+    for (int i = 0; i < matchingDocs.size(); i++) {
+
+      FacetsCollector.MatchingDocs hits = matchingDocs.get(i);
+      BinaryRangeDocValues binaryRangeDocValues =
+          new BinaryRangeDocValues(
+              DocValues.getBinary(hits.context.reader(), field), dims, numEncodedValueBytes);
+
+      final DocIdSetIterator it = createIterator(hits);
+      if (it == null) {
+        continue;
+      }
+
+      totCount += hits.totalHits;
+      for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
+        if (binaryRangeDocValues.advanceExact(doc)) {
+          boolean hasValidRange = false;
+          for (int range = 0; range < numRanges; range++) {
+            byte[] encodedRange = getEncodedRange(ranges[range]);

Review Comment:
   Addressed this in your other comment.



-- 
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-unsubscribe@lucene.apache.org

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


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