Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

added support for custom payload #287

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ''
]
]
]
];
}
}
```

Expand Down
10 changes: 9 additions & 1 deletion src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/SearchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public function buildQueryPayload()
],
];
}

/**
* Build the custom payload which same level with `query`.
*
* @return array|null
*/
public function buildCustomPayload()
{
}
}