Skip to content

Commit 9a2b257

Browse files
authored
Merge pull request #24 from justasmalinauskas/sime_query_string_support
Add simple_query_string support
2 parents 5384563 + 3eb5499 commit 9a2b257

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

src/Query/SimpleQueryStringQuery.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Erichard\ElasticQueryBuilder\Query;
4+
5+
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
6+
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
7+
8+
class SimpleQueryStringQuery implements QueryInterface
9+
{
10+
use HasMinimumShouldMatch;
11+
12+
/**
13+
* @param mixed[]|string[] $fields
14+
*/
15+
public function __construct(
16+
protected array $fields,
17+
protected string $query,
18+
19+
private ?string $flags = null,
20+
private ?bool $fuzzyTranspositions = null,
21+
private ?int $fuzzyMaxExpansions = null,
22+
private ?int $fuzzyPrefixLength = null,
23+
?string $minimumShouldMatch = null,
24+
private ?string $defaultOperator = null,
25+
private ?string $analyzer = null,
26+
private ?bool $lenient = null,
27+
private ?string $quoteFieldSuffix = null,
28+
private ?bool $analyzeWildCard = null,
29+
private ?bool $autoGenerateSynonymsPhraseQuery = null,
30+
31+
protected array $params = [],
32+
) {
33+
$this->minimumShouldMatch = $minimumShouldMatch;
34+
}
35+
36+
public function setFlags(string|null $flags): self
37+
{
38+
$this->flags = $flags;
39+
return $this;
40+
}
41+
42+
43+
public function setFuzzyTranspositions(bool|null $fuzzyTranspositions): self
44+
{
45+
$this->fuzzyTranspositions = $fuzzyTranspositions;
46+
return $this;
47+
}
48+
49+
50+
public function setFuzzyMaxExpansions(int|null $fuzzyMaxExpansions): self
51+
{
52+
$this->fuzzyMaxExpansions = $fuzzyMaxExpansions;
53+
return $this;
54+
}
55+
56+
57+
public function setFuzzyPrefixLength(int|null $fuzzyPrefixLength): self
58+
{
59+
$this->fuzzyPrefixLength = $fuzzyPrefixLength;
60+
return $this;
61+
}
62+
63+
64+
public function setDefaultOperator(string|null $defaultOperator): self
65+
{
66+
$this->defaultOperator = $defaultOperator;
67+
return $this;
68+
}
69+
70+
71+
public function setAnalyzer(string|null $analyzer): self
72+
{
73+
$this->analyzer = $analyzer;
74+
return $this;
75+
}
76+
77+
78+
public function setLenient(bool|null $lenient): self
79+
{
80+
$this->lenient = $lenient;
81+
return $this;
82+
}
83+
84+
85+
public function setQuoteFieldSuffix(string|null $quoteFieldSuffix): self
86+
{
87+
$this->quoteFieldSuffix = $quoteFieldSuffix;
88+
return $this;
89+
}
90+
91+
92+
public function setAnalyzeWildCard(bool|null $analyzeWildCard): self
93+
{
94+
$this->analyzeWildCard = $analyzeWildCard;
95+
return $this;
96+
}
97+
98+
99+
public function setAutoGenerateSynonymsPhraseQuery(bool|null $autoGenerateSynonymsPhraseQuery): self
100+
{
101+
$this->autoGenerateSynonymsPhraseQuery = $autoGenerateSynonymsPhraseQuery;
102+
return $this;
103+
}
104+
105+
public function setFields(array $fields): self
106+
{
107+
$this->fields = $fields;
108+
109+
return $this;
110+
}
111+
112+
public function setQuery(string $query): self
113+
{
114+
$this->query = $query;
115+
116+
return $this;
117+
}
118+
119+
public function setParams(array $params): self
120+
{
121+
$this->params = $params;
122+
123+
return $this;
124+
}
125+
126+
public function build(): array
127+
{
128+
$data = [
129+
'query' => $this->query,
130+
'fields' => $this->fields,
131+
];
132+
if (null !== $this->flags) {
133+
$data['flags'] = $this->flags;
134+
}
135+
if (null !== $this->fuzzyTranspositions) {
136+
$data['fuzzy_transpositions'] = $this->fuzzyTranspositions;
137+
}
138+
139+
if (null !== $this->fuzzyMaxExpansions) {
140+
$data['fuzzy_max_expansions'] = $this->fuzzyMaxExpansions;
141+
}
142+
143+
if (null !== $this->fuzzyPrefixLength) {
144+
$data['fuzzy_prefix_length'] = $this->fuzzyPrefixLength;
145+
}
146+
147+
if (null !== $this->defaultOperator) {
148+
$data['default_operator'] = $this->defaultOperator;
149+
}
150+
151+
if (null !== $this->analyzer) {
152+
$data['analyzer'] = $this->analyzer;
153+
}
154+
155+
if (null !== $this->lenient) {
156+
$data['lenient'] = $this->lenient;
157+
}
158+
159+
if (null !== $this->quoteFieldSuffix) {
160+
$data['quote_field_suffix'] = $this->quoteFieldSuffix;
161+
}
162+
163+
if (null !== $this->analyzeWildCard) {
164+
$data['analyze_wildcard'] = $this->analyzeWildCard;
165+
}
166+
if (null !== $this->autoGenerateSynonymsPhraseQuery) {
167+
$data['auto_generate_synonyms_phrase_query'] = $this->autoGenerateSynonymsPhraseQuery;
168+
}
169+
170+
$this->buildMinimumShouldMatchTo($data);
171+
172+
$build = $this->params;
173+
$build['simple_query_string'] = $data;
174+
175+
return $build;
176+
}
177+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Erichard\ElasticQueryBuilder\Query;
6+
7+
use Erichard\ElasticQueryBuilder\Query\MultiMatchQuery;
8+
use Erichard\ElasticQueryBuilder\Query\SimpleQueryStringQuery;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class SimpleQueryStringQueryTest extends TestCase
12+
{
13+
public function testItBuildTheQuery(): void
14+
{
15+
$query = new SimpleQueryStringQuery(
16+
['subject', 'body'],
17+
'~brown fox',
18+
'ALL',
19+
true,
20+
50,
21+
0,
22+
"1%",
23+
"or",
24+
"standard",
25+
false,
26+
"",
27+
false,
28+
true
29+
30+
);
31+
$this->assertEquals([
32+
'simple_query_string' =>
33+
[
34+
'query' => '~brown fox',
35+
'fields' =>
36+
[
37+
'subject',
38+
'body',
39+
],
40+
'flags' => 'ALL',
41+
'fuzzy_transpositions' => true,
42+
'fuzzy_max_expansions' => 50,
43+
'fuzzy_prefix_length' => 0,
44+
'default_operator' => 'or',
45+
'analyzer' => 'standard',
46+
'lenient' => false,
47+
'quote_field_suffix' => '',
48+
'analyze_wildcard' => false,
49+
'auto_generate_synonyms_phrase_query' => true,
50+
'minimum_should_match' => '1%',
51+
],
52+
], $query->build());
53+
}
54+
55+
public function testItBuildTheQueryWithAFuzziness(): void
56+
{
57+
$query = new SimpleQueryStringQuery(
58+
['subject', 'body'],
59+
'~brown fox',
60+
'ALL',
61+
true,
62+
50,
63+
0,
64+
"1%",
65+
"or",
66+
"standard",
67+
false,
68+
"",
69+
false,
70+
true
71+
72+
);
73+
$query->setDefaultOperator('and');
74+
$this->assertEquals([
75+
'simple_query_string' =>
76+
[
77+
'query' => '~brown fox',
78+
'fields' =>
79+
[
80+
'subject',
81+
'body',
82+
],
83+
'flags' => 'ALL',
84+
'fuzzy_transpositions' => true,
85+
'fuzzy_max_expansions' => 50,
86+
'fuzzy_prefix_length' => 0,
87+
'default_operator' => 'and',
88+
'analyzer' => 'standard',
89+
'lenient' => false,
90+
'quote_field_suffix' => '',
91+
'analyze_wildcard' => false,
92+
'auto_generate_synonyms_phrase_query' => true,
93+
'minimum_should_match' => '1%',
94+
],
95+
], $query->build());
96+
}
97+
}

0 commit comments

Comments
 (0)