From 2455dac4e5b1730c97d0fe4266612c88fc11e088 Mon Sep 17 00:00:00 2001 From: Rami <72725910+ramikg@users.noreply.github.com> Date: Wed, 6 Nov 2024 20:20:10 +0200 Subject: [PATCH] Add `_id` to the result of `helpers.search` (#2432) --- src/helpers.ts | 13 ++++++++++--- test/unit/helpers/search.test.ts | 30 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 16af051b6..10fda499a 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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 (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise { - appendFilterPath('hits.hits._source', params, true) + async search (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise> { + appendFilterPath('hits.hits._id,hits.hits._source', params, true) options.meta = true const { body: result } = await this[kClient].search(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 [] } diff --git a/test/unit/helpers/search.test.ts b/test/unit/helpers/search.test.ts index 9ed4605ab..e318571a8 100644 --- a/test/unit/helpers/search.test.ts +++ b/test/unit/helpers/search.test.ts @@ -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' } ]) })