Skip to content

Commit 8d3f65f

Browse files
committed
Add support for query_string queries with options
Introduces searchQueryString and related methods to Builder for building query_string and simple_query_string queries. Adds QueryStringOptions for managing query string options, updates DslFactory and Grammar to handle query_string clauses, and registers QueryStringOptions in ManagesOptions.
1 parent 6c5a4f2 commit 8d3f65f

File tree

7 files changed

+144
-37
lines changed

7 files changed

+144
-37
lines changed

src/Eloquent/Docs/ModelDocs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
* @method static $this orSearchFuzzyPrefix($term, $fields = ['*'], $options = [])
128128
* @method static $this searchNotFuzzyPrefix($term, $fields = ['*'], $options = [])
129129
* @method static $this orSearchNotFuzzyPrefix($term, $fields = ['*'], $options = [])
130+
* -----------------------------------
131+
* @method static $this searchQueryString($query, $fields = '*', $options = [])
132+
* @method static $this orSearchQueryString($query, $fields = '*', $options = [])
133+
* @method static $this searchNotQueryString($query, $fields = '*', $options = [])
134+
* @method static $this orSearchNotQueryString($query, $fields = '*', $options = [])
130135
*===========================================
131136
* Speciality methods
132137
*===========================================

src/Query/Builder.php

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,38 +1331,6 @@ public function orWhereNotRegex($column, string $value, array $options = []): se
13311331
return $this->whereRegex($column, $value, 'or', true, $options);
13321332
}
13331333

1334-
/**
1335-
* Add a 'query_string' statement to query
1336-
*/
1337-
public function whereQueryString($column, $value, array $options = []): self
1338-
{
1339-
return $this->_buildQueryStringWheres($column, $value, 'and', false, $options);
1340-
}
1341-
1342-
public function orWhereQueryString($column, $value, array $options = []): self
1343-
{
1344-
return $this->_buildQueryStringWheres($column, $value, 'or', false, $options);
1345-
}
1346-
1347-
public function whereNotQueryString($column, $value, array $options = []): self
1348-
{
1349-
return $this->_buildQueryStringWheres($column, $value, 'and', true, $options);
1350-
}
1351-
1352-
public function orWhereNotQueryString($column, $value, array $options = []): self
1353-
{
1354-
return $this->_buildQueryStringWheres($column, $value, 'or', true, $options);
1355-
}
1356-
1357-
private function _buildQueryStringWheres($column, $value, $boolean, $not, $options): self
1358-
{
1359-
$type = 'QueryString';
1360-
1361-
$this->wheres[] = compact('column', 'value', 'type', 'boolean', 'not', 'options');
1362-
1363-
return $this;
1364-
}
1365-
13661334
/**
13671335
* Add any where clause with given options.
13681336
*/
@@ -1595,6 +1563,52 @@ public function orSearchNotFuzzyPrefix($query, mixed $columns = null, $options =
15951563
return $this->search($query, 'bool_prefix', $columns, $options, true, 'or', ['fuzziness' => 'AUTO']);
15961564
}
15971565

1566+
/**
1567+
* Add a 'query_string' statement to query
1568+
*
1569+
* @throws Exception
1570+
*/
1571+
public function searchQueryString(mixed $query, string|array $columns = '*', $options = []): self
1572+
{
1573+
return $this->_buildQueryStringWheres($columns, $query, 'and', false, $options);
1574+
}
1575+
1576+
/**
1577+
* @throws Exception
1578+
*/
1579+
public function orSearchQueryString(mixed $query, string|array $columns = '*', $options = []): self
1580+
{
1581+
return $this->_buildQueryStringWheres($columns, $query, 'or', false, $options);
1582+
}
1583+
1584+
/**
1585+
* @throws Exception
1586+
*/
1587+
public function searchNotQueryString(mixed $query, string|array $columns = '*', $options = []): self
1588+
{
1589+
return $this->_buildQueryStringWheres($columns, $query, 'and', true, $options);
1590+
}
1591+
1592+
/**
1593+
* @throws Exception
1594+
*/
1595+
public function orSearchNotQueryString(mixed $query, string|array $columns = '*', $options = []): self
1596+
{
1597+
return $this->_buildQueryStringWheres($columns, $query, 'or', true, $options);
1598+
}
1599+
1600+
/**
1601+
* @throws Exception
1602+
*/
1603+
private function _buildQueryStringWheres($column, $value, $boolean, $not, $options): self
1604+
{
1605+
$type = 'QueryString';
1606+
$options = $this->setOptions($options, 'querystring')->toArray();
1607+
$this->wheres[] = compact('column', 'value', 'type', 'boolean', 'not', 'options');
1608+
1609+
return $this;
1610+
}
1611+
15981612
// ----------------------------------------------------------------------
15991613
// Ordering
16001614
// ----------------------------------------------------------------------

