From 6038d01f45047ecf430e6c910c2316ceaf62a6c2 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 8 Feb 2021 17:11:28 +0100 Subject: [PATCH] Stash --- dsl/es-types.d.ts | 17 +++++++++---- dsl/src/fluent.ts | 10 +++++++- dsl/src/query.ts | 17 +++++++++++++ test/dsl/fluent-query.test.ts | 45 +++++++++++++++++++++++++++++++++++ test/dsl/query.test.ts | 26 ++++++++++++++++++++ test/unit/client.test.js | 35 +++++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 5 deletions(-) diff --git a/dsl/es-types.d.ts b/dsl/es-types.d.ts index c74b09d64..7add00497 100644 --- a/dsl/es-types.d.ts +++ b/dsl/es-types.d.ts @@ -123,6 +123,9 @@ declare namespace T { export type GeoHashPrecision = number + export interface UserDefinedValue { + } + export interface Aggregate { meta?: Record } @@ -3356,15 +3359,21 @@ declare namespace T { to?: double } - export interface InlineScript { + export interface ScriptBase { + lang?: string + params?: Record + } + + export interface InlineScript extends ScriptBase { source?: string } - export interface Script { - lang?: string - params?: Record + export interface IndexedScript extends ScriptBase { + id?: string } + export type Script = InlineScript | IndexedScript | string + export interface ScriptField { script?: Script } diff --git a/dsl/src/fluent.ts b/dsl/src/fluent.ts index 0561d0e59..32e4a8a61 100644 --- a/dsl/src/fluent.ts +++ b/dsl/src/fluent.ts @@ -306,6 +306,14 @@ class FluentQ { return this } + script (source: string): this + script (source: string, lang: string): this + script (source: string, params: Record, lang?: string): this + script (source: string, params?: any, lang?: any): this { + this[kState].push(Q.script(source, params, lang)) + return this + } + size (s: number | Symbol): this { this[kState].push(Q.size(s)) return this @@ -328,7 +336,7 @@ class FluentQ { } toJSON () { - return this.build() + return this.buildQuery() } } diff --git a/dsl/src/query.ts b/dsl/src/query.ts index 51d337183..c73cdc8ea 100644 --- a/dsl/src/query.ts +++ b/dsl/src/query.ts @@ -602,6 +602,23 @@ namespace Q { // @ts-expect-error return { size: s } } + + export function script (source: string): T.ScriptQuery + export function script (source: string, lang: string): T.ScriptQuery + export function script (source: string, params: Record, lang?: string): T.ScriptQuery + export function script (source: string, params?: any, lang?: any): T.ScriptQuery { + if (typeof params === 'string') { + return { script: { source, lang: params } } + } else if (typeof params === 'object') { + if (typeof lang === 'string') { + return { script: { source, lang, params } } + } else { + return { script: { source, params } } + } + } else { + return { script: source } + } + } } // Tries to flat a bool query based on the content diff --git a/test/dsl/fluent-query.test.ts b/test/dsl/fluent-query.test.ts index 30b1b5cda..4d5f0b2c1 100644 --- a/test/dsl/fluent-query.test.ts +++ b/test/dsl/fluent-query.test.ts @@ -373,6 +373,30 @@ test('size', t => { t.end() }) +test('script', t => { + t.deepEqual( + F().script("doc['num1'].value > 1").build(), + Q(Q.script("doc['num1'].value > 1")) + ) + + t.deepEqual( + F().script("doc['num1'].value > 1", 'painless').build(), + Q(Q.script("doc['num1'].value > 1", 'painless')) + ) + + t.deepEqual( + F().script("doc['num1'].value > 1", { foo: 'bar' }).build(), + Q(Q.script("doc['num1'].value > 1", { foo: 'bar' })) + ) + + t.deepEqual( + F().script("doc['num1'].value > 1", { foo: 'bar' }, 'painless').build(), + Q(Q.script("doc['num1'].value > 1", { foo: 'bar' }, 'painless')) + ) + + t.end() +}) + test('must', t => { const q1 = F().must( F().match('foo', 'bar'), @@ -509,3 +533,24 @@ test('and', t => { t.end() }) + +test('toJSON', t => { + const q1 = F() + .match('foo', 'bar') + + t.strictEqual( + JSON.stringify(q1), + '{"match":{"foo":"bar"}}' + ) + + const q2 = F() + .match('foo', 'bar') + .match('foo', 'baz') + + t.strictEqual( + JSON.stringify(q2), + '{"bool":{"must":[{"match":{"foo":"bar"}},{"match":{"foo":"baz"}}]}}' + ) + + t.end() +}) diff --git a/test/dsl/query.test.ts b/test/dsl/query.test.ts index 2fcd3dc48..a7ddb8040 100644 --- a/test/dsl/query.test.ts +++ b/test/dsl/query.test.ts @@ -1018,6 +1018,32 @@ test('name', t => { t.end() }) +test('script', t => { + t.type(Q.script, 'function') + + t.deepEqual( + Q.script("doc['num1'].value > 1"), + { script: "doc['num1'].value > 1" } + ) + + t.deepEqual( + Q.script("doc['num1'].value > 1", 'painless'), + { script: { source: "doc['num1'].value > 1", lang: 'painless' } } + ) + + t.deepEqual( + Q.script("doc['num1'].value > 1", { foo: 'bar' }), + { script: { source: "doc['num1'].value > 1", params: { foo: 'bar' } } } + ) + + t.deepEqual( + Q.script("doc['num1'].value > 1", { foo: 'bar' }, 'painless'), + { script: { source: "doc['num1'].value > 1", params: { foo: 'bar' }, lang: 'painless' } } + ) + + t.end() +}) + // build a condition bloc function c (key: string): types.Condition { return { match: { [key]: key } } diff --git a/test/unit/client.test.js b/test/unit/client.test.js index cd3ce17f7..061153b2f 100644 --- a/test/unit/client.test.js +++ b/test/unit/client.test.js @@ -861,6 +861,41 @@ test('Elastic cloud config', t => { t.deepEqual(pool._ssl, { secureProtocol: 'TLSv1_2_method' }) }) + t.test('Without kibana component', t => { + t.plan(5) + const client = new Client({ + cloud: { + // 'localhost$abcd$efgh' + id: 'name:bG9jYWxob3N0JGFiY2Qk', + username: 'elastic', + password: 'changeme' + } + }) + + const pool = client.connectionPool + t.ok(pool instanceof CloudConnectionPool) + t.match(pool.connections.find(c => c.id === 'https://abcd.localhost/'), { + url: new URL('https://elastic:changeme@abcd.localhost'), + id: 'https://abcd.localhost/', + headers: { + authorization: 'Basic ' + Buffer.from('elastic:changeme').toString('base64') + }, + ssl: { secureProtocol: 'TLSv1_2_method' }, + deadCount: 0, + resurrectTimeout: 0, + roles: { + master: true, + data: true, + ingest: true, + ml: false + } + }) + + t.strictEqual(client.transport.compression, 'gzip') + t.strictEqual(client.transport.suggestCompression, true) + t.deepEqual(pool._ssl, { secureProtocol: 'TLSv1_2_method' }) + }) + t.test('Auth as separate option', t => { t.plan(5) const client = new Client({