Add _id to the result of helpers.search (#2432)
This commit is contained in:
@ -28,6 +28,7 @@ import { errors, TransportResult, TransportRequestOptions, TransportRequestOptio
|
||||
import { Table, TypeMap, tableFromIPC, RecordBatchStreamReader } from 'apache-arrow/Arrow.node'
|
||||
import Client from './client'
|
||||
import * as T from './api/types'
|
||||
import { Id } from './api/types'
|
||||
|
||||
export interface HelpersOptions {
|
||||
client: Client
|
||||
@ -193,12 +194,18 @@ export default class Helpers {
|
||||
* @param {object} options - The client optional configuration for this request.
|
||||
* @return {array} The documents that matched the request.
|
||||
*/
|
||||
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<TDocument[]> {
|
||||
appendFilterPath('hits.hits._source', params, true)
|
||||
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<Array<TDocument & {_id: Id}>> {
|
||||
appendFilterPath('hits.hits._id,hits.hits._source', params, true)
|
||||
options.meta = true
|
||||
const { body: result } = await this[kClient].search<TDocument>(params, options as TransportRequestOptionsWithMeta)
|
||||
if (result.hits?.hits != null) {
|
||||
return result.hits.hits.map(d => d._source as TDocument)
|
||||
return result.hits.hits.map(d => ({
|
||||
// Starting with version 8.14.0, _id is optional, but in our case it's always present.
|
||||
// See @es_quirk documentation in elasticsearch-specification/specification/_global/search/_types/hits.ts
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
_id: d._id!,
|
||||
...(d._source as TDocument)
|
||||
}))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
@ -24,14 +24,14 @@ import { connection } from '../../utils'
|
||||
test('Search should have an additional documents property', async t => {
|
||||
const MockConnection = connection.buildMockConnection({
|
||||
onRequest (params) {
|
||||
t.equal(params.querystring, 'filter_path=hits.hits._source')
|
||||
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
|
||||
return {
|
||||
body: {
|
||||
hits: {
|
||||
hits: [
|
||||
{ _source: { one: 'one' } },
|
||||
{ _source: { two: 'two' } },
|
||||
{ _source: { three: 'three' } }
|
||||
{ _id: '1', _source: { one: 'one' } },
|
||||
{ _id: '2', _source: { two: 'two' } },
|
||||
{ _id: '3', _source: { three: 'three' } }
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -49,16 +49,16 @@ test('Search should have an additional documents property', async t => {
|
||||
query: { match_all: {} }
|
||||
})
|
||||
t.same(result, [
|
||||
{ one: 'one' },
|
||||
{ two: 'two' },
|
||||
{ three: 'three' }
|
||||
{ _id: '1', one: 'one' },
|
||||
{ _id: '2', two: 'two' },
|
||||
{ _id: '3', three: 'three' }
|
||||
])
|
||||
})
|
||||
|
||||
test('kGetHits fallback', async t => {
|
||||
const MockConnection = connection.buildMockConnection({
|
||||
onRequest (params) {
|
||||
t.equal(params.querystring, 'filter_path=hits.hits._source')
|
||||
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
|
||||
return { body: {} }
|
||||
}
|
||||
})
|
||||
@ -78,14 +78,14 @@ test('kGetHits fallback', async t => {
|
||||
test('Merge filter paths (snake_case)', async t => {
|
||||
const MockConnection = connection.buildMockConnection({
|
||||
onRequest (params) {
|
||||
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._source')
|
||||
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._id%2Chits.hits._source')
|
||||
return {
|
||||
body: {
|
||||
hits: {
|
||||
hits: [
|
||||
{ _source: { one: 'one' } },
|
||||
{ _source: { two: 'two' } },
|
||||
{ _source: { three: 'three' } }
|
||||
{ _id: '1', _source: { one: 'one' } },
|
||||
{ _id: '2', _source: { two: 'two' } },
|
||||
{ _id: '3', _source: { three: 'three' } }
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -104,9 +104,9 @@ test('Merge filter paths (snake_case)', async t => {
|
||||
query: { match_all: {} }
|
||||
})
|
||||
t.same(result, [
|
||||
{ one: 'one' },
|
||||
{ two: 'two' },
|
||||
{ three: 'three' }
|
||||
{ _id: '1', one: 'one' },
|
||||
{ _id: '2', two: 'two' },
|
||||
{ _id: '3', three: 'three' }
|
||||
])
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user