Skip to content

Commit 7f28d2f

Browse files
author
Markus Kalkbrenner
authored
support multiple spellcheck dictionaries (#668)
1 parent 025639f commit 7f28d2f

File tree

9 files changed

+86
-28
lines changed

9 files changed

+86
-28
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [5.0.0-beta.1]
8+
### Added
9+
- Support multiple spellcheck dictionaries
10+
811
### Fixed
912
- Helper::rangeQuery() must not escape point values. Added a new parameter to turn off escaping.
1013

1114

1215
## [5.0.0-alpha.2]
1316
### Added
14-
- introduced FacetResultInterface
17+
- Introduced FacetResultInterface
1518

1619
### Fixed
1720
- TypeError: Return value of Solarium\Component\Result\FacetSet::getFacet()

src/Component/ComponentTraits/SpellcheckTrait.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,26 @@ public function getReload(): ?bool
6767
/**
6868
* Set dictionary option.
6969
*
70-
* The name of the dictionary to use
70+
* The name of the dictionary or dictionaries to use
7171
*
72-
* @param string $dictionary
72+
* @param string|array $dictionary
7373
*
74-
* @return self Provides fluent interface
74+
* @return SpellcheckInterface Provides fluent interface
7575
*/
76-
public function setDictionary(string $dictionary): SpellcheckInterface
76+
public function setDictionary($dictionary): SpellcheckInterface
7777
{
78+
if (is_string($dictionary)) {
79+
$dictionary = [$dictionary];
80+
}
7881
return $this->setOption('dictionary', $dictionary);
7982
}
8083

8184
/**
8285
* Get dictionary option.
8386
*
84-
* @return string|null
87+
* @return array|null
8588
*/
86-
public function getDictionary(): ?string
89+
public function getDictionary(): ?array
8790
{
8891
return $this->getOption('dictionary');
8992
}
@@ -95,7 +98,7 @@ public function getDictionary(): ?string
9598
*
9699
* @param int $count
97100
*
98-
* @return self Provides fluent interface
101+
* @return SpellcheckInterface Provides fluent interface
99102
*/
100103
public function setCount(int $count): SpellcheckInterface
101104
{
@@ -119,7 +122,7 @@ public function getCount(): ?int
119122
*
120123
* @param bool $onlyMorePopular
121124
*
122-
* @return self Provides fluent interface
125+
* @return SpellcheckInterface Provides fluent interface
123126
*/
124127
public function setOnlyMorePopular(bool $onlyMorePopular): SpellcheckInterface
125128
{
@@ -141,7 +144,7 @@ public function getOnlyMorePopular(): ?bool
141144
*
142145
* @param bool $extendedResults
143146
*
144-
* @return self Provides fluent interface
147+
* @return SpellcheckInterface Provides fluent interface
145148
*/
146149
public function setExtendedResults(bool $extendedResults): SpellcheckInterface
147150
{
@@ -163,7 +166,7 @@ public function getExtendedResults(): ?bool
163166
*
164167
* @param bool $collate
165168
*
166-
* @return self Provides fluent interface
169+
* @return SpellcheckInterface Provides fluent interface
167170
*/
168171
public function setCollate(bool $collate): SpellcheckInterface
169172
{
@@ -185,7 +188,7 @@ public function getCollate(): ?bool
185188
*
186189
* @param int $maxCollations
187190
*
188-
* @return self Provides fluent interface
191+
* @return SpellcheckInterface Provides fluent interface
189192
*/
190193
public function setMaxCollations(int $maxCollations): SpellcheckInterface
191194
{
@@ -207,7 +210,7 @@ public function getMaxCollations(): ?int
207210
*
208211
* @param int $maxCollationTries
209212
*
210-
* @return self Provides fluent interface
213+
* @return SpellcheckInterface Provides fluent interface
211214
*/
212215
public function setMaxCollationTries(int $maxCollationTries): SpellcheckInterface
213216
{
@@ -251,7 +254,7 @@ public function getMaxCollationEvaluations(): ?int
251254
*
252255
* @param bool $collateExtendedResults
253256
*
254-
* @return self Provides fluent interface
257+
* @return SpellcheckInterface Provides fluent interface
255258
*/
256259
public function setCollateExtendedResults(bool $collateExtendedResults): SpellcheckInterface
257260
{
@@ -273,7 +276,7 @@ public function getCollateExtendedResults(): ?bool
273276
*
274277
* @param float $accuracy
275278
*
276-
* @return self Provides fluent interface
279+
* @return SpellcheckInterface Provides fluent interface
277280
*/
278281
public function setAccuracy(float $accuracy): SpellcheckInterface
279282
{
@@ -296,7 +299,7 @@ public function getAccuracy(): ?float
296299
* @param string $param
297300
* @param mixed $value
298301
*
299-
* @return self Provides fluent interface
302+
* @return SpellcheckInterface Provides fluent interface
300303
*/
301304
public function setCollateParam(string $param, $value): SpellcheckInterface
302305
{

src/Component/SpellcheckInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ public function getReload(): ?bool;
5757
*
5858
* The name of the dictionary to use
5959
*
60-
* @param string $dictionary
60+
* @param string|array $dictionary
6161
*
6262
* @return self Provides fluent interface
6363
*/
64-
public function setDictionary(string $dictionary): self;
64+
public function setDictionary($dictionary): self;
6565

6666
/**
6767
* Get dictionary option.
6868
*
69-
* @return string|null
69+
* @return array|null
7070
*/
71-
public function getDictionary(): ?string;
71+
public function getDictionary(): ?array;
7272

7373
/**
7474
* Set count option.

src/Core/Query/Helper.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ public function rangeQuery(string $field, ?string $from, ?string $to, bool $incl
193193
return $field.':['.$from.' TO '.$to.']';
194194
}
195195

196-
if ($inclusive) {
197-
return $field.':['.$from.' TO '.$to.']';
198-
}
199-
200196
return $field.':{'.$from.' TO '.$to.'}';
201197
}
202198

tests/Component/RequestBuilder/SpellcheckTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,52 @@ public function testBuildComponent()
3838
'spellcheck.q' => 'testquery',
3939
'spellcheck.build' => 'false',
4040
'spellcheck.reload' => 'true',
41-
'spellcheck.dictionary' => 'testdict',
41+
'spellcheck.dictionary' => ['testdict'],
42+
'spellcheck.count' => 3,
43+
'spellcheck.onlyMorePopular' => 'false',
44+
'spellcheck.extendedResults' => 'true',
45+
'spellcheck.collate' => 'true',
46+
'spellcheck.maxCollations' => 2,
47+
'spellcheck.maxCollationTries' => 4,
48+
'spellcheck.maxCollationEvaluations' => 4,
49+
'spellcheck.collateExtendedResults' => 'true',
50+
'spellcheck.accuracy' => .2,
51+
'spellcheck.collateParam.mm' => '100%',
52+
],
53+
$request->getParams()
54+
);
55+
}
56+
57+
public function testBuildComponentMulipleDictionaries()
58+
{
59+
$builder = new RequestBuilder();
60+
$request = new Request();
61+
62+
$component = new Component();
63+
$component->setQuery('testquery');
64+
$component->setBuild(false);
65+
$component->setReload(true);
66+
$component->setDictionary(['dictionary', 'alt_dictionary']);
67+
$component->setCount(3);
68+
$component->setOnlyMorePopular(false);
69+
$component->setExtendedResults(true);
70+
$component->setCollate(true);
71+
$component->setMaxCollations(2);
72+
$component->setMaxCollationTries(4);
73+
$component->setMaxCollationEvaluations(4);
74+
$component->setCollateExtendedResults(true);
75+
$component->setAccuracy(.2);
76+
$component->setCollateParam('mm', '100%');
77+
78+
$request = $builder->buildComponent($component, $request);
79+
80+
$this->assertEquals(
81+
[
82+
'spellcheck' => 'true',
83+
'spellcheck.q' => 'testquery',
84+
'spellcheck.build' => 'false',
85+
'spellcheck.reload' => 'true',
86+
'spellcheck.dictionary' => ['dictionary', 'alt_dictionary'],
4287
'spellcheck.count' => 3,
4388
'spellcheck.onlyMorePopular' => 'false',
4489
'spellcheck.extendedResults' => 'true',

tests/Component/SpellcheckTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testSetAndGetDictionary()
8686
$this->spellCheck->setDictionary($value);
8787

8888
$this->assertEquals(
89-
$value,
89+
[$value],
9090
$this->spellCheck->getDictionary()
9191
);
9292
}

tests/Integration/AbstractTechproductsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ public function testFacetHighlightSpellcheckComponent()
140140
'card',
141141
], $words);
142142

143+
$spellcheck->setDictionary(['default', 'wordbreak']);
144+
145+
$result = $this->client->select($select);
146+
$this->assertSame(0, $result->getNumFound());
147+
148+
$this->assertSame([
149+
'power' => 'power',
150+
'cort' => 'cord',
151+
],
152+
$result->getSpellcheck()->getCollations()[0]->getCorrections());
153+
143154
$select->setQuery('power cord');
144155
// Activate highlighting.
145156
$select->getHighlighting();

tests/QueryType/Spellcheck/QueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testSetAndGetDictionary()
5050
$this->query->setDictionary($value);
5151

5252
$this->assertSame(
53-
$value,
53+
[$value],
5454
$this->query->getDictionary()
5555
);
5656
}

tests/QueryType/Spellcheck/RequestBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testBuildParams()
4040
[
4141
'spellcheck' => 'true',
4242
'spellcheck.q' => 'ap ip',
43-
'spellcheck.dictionary' => 'suggest',
43+
'spellcheck.dictionary' => ['suggest'],
4444
'spellcheck.count' => 13,
4545
'spellcheck.onlyMorePopular' => 'true',
4646
'spellcheck.collate' => 'true',

0 commit comments

Comments
 (0)