From 03997248b52b8f1cb28c2c0c325a950070f8049d Mon Sep 17 00:00:00 2001 From: Kevin Masseix Date: Mon, 17 May 2021 21:25:42 +0200 Subject: [PATCH] [Query Builder] Correctly handle match phrases when a single value is specified or when the match phrases is negated (#33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Build query] Correctly handle phrases filter that specify only one single value Contrary to the phrase filter type which always specify a meta.params.query, the phrases filter type never specify a meta.params.query even when a single value is selected. This anomaly trigger an error generated by the MonoFieldQueryBase included in the elastic-builder dependency and prevent the generation of the report. Signed-off-by: Kévin Masseix * [Build query] Correctly handle negated phrases filter that specify only one single value Signed-off-by: Kévin Masseix * [Build query] Correctlty handle negated phrases to exclude them from the report instead of including them Signed-off-by: Kévin Masseix --- .../server/routes/utils/dataReportHelpers.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dashboards-reports/server/routes/utils/dataReportHelpers.ts b/dashboards-reports/server/routes/utils/dataReportHelpers.ts index 2415bfa7..f20acc96 100644 --- a/dashboards-reports/server/routes/utils/dataReportHelpers.ts +++ b/dashboards-reports/server/routes/utils/dataReportHelpers.ts @@ -86,7 +86,7 @@ export const buildQuery = (report, is_count) => { } } else { requestBody.should( - esb.matchPhraseQuery(item.meta.key, item.meta.params.query) + esb.matchPhraseQuery(item.meta.key, item.meta.value) ); } requestBody.minimumShouldMatch(1); @@ -104,17 +104,19 @@ export const buildQuery = (report, is_count) => { requestBody.mustNot(esb.existsQuery(item.meta.key)); break; case 'phrases': + let negatedBody = esb.boolQuery(); if (item.meta.value.indexOf(',') > -1) { const valueSplit = item.meta.value.split(', '); for (const [key, incr] of valueSplit.entries()) { - requestBody.should(esb.matchPhraseQuery(item.meta.key, incr)); + negatedBody.should(esb.matchPhraseQuery(item.meta.key, incr)); } } else { - requestBody.should( - esb.matchPhraseQuery(item.meta.key, item.meta.params.query) + negatedBody.should( + esb.matchPhraseQuery(item.meta.key, item.meta.value) ); } - requestBody.minimumShouldMatch(1); + negatedBody.minimumShouldMatch(1); + requestBody.mustNot(negatedBody); break; } break;