src/Query/DSL/DslFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,26 @@ public static function functionScore(array $query, string $functionType, array $
267267
];
268268
}
269269

270+
public static function queryString(string|array $field, mixed $query, array $options = [])
271+
{
272+
$fields = is_array($field) ? $field : [$field];
273+
$type = 'query_string';
274+
if (! empty($options['simple_query_string'])) {
275+
$type = 'simple_query_string';
276+
}
277+
unset($options['simple_query_string']);
278+
279+
return [
280+
$type => array_merge(
281+
[
282+
'query' => (string) $query,
283+
'fields' => $fields,
284+
],
285+
$options
286+
),
287+
];
288+
}
289+
270290
// ----------------------------------------------------------------------
271291
// Aggregations
272292
// ----------------------------------------------------------------------

src/Query/Grammar.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,15 @@ protected function compileWhereSearch(Builder $builder, array $where): array
721721

722722
}
723723

724+
private function compileWhereQueryString(Builder $builder, array $where)
725+
{
726+
$fields = $where['column'];
727+
$query = $where['value'];
728+
$options = $where['options'] ?? [];
729+
730+
return DslFactory::queryString($fields, $query, $options);
731+
}
732+
724733
/**
725734
* Compile a child clause
726735
*/

src/Query/ManagesOptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PDPhilip\Elasticsearch\Query\Options\PhraseOptions;
1414
use PDPhilip\Elasticsearch\Query\Options\PhrasePrefixOptions;
1515
use PDPhilip\Elasticsearch\Query\Options\PrefixOptions;
16+
use PDPhilip\Elasticsearch\Query\Options\QueryStringOptions;
1617
use PDPhilip\Elasticsearch\Query\Options\RegexOptions;
1718
use PDPhilip\Elasticsearch\Query\Options\SearchOptions;
1819
use PDPhilip\Elasticsearch\Query\Options\TermOptions;
@@ -219,6 +220,7 @@ protected function getOptionClass($type)
219220
'prefix' => PrefixOptions::class,
220221
'regex' => RegexOptions::class,
221222
'nested' => NestedOptions::class,
223+
'querystring' => QueryStringOptions::class,
222224
default => null
223225
};
224226
}

src/Query/Options/QueryOptions.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public function __call($method, $parameters)
2222
{
2323
$key = Str::snake($method);
2424

25-
// Let ES validate the option
26-
// if (! in_array($key, $this->allowedOptions(), true)) {
27-
// throw new \InvalidArgumentException("Option '{$key}' is not allowed for this query type.");
28-
// }
29-
3025
$this->{$key} = $parameters[0];
3126

3227
return $this;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PDPhilip\Elasticsearch\Query\Options;
4+
5+
/**
6+
* QueryStringOptions for Query String Queries.
7+
*
8+
*
9+
* @method $this allowLeadingWildcard(bool $value)
10+
* @method $this analyzeWildcard(bool $value)
11+
* @method $this analyzer(string $analyzer)
12+
* @method $this autoGenerateSynonymsPhraseQuery(bool $value)
13+
* @method $this boost(float $value)
14+
* @method $this defaultOperator(string $value) OR|AND
15+
* @method $this fuzziness(string|int $value)
16+
* @method $this fuzzyMaxExpansions(int $value)
17+
* @method $this fuzzyPrefixLength(int $value)
18+
* @method $this fuzzyTranspositions(bool $value)
19+
* @method $this lenient(bool $value)
20+
* @method $this maxDeterminizedStates(int $value)
21+
* @method $this minimumShouldMatch(string $value)
22+
* @method $this quoteAnalyzer(string $value)
23+
* @method $this phraseSlop(int $value)
24+
* @method $this quoteFieldSuffix(string $value)
25+
* @method $this rewrite(string $value)
26+
* @method $this timeZone(string $value)
27+
* @method $this simpleQueryString(bool $value)
28+
*/
29+
class QueryStringOptions extends QueryOptions
30+
{
31+
public function allowedOptions(): array
32+
{
33+
return [
34+
'allow_leading_wildcard',
35+
'analyze_wildcard',
36+
'analyzer',
37+
'auto_generate_synonyms_phrase_query',
38+
'boost',
39+
'default_operator',
40+
'fuzziness',
41+
'fuzzy_max_expansions',
42+
'fuzzy_prefix_length',
43+
'fuzzy_transpositions',
44+
'fuzzy_rewrite',
45+
'fuzzy_transpositions',
46+
'lenient',
47+
'max_determinized_states',
48+
'minimum_should_match',
49+
'quote_analyzer',
50+
'phrase_slop',
51+
'quote_field_suffix',
52+
'rewrite',
53+
'time_zone',
54+
'simple_query_string',
55+
];
56+
}
57+
58+
public function asSimple()
59+
{
60+
$this->simpleQueryString(true);
61+
}
62+
}

0 commit comments

Comments
 (0)