feat(esmole): es_request, es_search, es_list_indices, es_get_mapping, es_cluster_health

This commit is contained in:
admin committed 2026-06-16 21:04:12 +05:00
1 parent 889e844768
commit 49fb98c18b
12 files changed
+568

No files matched your search

@@ -0,0 +1,54 @@
import { describe, expect, it } from 'vitest'
import { runEsSearch } from '../../../src/tools/search.js'
const fakeManaged = (calls: unknown[]) =>
({
config: { name: 'c', readonly: false, type: 'elasticsearch' },
backend: {
request: async (method: string, path: string, opts?: { body?: unknown }) => {
calls.push({ method, path, body: opts?.body })
return { status: 200, body: { hits: { total: { value: 0 } }, aggregations: {} } }
},
dispose: async () => {}
}
}) as never
describe('runEsSearch', () => {
it('posts query + size and forwards _source projection', async () => {
const calls: { path: string; body: { query: unknown; size: number; _source: unknown } }[] =
[]
await runEsSearch(fakeManaged(calls), 'books', { match: { t: 'x' } }, undefined, 5, [
'title'
])
expect(calls[0].path).toBe('/books/_search')
expect(calls[0].body.query).toEqual({ match: { t: 'x' } })
expect(calls[0].body.size).toBe(5)
expect(calls[0].body._source).toEqual(['title'])
})
it('forces size 0 in aggs-only mode (aggs present, no query)', async () => {
const calls: { body: { size: number; aggs: unknown; query?: unknown } }[] = []
await runEsSearch(
fakeManaged(calls),
'books',
undefined,
{ byYear: { terms: { field: 'year' } } },
undefined,
undefined
)
expect(calls[0].body.size).toBe(0)
expect(calls[0].body.aggs).toEqual({ byYear: { terms: { field: 'year' } } })
expect(calls[0].body.query).toBeUndefined()
})
it('defaults size to 10 when query given without size', async () => {
const calls: { body: { size: number } }[] = []
await runEsSearch(
fakeManaged(calls),
'books',
{ match_all: {} },
undefined,
undefined,
undefined
)
expect(calls[0].body.size).toBe(10)
})
})