From d7183f8f3ddaaa921fec1c29cc7093ca59c104e4 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Tue, 7 Aug 2018 16:20:09 -0700 Subject: [PATCH] Make sure that field collapsing supports field aliases. (#32648) --- .../test/search/110_field_collapsing.yml | 31 ++++++++++++++++++- .../search/collapse/CollapseBuilder.java | 2 +- .../search/collapse/CollapseContext.java | 19 +++++++----- .../subphase/DocValueFieldsFetchSubPhase.java | 2 +- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml index 2dfd868d66b..bf74b3bd326 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml @@ -1,7 +1,14 @@ setup: - do: indices.create: - index: test + index: test + body: + mappings: + test: + properties: + numeric_group: { type: integer } + group_alias: { type: alias, path: numeric_group } + - do: index: index: test @@ -341,3 +348,25 @@ setup: - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.0._version: 55 } - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4" } - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._version: 44 } + +--- +"field collapsing on a field alias": + - skip: + version: " - 6.99.99" + reason: The associated bugfix is pending backport to 6.x. + - do: + search: + index: test + body: + collapse: { field: group_alias, inner_hits: { name: sub_hits } } + sort: [{ sort: desc }] + + - match: { hits.total: 6 } + - length: { hits.hits: 3 } + + - match: { hits.hits.0.fields.group_alias: [3] } + - match: { hits.hits.0.inner_hits.sub_hits.hits.total: 1} + - match: { hits.hits.1.fields.group_alias: [1] } + - match: { hits.hits.1.inner_hits.sub_hits.hits.total: 3} + - match: { hits.hits.2.fields.group_alias: [25] } + - match: { hits.hits.2.inner_hits.sub_hits.hits.total: 2} diff --git a/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java b/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java index 90e35c34e28..ccab5e2cb93 100644 --- a/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java @@ -247,6 +247,6 @@ public class CollapseBuilder implements Writeable, ToXContentObject { + field + "`, " + "only indexed field can retrieve `inner_hits`"); } - return new CollapseContext(fieldType, innerHits); + return new CollapseContext(field, fieldType, innerHits); } } diff --git a/server/src/main/java/org/elasticsearch/search/collapse/CollapseContext.java b/server/src/main/java/org/elasticsearch/search/collapse/CollapseContext.java index 95fee901a30..82a7657f180 100644 --- a/server/src/main/java/org/elasticsearch/search/collapse/CollapseContext.java +++ b/server/src/main/java/org/elasticsearch/search/collapse/CollapseContext.java @@ -25,26 +25,31 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.index.query.InnerHitBuilder; -import java.util.Collections; import java.util.List; /** * Context used for field collapsing */ public class CollapseContext { + private final String fieldName; private final MappedFieldType fieldType; private final List innerHits; - public CollapseContext(MappedFieldType fieldType, InnerHitBuilder innerHit) { - this.fieldType = fieldType; - this.innerHits = Collections.singletonList(innerHit); - } - - public CollapseContext(MappedFieldType fieldType, List innerHits) { + public CollapseContext(String fieldName, + MappedFieldType fieldType, + List innerHits) { + this.fieldName = fieldName; this.fieldType = fieldType; this.innerHits = innerHits; } + /** + * The requested field name to collapse on. + */ + public String getFieldName() { + return fieldName; + } + /** The field type used for collapsing **/ public MappedFieldType getFieldType() { return fieldType; diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/DocValueFieldsFetchSubPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/DocValueFieldsFetchSubPhase.java index a1562e118fb..3ef3064697a 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/DocValueFieldsFetchSubPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/DocValueFieldsFetchSubPhase.java @@ -61,7 +61,7 @@ public final class DocValueFieldsFetchSubPhase implements FetchSubPhase { if (context.collapse() != null) { // retrieve the `doc_value` associated with the collapse field - String name = context.collapse().getFieldType().name(); + String name = context.collapse().getFieldName(); if (context.docValueFieldsContext() == null) { context.docValueFieldsContext(new DocValueFieldsContext( Collections.singletonList(new FieldAndFormat(name, DocValueFieldsContext.USE_DEFAULT_FORMAT))));