Skip to content

Commit

Permalink
Add support for selecting percolator query candidate matches containi…
Browse files Browse the repository at this point in the history
…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
martijnvg committed Jul 27, 2017
1 parent af86291 commit 23f09d9
Show file tree
Hide file tree
Showing 8 changed files with 971 additions and 173 deletions.
71 changes: 71 additions & 0 deletions core/src/main/java/org/apache/lucene/document/BinaryRange.java
@@ -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) + "]";
}
};
}

}
Expand Up @@ -44,7 +44,7 @@
final class PercolateQuery extends Query implements Accountable {

// cost of matching the query against the document, arbitrary as it would be really complex to estimate
public static final float MATCH_COST = 1000;
private static final float MATCH_COST = 1000;

private final QueryStore queryStore;
private final BytesReference documentSource;
Expand Down Expand Up @@ -164,15 +164,15 @@ boolean matchDocId(int docId) throws IOException {
};
}

public IndexSearcher getPercolatorIndexSearcher() {
IndexSearcher getPercolatorIndexSearcher() {
return percolatorIndexSearcher;
}

public BytesReference getDocumentSource() {
BytesReference getDocumentSource() {
return documentSource;
}

public QueryStore getQueryStore() {
QueryStore getQueryStore() {
return queryStore;
}

Expand Down

Large diffs are not rendered by default.

Expand Up @@ -50,7 +50,7 @@
* Highlighting in the case of the percolate query is a bit different, because the PercolateQuery itself doesn't get highlighted,
* but the source of the PercolateQuery gets highlighted by each hit containing a query.
*/
public final class PercolatorHighlightSubFetchPhase extends HighlightPhase {
final class PercolatorHighlightSubFetchPhase extends HighlightPhase {

PercolatorHighlightSubFetchPhase(Settings settings, Map<String, Highlighter> highlighters) {
super(settings, highlighters);
Expand Down

0 comments on commit 23f09d9

Please sign in to comment.