Skip to content

Commit

Permalink
Make sure that field collapsing supports field aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Aug 7, 2018
1 parent 3d5e911 commit 3321c7a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ public CollapseContext build(SearchContext context) {
+ field + "`, " + "only indexed field can retrieve `inner_hits`");
}

return new CollapseContext(fieldType, innerHits);
return new CollapseContext(field, fieldType, innerHits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,29 @@
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) {
public CollapseContext(String fieldName,
MappedFieldType fieldType,
List<InnerHitBuilder> innerHits) {
this.fieldName = fieldName;
this.fieldType = fieldType;
this.innerHits = Collections.singletonList(innerHit);
this.innerHits = innerHits;
}

public CollapseContext(MappedFieldType fieldType, List<InnerHitBuilder> innerHits) {
this.fieldType = fieldType;
this.innerHits = innerHits;
/**
* The requested field name to collapse on.
*/
public String getFieldName() {
return fieldName;
}

/** The field type used for collapsing **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOExcept

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))));
Expand Down

0 comments on commit 3321c7a

Please sign in to comment.