Skip to content

Commit

Permalink
Elasticsearch: Sort results by index order as well as @timestamp (#29761
Browse files Browse the repository at this point in the history
)

* Elasticsearch - sort results by index order as well as timestamp.

* Fix test.
  • Loading branch information
STEELBADGE committed Jan 14, 2021
1 parent e1f213f commit 1e26164
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 8 additions & 1 deletion public/app/plugins/datasource/elasticsearch/query_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ export class ElasticQueryBuilder {
documentQuery(query: any, size: number) {
query.size = size;
query.sort = {};
query.sort[this.timeField] = { order: 'desc', unmapped_type: 'boolean' };
query.sort = [
{
[this.timeField]: { order: 'desc', unmapped_type: 'boolean' },
},
{
_doc: { order: 'desc' },
},
];

// fields field not supported on ES 5.x
if (this.esVersion < 5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ describe('ElasticQueryBuilder', () => {
],
},
},
sort: {
'@timestamp': {
order: 'desc',
unmapped_type: 'boolean',
},
},
sort: [{ '@timestamp': { order: 'desc', unmapped_type: 'boolean' } }, { _doc: { order: 'desc' } }],
script_fields: {},
});
});
Expand Down Expand Up @@ -552,7 +547,10 @@ describe('ElasticQueryBuilder', () => {
};
expect(query.query).toEqual(expectedQuery);

expect(query.sort).toEqual({ '@timestamp': { order: 'desc', unmapped_type: 'boolean' } });
expect(query.sort).toEqual([
{ '@timestamp': { order: 'desc', unmapped_type: 'boolean' } },
{ _doc: { order: 'desc' } },
]);

const expectedAggs = {
2: {
Expand Down

0 comments on commit 1e26164

Please sign in to comment.