Skip to content

Commit d7183f8

Browse files
authored
Make sure that field collapsing supports field aliases. (#32648)
1 parent 8bfb0f3 commit d7183f8

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
setup:
22
- do:
33
indices.create:
4-
index: test
4+
index: test
5+
body:
6+
mappings:
7+
test:
8+
properties:
9+
numeric_group: { type: integer }
10+
group_alias: { type: alias, path: numeric_group }
11+
512
- do:
613
index:
714
index: test
@@ -341,3 +348,25 @@ setup:
341348
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.0._version: 55 }
342349
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4" }
343350
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._version: 44 }
351+
352+
---
353+
"field collapsing on a field alias":
354+
- skip:
355+
version: " - 6.99.99"
356+
reason: The associated bugfix is pending backport to 6.x.
357+
- do:
358+
search:
359+
index: test
360+
body:
361+
collapse: { field: group_alias, inner_hits: { name: sub_hits } }
362+
sort: [{ sort: desc }]
363+
364+
- match: { hits.total: 6 }
365+
- length: { hits.hits: 3 }
366+
367+
- match: { hits.hits.0.fields.group_alias: [3] }
368+
- match: { hits.hits.0.inner_hits.sub_hits.hits.total: 1}
369+
- match: { hits.hits.1.fields.group_alias: [1] }
370+
- match: { hits.hits.1.inner_hits.sub_hits.hits.total: 3}
371+
- match: { hits.hits.2.fields.group_alias: [25] }
372+
- match: { hits.hits.2.inner_hits.sub_hits.hits.total: 2}

server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,6 @@ public CollapseContext build(SearchContext context) {
247247
+ field + "`, " + "only indexed field can retrieve `inner_hits`");
248248
}
249249

250-
return new CollapseContext(fieldType, innerHits);
250+
return new CollapseContext(field, fieldType, innerHits);
251251
}
252252
}

server/src/main/java/org/elasticsearch/search/collapse/CollapseContext.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,29 @@
2525
import org.elasticsearch.index.mapper.NumberFieldMapper;
2626
import org.elasticsearch.index.query.InnerHitBuilder;
2727

28-
import java.util.Collections;
2928
import java.util.List;
3029

3130
/**
3231
* Context used for field collapsing
3332
*/
3433
public class CollapseContext {
34+
private final String fieldName;
3535
private final MappedFieldType fieldType;
3636
private final List<InnerHitBuilder> innerHits;
3737

38-
public CollapseContext(MappedFieldType fieldType, InnerHitBuilder innerHit) {
38+
public CollapseContext(String fieldName,
39+
MappedFieldType fieldType,
40+
List<InnerHitBuilder> innerHits) {
41+
this.fieldName = fieldName;
3942
this.fieldType = fieldType;
40-
this.innerHits = Collections.singletonList(innerHit);
43+
this.innerHits = innerHits;
4144
}
4245

43-
public CollapseContext(MappedFieldType fieldType, List<InnerHitBuilder> innerHits) {
44-
this.fieldType = fieldType;
45-
this.innerHits = innerHits;
46+
/**
47+
* The requested field name to collapse on.
48+
*/
49+
public String getFieldName() {
50+
return fieldName;
4651
}
4752

4853
/** The field type used for collapsing **/

server/src/main/java/org/elasticsearch/search/fetch/subphase/DocValueFieldsFetchSubPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOExcept
6161

6262
if (context.collapse() != null) {
6363
// retrieve the `doc_value` associated with the collapse field
64-
String name = context.collapse().getFieldType().name();
64+
String name = context.collapse().getFieldName();
6565
if (context.docValueFieldsContext() == null) {
6666
context.docValueFieldsContext(new DocValueFieldsContext(
6767
Collections.singletonList(new FieldAndFormat(name, DocValueFieldsContext.USE_DEFAULT_FORMAT))));

0 commit comments

Comments
 (0)