Skip to content

Commit

Permalink
HSEARCH-4173 Assume no value in the Elasticsearch geo_distance sort i…
Browse files Browse the repository at this point in the history
…f the target field doesn't exist in one of the targeted indexes

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere committed Mar 2, 2021
1 parent d82ddf0 commit 13e1f7f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
Expand Up @@ -28,4 +28,12 @@ public void requestDocValues(JsonObject requestBody, JsonPrimitive fieldName) {
// So we just don't specify a format.
DOCVALUE_FIELDS_ACCESSOR.addElementIfAbsent( requestBody, fieldName );
}

@Override
public void requestGeoDistanceSortIgnoreUnmapped(JsonObject innerObject) {
// Support for ignore_unmapped in geo_distance sorts added in 6.4:
// https://github.com/elastic/elasticsearch/pull/31153
// In 6.3 and below, we just can't ignore unmapped fields,
// which means sorts will fail when the geo_point field is not present in all indexes.
}
}
Expand Up @@ -26,6 +26,8 @@ public class Elasticsearch7SearchSyntax implements ElasticsearchSearchSyntax {
private static final JsonAccessor<JsonElement> NESTED_ACCESSOR = JsonAccessor.root().property( "nested" );
private static final JsonAccessor<JsonElement> PATH_ACCESSOR = JsonAccessor.root().property( "path" );
private static final JsonAccessor<JsonElement> FILTER_ACCESSOR = JsonAccessor.root().property( "filter" );
private static final JsonAccessor<Boolean> IGNORE_UNMAPPED_ACCESSOR =
JsonAccessor.root().property( "ignore_unmapped" ).asBoolean();

@Override
public String getTermAggregationOrderByTermToken() {
Expand Down Expand Up @@ -55,4 +57,9 @@ public void requestNestedSort(List<String> nestedPathHierarchy, JsonObject inner
nextNestedObjectTarget = nestedObject;
}
}

@Override
public void requestGeoDistanceSortIgnoreUnmapped(JsonObject innerObject) {
IGNORE_UNMAPPED_ACCESSOR.set( innerObject, true );
}
}
Expand Up @@ -19,4 +19,6 @@ public interface ElasticsearchSearchSyntax {

void requestNestedSort(List<String> nestedPathHierarchy, JsonObject innerObject, JsonObject filterOrNull);

void requestGeoDistanceSortIgnoreUnmapped(JsonObject innerObject);

}
Expand Up @@ -40,7 +40,7 @@ abstract class AbstractElasticsearchDocumentValueSort extends AbstractElasticsea

protected final String absoluteFieldPath;
protected final List<String> nestedPathHierarchy;
private final ElasticsearchSearchSyntax searchSyntax;
protected final ElasticsearchSearchSyntax searchSyntax;

private final JsonPrimitive mode;
private final ElasticsearchSearchPredicate filter;
Expand Down
Expand Up @@ -41,6 +41,11 @@ private ElasticsearchDistanceSort(Builder builder) {
@Override
protected void doToJsonSorts(ElasticsearchSearchSortCollector collector, JsonObject innerObject) {
innerObject.add( absoluteFieldPath, ElasticsearchGeoPointFieldCodec.INSTANCE.encode( center ) );
if ( indexNames.size() > 1 ) {
// There are multiple target indexes; some of them may not declare the field.
// Instruct ES to behave as if the field had no value in that case.
searchSyntax.requestGeoDistanceSortIgnoreUnmapped( innerObject );
}

JsonObject outerObject = new JsonObject();
GEO_DISTANCE_ACCESSOR.add( outerObject, innerObject );
Expand Down

0 comments on commit 13e1f7f

Please sign in to comment.