Make sure that field collapsing supports field aliases. (#32648)

This commit is contained in:
Julie Tibshirani 2018-08-07 16:20:09 -07:00 committed by GitHub
parent 8bfb0f3f8d
commit d7183f8f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 10 deletions

View File

@ -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}

View File

@ -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);
}
}

View File

@ -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<InnerHitBuilder> innerHits;
public CollapseContext(MappedFieldType fieldType, InnerHitBuilder innerHit) {
this.fieldType = fieldType;
this.innerHits = Collections.singletonList(innerHit);
}
public CollapseContext(MappedFieldType fieldType, List<InnerHitBuilder> innerHits) {
public CollapseContext(String fieldName,
MappedFieldType fieldType,
List<InnerHitBuilder> 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;

View File

@ -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))));