Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for selecting percolator query candidate matches containi…
…ng range queries. Extracts ranges from range queries on byte, short, integer, long, half_float, scaled_float, float, double, date and ip fields. byte, short, integer and date ranges are normalized to Lucene's LongRange. half_float and float are normalized to Lucene's DoubleRange. When extracting range queries, the QueryAnalyzer computes the width of the range. This width is used to determine what range should be preferred in a conjunction query. The QueryAnalyzer prefers the smaller ranges, because these ranges tend to match with less documents. Closes #21040
- Loading branch information
Showing
8 changed files
with
971 additions
and
173 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
core/src/main/java/org/apache/lucene/document/BinaryRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.search.Query; | ||
import org.apache.lucene.util.BytesRef; | ||
|
||
/** | ||
* A range field for binary encoded ranges | ||
*/ | ||
public final class BinaryRange extends Field { | ||
/** The number of bytes per dimension, use {@link InetAddressPoint#BYTES} as max, because that is maximum we need to support */ | ||
public static final int BYTES = InetAddressPoint.BYTES; | ||
|
||
private static final FieldType TYPE; | ||
static { | ||
TYPE = new FieldType(); | ||
TYPE.setDimensions(2, BYTES); | ||
TYPE.freeze(); | ||
} | ||
|
||
/** | ||
* Create a new BinaryRange from a provided encoded binary range | ||
* @param name field name. must not be null. | ||
* @param encodedRange Encoded range | ||
*/ | ||
public BinaryRange(String name, byte[] encodedRange) { | ||
super(name, TYPE); | ||
if (encodedRange.length != BYTES * 2) { | ||
throw new IllegalArgumentException("Unexpected encoded range length [" + encodedRange.length + "]"); | ||
} | ||
fieldsData = new BytesRef(encodedRange); | ||
} | ||
|
||
/** | ||
* Create a query for matching indexed ip ranges that {@code INTERSECT} the defined range. | ||
* @param field field name. must not be null. | ||
* @param encodedRange Encoded range | ||
* @return query for matching intersecting encoded ranges (overlap, within, crosses, or contains) | ||
* @throws IllegalArgumentException if {@code field} is null, {@code min} or {@code max} is invalid | ||
*/ | ||
public static Query newIntersectsQuery(String field, byte[] encodedRange) { | ||
return newRelationQuery(field, encodedRange, RangeFieldQuery.QueryType.INTERSECTS); | ||
} | ||
|
||
static Query newRelationQuery(String field, byte[] encodedRange, RangeFieldQuery.QueryType relation) { | ||
return new RangeFieldQuery(field, encodedRange, 1, relation) { | ||
@Override | ||
protected String toString(byte[] ranges, int dimension) { | ||
return "[" + new BytesRef(ranges, 0, BYTES) + " TO " + new BytesRef(ranges, BYTES, BYTES) + "]"; | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 111 additions & 43 deletions
154
modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.