@@ -25,12 +25,14 @@ class BoolQueryBuilder implements BoolQuery, Criteria
2525 use SupportsPath;
2626
2727 protected CriteriaCollection $ must ;
28+ protected CriteriaCollection $ should ;
2829 protected CriteriaCollection $ filter ;
2930 protected CriteriaCollection $ mustNot ;
3031
3132 public function __construct (protected string $ path = '' , protected bool $ emptyMatchAll = true )
3233 {
3334 $ this ->must = new CriteriaCollection ();
35+ $ this ->should = new CriteriaCollection ();
3436 $ this ->filter = new CriteriaCollection ();
3537 $ this ->mustNot = new CriteriaCollection ();
3638 }
@@ -65,6 +67,7 @@ public function toDSL(): array
6567
6668 $ body = array_merge (
6769 $ this ->criteriasToDSL ('must ' , $ this ->must ),
70+ $ this ->criteriasToDSL ('should ' , $ this ->should ),
6871 $ this ->criteriasToDSL ('filter ' , $ this ->filter ),
6972 $ this ->criteriasToDSL ('must_not ' , $ this ->mustNot ),
7073 );
@@ -79,7 +82,7 @@ protected function criteriasToDSL(string $key, CriteriaCollection $criterias): a
7982
8083 protected function criteriasCount (): int
8184 {
82- return $ this ->must ->count () + $ this ->filter ->count () + $ this ->mustNot ->count ();
85+ return $ this ->must ->count () + $ this ->should -> count () + $ this -> filter ->count () + $ this ->mustNot ->count ();
8386 }
8487
8588 //endregion
@@ -147,12 +150,25 @@ public function whereNotNull(string $field): static
147150
148151 public function whereMatch (string $ field , string $ query , string |MatchOptions $ operator = 'or ' ): static
149152 {
150- $ options = is_string ($ operator ) ? MatchOptions::make ($ operator ) : $ operator ;
151- $ this ->must ->add (new OneMatch ($ this ->absolutePath ($ field ), $ query , $ options ));
153+ $ this ->must ->add ($ this ->makeMatch ($ field , $ query , $ operator ));
154+
155+ return $ this ;
156+ }
157+
158+ public function orWhereMatch (string $ field , string $ query , string |MatchOptions $ operator = 'or ' ): static
159+ {
160+ $ this ->should ->add ($ this ->makeMatch ($ field , $ query , $ operator ));
152161
153162 return $ this ;
154163 }
155164
165+ protected function makeMatch (string $ field , string $ query , string |MatchOptions $ operator = 'or ' ): OneMatch
166+ {
167+ $ options = is_string ($ operator ) ? MatchOptions::make ($ operator ) : $ operator ;
168+
169+ return new OneMatch ($ this ->absolutePath ($ field ), $ query , $ options );
170+ }
171+
156172 public function whereMultiMatch (array $ fields , string $ query , string |MultiMatchOptions |null $ type = null ): static
157173 {
158174 $ options = is_string ($ type ) ? MultiMatchOptions::make ($ type ) : $ type ;
@@ -169,11 +185,31 @@ public function whereMultiMatch(array $fields, string $query, string|MultiMatchO
169185
170186 public function whereWildcard (string $ field , string $ query , ?WildcardOptions $ options = null ): static
171187 {
172- $ this ->must ->add (new Wildcard ( $ this ->absolutePath ($ field) , $ query , $ options ?: new WildcardOptions () ));
188+ $ this ->must ->add ($ this ->makeWildcard ($ field , $ query , $ options ));
173189
174190 return $ this ;
175191 }
176192
193+ public function orWhereWildcard (string $ field , string $ query , ?WildcardOptions $ options = null ): static
194+ {
195+ $ this ->should ->add ($ this ->makeWildcard ($ field , $ query , $ options ));
196+
197+ return $ this ;
198+ }
199+
200+ public function makeWildcard (string $ field , string $ query , ?WildcardOptions $ options = null ): Wildcard
201+ {
202+ return new Wildcard ($ this ->absolutePath ($ field ), $ query , $ options ?: new WildcardOptions ());
203+ }
204+
205+ public function addMustBool (): static
206+ {
207+ $ boolCriteria = static ::make ();
208+ $ this ->must ->add ($ boolCriteria );
209+
210+ return $ boolCriteria ;
211+ }
212+
177213 protected function addNestedCriteria (string $ nested , Closure $ filter , CriteriaCollection $ target ): static
178214 {
179215 $ path = $ this ->absolutePath ($ nested );
0 commit comments