From 43550186b83a5b315dd47d8f18f47d6c1ceb1dc4 Mon Sep 17 00:00:00 2001 From: Js Lim Date: Mon, 21 Oct 2019 15:37:47 +0800 Subject: [PATCH 1/2] added support for custom payload --- README.md | 33 +++++++++++++++++++++++++++++++++ src/ElasticEngine.php | 6 ++++++ src/SearchRule.php | 9 +++++++++ 3 files changed, 48 insertions(+) diff --git a/README.md b/README.md index 54078be..3706529 100644 --- a/README.md +++ b/README.md @@ -348,6 +348,39 @@ class MySearch extends SearchRule ] ]; } + + // In case you using any Elasticsearch plugin, which required to set custom payload + // Will be same level with `query`, e.g. + // + // { + // "query": { + // "must": { + // "match": { + // "name": "search word" + // } + // } + // }, + // "rescore": { + // "some": { + // "custom": { + // "attr": "" + // } + // } + // } + // } + // } + public function buildCustomPayload() + { + return [ + 'rescore' => [ + 'some' => [ + 'custom' => [ + 'attr' => '' + ] + ] + ] + ]; + } } ``` diff --git a/src/ElasticEngine.php b/src/ElasticEngine.php index a7a3dfa..da91c2f 100644 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -114,6 +114,12 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio if ($ruleEntity->isApplicable()) { $payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload()); + if ($customPayloads = $ruleEntity->buildCustomPayload()) { + foreach ($customPayloads as $customKey => $customPayload) { + $payload->addIfNotEmpty('body.' . $customKey, $customPayload); + } + } + if ($options['highlight'] ?? true) { $payload->setIfNotEmpty('body.highlight', $ruleEntity->buildHighlightPayload()); } diff --git a/src/SearchRule.php b/src/SearchRule.php index 4ecae26..b3f3d6f 100644 --- a/src/SearchRule.php +++ b/src/SearchRule.php @@ -58,4 +58,13 @@ public function buildQueryPayload() ], ]; } + + /** + * Build the custom payload which same level with `query`. + * + * @return array|null + */ + public function buildCustomPayload() + { + } } From 5f3a9a48e21db1058fd87e96dc0393d010de907f Mon Sep 17 00:00:00 2001 From: Js Lim Date: Mon, 21 Oct 2019 16:16:00 +0800 Subject: [PATCH 2/2] Ignore custom payload when requesting count --- src/ElasticEngine.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ElasticEngine.php b/src/ElasticEngine.php index da91c2f..503b302 100644 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -114,9 +114,11 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio if ($ruleEntity->isApplicable()) { $payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload()); - if ($customPayloads = $ruleEntity->buildCustomPayload()) { - foreach ($customPayloads as $customKey => $customPayload) { - $payload->addIfNotEmpty('body.' . $customKey, $customPayload); + if ($options['custom'] ?? true) { + if ($customPayloads = $ruleEntity->buildCustomPayload()) { + foreach ($customPayloads as $customKey => $customPayload) { + $payload->addIfNotEmpty('body.' . $customKey, $customPayload); + } } } @@ -254,7 +256,7 @@ public function count(Builder $builder) $count = 0; $this - ->buildSearchQueryPayloadCollection($builder, ['highlight' => false]) + ->buildSearchQueryPayloadCollection($builder, ['highlight' => false, 'custom' => false]) ->each(function ($payload) use (&$count) { $result = ElasticClient::count($payload);