Skip to content

Commit

Permalink
[data.search.aggs] Use fields instead of _source in top_hits agg
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Aug 20, 2021
1 parent 913d178 commit 950ae9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/plugins/data/common/search/aggs/metrics/top_hit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,28 @@ describe('Top hit metric', () => {
});

it('should request the _source field', () => {
init({ field: '_source' });
expect(aggDsl.top_hits._source).toBeTruthy();
expect(aggDsl.top_hits.docvalue_fields).toBeUndefined();
init({ fieldName: '_source' });
expect(aggDsl.top_hits._source).toBe(true);
expect(aggDsl.top_hits.fields).toBeUndefined();
});

it('requests both source and docvalues_fields for non-text aggregatable fields', () => {
it('requests fields for non-text aggregatable fields', () => {
init({ fieldName: 'bytes', readFromDocValues: true });
expect(aggDsl.top_hits._source).toBe('bytes');
expect(aggDsl.top_hits.docvalue_fields).toEqual([{ field: 'bytes' }]);
expect(aggDsl.top_hits._source).toBe(false);
expect(aggDsl.top_hits.fields).toEqual([{ field: 'bytes' }]);
});

it('requests both source and docvalues_fields for date aggregatable fields', () => {
it('requests fields for date aggregatable fields', () => {
init({ fieldName: '@timestamp', readFromDocValues: true, fieldType: KBN_FIELD_TYPES.DATE });

expect(aggDsl.top_hits._source).toBe('@timestamp');
expect(aggDsl.top_hits.docvalue_fields).toEqual([{ field: '@timestamp', format: 'date_time' }]);
expect(aggDsl.top_hits._source).toBe(false);
expect(aggDsl.top_hits.fields).toEqual([{ field: '@timestamp', format: 'date_time' }]);
});

it('requests just source for aggregatable text fields', () => {
it('requests fields for aggregatable text fields', () => {
init({ fieldName: 'machine.os' });
expect(aggDsl.top_hits._source).toBe('machine.os');
expect(aggDsl.top_hits.docvalue_fields).toBeUndefined();
expect(aggDsl.top_hits._source).toBe(false);
expect(aggDsl.top_hits.fields).toEqual([{ field: 'machine.os' }]);
});

describe('try to get the value from the top hit', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/common/search/aggs/metrics/top_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export const getTopHitMetricAgg = () => {
},
};
} else {
if (field.readFromDocValues) {
output.params.docvalue_fields = [
if (field.name !== '_source') {
output.params.fields = [
{
field: field.name,
// always format date fields as date_time to avoid
Expand All @@ -89,7 +89,7 @@ export const getTopHitMetricAgg = () => {
},
];
}
output.params._source = field.name === '_source' ? true : field.name;
output.params._source = field.name === '_source';
}
},
},
Expand Down

0 comments on commit 950ae9f

Please sign in to comment.