From 3fa2dbd98dc44407878f8e685f6c7a5cfcb3f351 Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Thu, 12 May 2022 16:56:04 +0100 Subject: [PATCH 1/2] feat: pass additional `match` options to ES --- src/QueryGrammar.php | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/QueryGrammar.php b/src/QueryGrammar.php index 0c1386e..c35a6b5 100644 --- a/src/QueryGrammar.php +++ b/src/QueryGrammar.php @@ -514,19 +514,17 @@ protected function compileWhereSearch(Builder $builder, array $where): array 'match' => [ $field => [ 'query' => $where['value'], - ] + ], ], ]; } - if (! empty($where['options']['fuzziness'])) { - $matchType = array_keys($query)[0]; + $matchType = array_keys($query)[0]; - if ($matchType === 'multi_match') { - $query[$matchType]['fuzziness'] = $where['options']['fuzziness']; - } else { - $query[$matchType][$field]['fuzziness'] = $where['options']['fuzziness']; - } + if (! empty($where['options']['fuzziness']) && $matchType === 'multi_match') { + $query[$matchType]['fuzziness'] = $where['options']['fuzziness']; + } elseif (! empty($where['options']['fuzziness'])) { + $query[$matchType][$field]['fuzziness'] = $where['options']['fuzziness']; } if (! empty($where['options']['constant_score'])) { @@ -537,6 +535,17 @@ protected function compileWhereSearch(Builder $builder, array $where): array ]; } + $otherOptions = array_diff_key( + $where['options'], + array_flip(['fields', 'fuzziness', 'constant_score']) + ); + + if ($matchType === 'multi_match') { + $query['multi_match'] = $query['multi_match'] + $otherOptions; + } else { + $query[$matchType][$field] = $query[$matchType][$field] + $otherOptions; + } + return $query; } @@ -997,19 +1006,12 @@ protected function compileMetricAggregation(array $aggregation): array { $metric = $aggregation['type']; - if (is_array($aggregation['args'])) { - $args = array_filter([ - 'field' => $aggregation['args']['field'] ?? null, - 'script' => $aggregation['args']['script'] ?? null, - ]); - } else { - $args = [ - 'field' => $aggregation['args'], - ]; - } + $field = is_array($aggregation['args']) ? $aggregation['args']['field'] : $aggregation['args']; return [ - $metric => $args, + $metric => [ + 'field' => $field + ] ]; } From ac9021b0bfbc352096b0416ac1841c38fcb0cdec Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Thu, 12 May 2022 16:58:25 +0100 Subject: [PATCH 2/2] chore: put back script changes --- src/QueryGrammar.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/QueryGrammar.php b/src/QueryGrammar.php index c35a6b5..4f7c1f6 100644 --- a/src/QueryGrammar.php +++ b/src/QueryGrammar.php @@ -1006,12 +1006,19 @@ protected function compileMetricAggregation(array $aggregation): array { $metric = $aggregation['type']; - $field = is_array($aggregation['args']) ? $aggregation['args']['field'] : $aggregation['args']; + if (is_array($aggregation['args'])) { + $args = array_filter([ + 'field' => $aggregation['args']['field'] ?? null, + 'script' => $aggregation['args']['script'] ?? null, + ]); + } else { + $args = [ + 'field' => $aggregation['args'], + ]; + } return [ - $metric => [ - 'field' => $field - ] + $metric => $args, ]; }