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..503b302 100644 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -114,6 +114,14 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio if ($ruleEntity->isApplicable()) { $payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload()); + if ($options['custom'] ?? true) { + if ($customPayloads = $ruleEntity->buildCustomPayload()) { + foreach ($customPayloads as $customKey => $customPayload) { + $payload->addIfNotEmpty('body.' . $customKey, $customPayload); + } + } + } + if ($options['highlight'] ?? true) { $payload->setIfNotEmpty('body.highlight', $ruleEntity->buildHighlightPayload()); } @@ -248,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); 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() + { + } }