Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _id to result of helpers.search #101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -192,12 +193,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 []
}
Expand Down
30 changes: 15 additions & 15 deletions test/unit/helpers/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }
]
}
}
Expand All @@ -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: {} }
}
})
Expand All @@ -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' } }
]
}
}
Expand All @@ -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' }
])
})

Loading