Skip to content

Commit b83354e

Browse files
committed
Make sure that field collapsing supports field aliases. (#32648)
1 parent c313f11 commit b83354e

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
@@ -387,3 +394,25 @@ setup:
387394
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.0._version: 55 }
388395
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4" }
389396
- match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._version: 44 }
397+
398+
---
399+
"field collapsing on a field alias":
400+
- skip:
401+
version: " - 6.3.99"
402+
reason: Field aliases were introduced in 6.4.0.
403+
- do:
404+
search:
405+
index: test
406+
body:
407+
collapse: { field: group_alias, inner_hits: { name: sub_hits } }
408+
sort: [{ sort: desc }]
409+
410+
- match: { hits.total: 6 }
411+
- length: { hits.hits: 3 }
412+
413+
- match: { hits.hits.0.fields.group_alias: [3] }
414+
- match: { hits.hits.0.inner_hits.sub_hits.hits.total: 1}
415+
- match: { hits.hits.1.fields.group_alias: [1] }
416+
- match: { hits.hits.1.inner_hits.sub_hits.hits.total: 3}
417+
- match: { hits.hits.2.fields.group_alias: [25] }
418+
- 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)