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

Commit c93124b

Browse files
committed
Added the buildPayload method to the Builder. It allows to retrieve the payload, that will be send to ES.
1 parent 7215621 commit c93124b

File tree

2 files changed

+61
-45
lines changed

2 files changed

+61
-45
lines changed

src/Builders/FilterBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,9 @@ public function profile()
139139
{
140140
return $this->engine()->profile($this);
141141
}
142+
143+
public function buildPayload()
144+
{
145+
return $this->engine()->buildSearchQueryPayloadCollection($this);
146+
}
142147
}

src/ElasticEngine.php

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,6 @@ public function __construct()
2222
$this->updateMapping = config('scout_elastic.update_mapping');
2323
}
2424

25-
protected function buildSearchQueryPayload(Builder $builder, $queryPayload, array $options = [])
26-
{
27-
foreach ($builder->wheres as $clause => $filters) {
28-
if (count($filters) == 0) {
29-
continue;
30-
}
31-
32-
$queryPayload = array_merge_recursive(
33-
$queryPayload,
34-
['filter' => ['bool' => [$clause => $filters]]]
35-
);
36-
}
37-
38-
$payload = (new TypePayload($builder->model))
39-
->setIfNotEmpty('body.query.bool', $queryPayload)
40-
->setIfNotEmpty('body.sort', $builder->orders)
41-
->setIfNotEmpty('body.explain', isset($options['explain']) ? $options['explain'] : null)
42-
->setIfNotEmpty('body.profile', isset($options['profile']) ? $options['profile'] : null);
43-
44-
if ($size = isset($options['limit']) ? $options['limit'] : $builder->limit) {
45-
$payload->set('body.size', $size);
46-
47-
if (isset($options['page'])) {
48-
$payload->set('body.from', ($options['page'] - 1) * $size);
49-
}
50-
}
51-
52-
return $payload->get();
53-
}
54-
5525
public function update($models)
5626
{
5727
$models->map(function ($model) {
@@ -82,17 +52,39 @@ public function delete($models)
8252
});
8353
}
8454

85-
protected function performSearch(Builder $builder, array $options = []) {
86-
if ($builder->callback) {
87-
return call_user_func(
88-
$builder->callback,
89-
ElasticClient::getFacadeRoot(),
90-
$builder->query,
91-
$options
55+
protected function buildSearchQueryPayload(Builder $builder, $queryPayload, array $options = [])
56+
{
57+
foreach ($builder->wheres as $clause => $filters) {
58+
if (count($filters) == 0) {
59+
continue;
60+
}
61+
62+
$queryPayload = array_merge_recursive(
63+
$queryPayload,
64+
['filter' => ['bool' => [$clause => $filters]]]
9265
);
9366
}
9467

95-
$results = null;
68+
$payload = (new TypePayload($builder->model))
69+
->setIfNotEmpty('body.query.bool', $queryPayload)
70+
->setIfNotEmpty('body.sort', $builder->orders)
71+
->setIfNotEmpty('body.explain', isset($options['explain']) ? $options['explain'] : null)
72+
->setIfNotEmpty('body.profile', isset($options['profile']) ? $options['profile'] : null);
73+
74+
if ($size = isset($options['limit']) ? $options['limit'] : $builder->limit) {
75+
$payload->set('body.size', $size);
76+
77+
if (isset($options['page'])) {
78+
$payload->set('body.from', ($options['page'] - 1) * $size);
79+
}
80+
}
81+
82+
return $payload->get();
83+
}
84+
85+
public function buildSearchQueryPayloadCollection(Builder $builder, array $options = [])
86+
{
87+
$payloadCollection = collect();
9688

9789
if ($builder instanceof SearchBuilder) {
9890
$searchRules = $builder->rules ?: $builder->model->getSearchRules();
@@ -117,11 +109,7 @@ protected function performSearch(Builder $builder, array $options = []) {
117109
$options
118110
);
119111

120-
$results = ElasticClient::search($payload);
121-
122-
if ($this->getTotalCount($results) > 0) {
123-
return $results;
124-
}
112+
$payloadCollection->push($payload);
125113
}
126114
} else {
127115
$payload = $this->buildSearchQueryPayload(
@@ -130,10 +118,33 @@ protected function performSearch(Builder $builder, array $options = []) {
130118
$options
131119
);
132120

133-
$results = ElasticClient::search($payload);
121+
$payloadCollection->push($payload);
134122
}
135123

136-
return $results;
124+
return $payloadCollection;
125+
}
126+
127+
protected function performSearch(Builder $builder, array $options = []) {
128+
if ($builder->callback) {
129+
return call_user_func(
130+
$builder->callback,
131+
ElasticClient::getFacadeRoot(),
132+
$builder->query,
133+
$options
134+
);
135+
}
136+
137+
$result = null;
138+
139+
$this->buildSearchQueryPayloadCollection($builder, $options)->each(function($payload) use (&$result) {
140+
$result = ElasticClient::search($payload);
141+
142+
if ($this->getTotalCount($result) > 0) {
143+
return false;
144+
}
145+
});
146+
147+
return $result;
137148
}
138149

139150
public function search(Builder $builder)

0 commit comments

Comments
 (0)