-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search service] Shim total hits in async search response (#61565)
* Shim total hits in async search response * Resolve types * Fix types * Fix tests Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
5d8c65a
commit c98c225
Showing
3 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
x-pack/plugins/data_enhanced/server/search/shim_hits_total.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { shimHitsTotal } from './shim_hits_total'; | ||
|
||
describe('shimHitsTotal', () => { | ||
test('returns the total if it is already numeric', () => { | ||
const result = shimHitsTotal({ | ||
hits: { | ||
total: 5, | ||
}, | ||
} as any); | ||
expect(result).toEqual({ | ||
hits: { | ||
total: 5, | ||
}, | ||
}); | ||
}); | ||
|
||
test('returns the total if it is inside `value`', () => { | ||
const result = shimHitsTotal({ | ||
hits: { | ||
total: { | ||
value: 5, | ||
}, | ||
}, | ||
} as any); | ||
expect(result).toEqual({ | ||
hits: { | ||
total: 5, | ||
}, | ||
}); | ||
}); | ||
|
||
test('returns other properties from the response', () => { | ||
const result = shimHitsTotal({ | ||
_shards: {}, | ||
hits: { | ||
hits: [], | ||
total: { | ||
value: 5, | ||
}, | ||
}, | ||
} as any); | ||
expect(result).toEqual({ | ||
_shards: {}, | ||
hits: { | ||
hits: [], | ||
total: 5, | ||
}, | ||
}); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
x-pack/plugins/data_enhanced/server/search/shim_hits_total.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { SearchResponse } from 'elasticsearch'; | ||
|
||
/** | ||
* Temporary workaround until https://github.com/elastic/kibana/issues/26356 is addressed. | ||
* Since we are setting `track_total_hits` in the request, `hits.total` will be an object | ||
* containing the `value`. | ||
*/ | ||
export function shimHitsTotal(response: SearchResponse<any>) { | ||
const total = (response.hits?.total as any)?.value ?? response.hits?.total; | ||
const hits = { ...response.hits, total }; | ||
return { ...response, hits }; | ||
} |