@@ -22,36 +22,6 @@ public function __construct()
22
22
$ this ->updateMapping = config ('scout_elastic.update_mapping ' );
23
23
}
24
24
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
-
55
25
public function update ($ models )
56
26
{
57
27
$ models ->map (function ($ model ) {
@@ -82,17 +52,39 @@ public function delete($models)
82
52
});
83
53
}
84
54
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 ]]]
92
65
);
93
66
}
94
67
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 ();
96
88
97
89
if ($ builder instanceof SearchBuilder) {
98
90
$ searchRules = $ builder ->rules ?: $ builder ->model ->getSearchRules ();
@@ -117,11 +109,7 @@ protected function performSearch(Builder $builder, array $options = []) {
117
109
$ options
118
110
);
119
111
120
- $ results = ElasticClient::search ($ payload );
121
-
122
- if ($ this ->getTotalCount ($ results ) > 0 ) {
123
- return $ results ;
124
- }
112
+ $ payloadCollection ->push ($ payload );
125
113
}
126
114
} else {
127
115
$ payload = $ this ->buildSearchQueryPayload (
@@ -130,10 +118,33 @@ protected function performSearch(Builder $builder, array $options = []) {
130
118
$ options
131
119
);
132
120
133
- $ results = ElasticClient:: search ($ payload );
121
+ $ payloadCollection -> push ($ payload );
134
122
}
135
123
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 ;
137
148
}
138
149
139
150
public function search (Builder $ builder )
0 commit comments