From ec29379e5d4d8d0a3b28afe498b4c76cce396781 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 2 Sep 2020 16:50:33 +0200 Subject: [PATCH] Fix: there might be objects with the same top level key --- dsl/src/query-helpers.ts | 79 +++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/dsl/src/query-helpers.ts b/dsl/src/query-helpers.ts index 9825af0d4..18371cee4 100644 --- a/dsl/src/query-helpers.ts +++ b/dsl/src/query-helpers.ts @@ -26,53 +26,40 @@ import deepMerge from 'deepmerge' import * as t from './types' function Q (...blocks: t.AnyQuery[]): Record { - const { aggs, - collapse, - explain, - from, - highlight, - indices_boost, - min_score, - post_filter, - profile, - rescore, - script_fields, - search_after, - size, - slice, - sort, - _source, - suggest, - terminate_after, - timeout, - track_scores, - version, - ...queries - } = Object.assign.apply({}, blocks.flat()) + const topLevelKeys = [ + 'aggs', + 'collapse', + 'explain', + 'from', + 'highlight', + 'indices_boost', + 'min_score', + 'post_filter', + 'profile', + 'rescore', + 'script_fields', + 'search_after', + 'size', + 'slice', + 'sort', + '_source', + 'suggest', + 'terminate_after', + 'timeout', + 'track_scores', + 'version' + ] - const query: t.AnyQuery[] = Object.keys(queries).map(q => ({ [q]: queries[q] })) - const body: Record = query.length > 0 ? Q.bool(...query) : {} - if (aggs) body.aggs = aggs - if (collapse) body.collapse = collapse - if (explain) body.explain = explain - if (from) body.from = from - if (highlight) body.highlight = highlight - if (indices_boost) body.indices_boost = indices_boost - if (min_score) body.min_score = min_score - if (post_filter) body.post_filter = post_filter - if (profile) body.profile = profile - if (rescore) body.rescore = rescore - if (script_fields) body.script_fields = script_fields - if (search_after) body.search_after = search_after - if (size) body.size = size - if (slice) body.slice = slice - if (sort) body.sort = sort - if (_source) body._source = _source - if (suggest) body.suggest = suggest - if (terminate_after) body.terminate_after = terminate_after - if (timeout) body.timeout = timeout - if (track_scores) body.track_scores = track_scores - if (version) body.version = version + const queries = blocks.flat().filter(block => { + return !topLevelKeys.includes(Object.keys(block)[0]) + }) + const body: Record = queries.length > 0 ? Q.bool(...queries) : {} + for (const block of blocks) { + const key = Object.keys(block)[0] + if (topLevelKeys.includes(key)) { + body[key] = block[key] + } + } return body }