From 7f28d2ffe41be83a37102ea7f61d50c4d54735e5 Mon Sep 17 00:00:00 2001 From: Markus Kalkbrenner Date: Thu, 18 Apr 2019 14:44:18 +0200 Subject: [PATCH] support multiple spellcheck dictionaries (#668) --- CHANGELOG.md | 5 +- .../ComponentTraits/SpellcheckTrait.php | 33 +++++++------ src/Component/SpellcheckInterface.php | 8 ++-- src/Core/Query/Helper.php | 4 -- .../RequestBuilder/SpellcheckTest.php | 47 ++++++++++++++++++- tests/Component/SpellcheckTest.php | 2 +- .../Integration/AbstractTechproductsTest.php | 11 +++++ tests/QueryType/Spellcheck/QueryTest.php | 2 +- .../Spellcheck/RequestBuilderTest.php | 2 +- 9 files changed, 86 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86b919a82..6d5246037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [5.0.0-beta.1] +### Added +- Support multiple spellcheck dictionaries + ### Fixed - Helper::rangeQuery() must not escape point values. Added a new parameter to turn off escaping. ## [5.0.0-alpha.2] ### Added -- introduced FacetResultInterface +- Introduced FacetResultInterface ### Fixed - TypeError: Return value of Solarium\Component\Result\FacetSet::getFacet() diff --git a/src/Component/ComponentTraits/SpellcheckTrait.php b/src/Component/ComponentTraits/SpellcheckTrait.php index 3a5b30aa3..e50d2fcc6 100644 --- a/src/Component/ComponentTraits/SpellcheckTrait.php +++ b/src/Component/ComponentTraits/SpellcheckTrait.php @@ -67,23 +67,26 @@ public function getReload(): ?bool /** * Set dictionary option. * - * The name of the dictionary to use + * The name of the dictionary or dictionaries to use * - * @param string $dictionary + * @param string|array $dictionary * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ - public function setDictionary(string $dictionary): SpellcheckInterface + public function setDictionary($dictionary): SpellcheckInterface { + if (is_string($dictionary)) { + $dictionary = [$dictionary]; + } return $this->setOption('dictionary', $dictionary); } /** * Get dictionary option. * - * @return string|null + * @return array|null */ - public function getDictionary(): ?string + public function getDictionary(): ?array { return $this->getOption('dictionary'); } @@ -95,7 +98,7 @@ public function getDictionary(): ?string * * @param int $count * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setCount(int $count): SpellcheckInterface { @@ -119,7 +122,7 @@ public function getCount(): ?int * * @param bool $onlyMorePopular * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setOnlyMorePopular(bool $onlyMorePopular): SpellcheckInterface { @@ -141,7 +144,7 @@ public function getOnlyMorePopular(): ?bool * * @param bool $extendedResults * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setExtendedResults(bool $extendedResults): SpellcheckInterface { @@ -163,7 +166,7 @@ public function getExtendedResults(): ?bool * * @param bool $collate * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setCollate(bool $collate): SpellcheckInterface { @@ -185,7 +188,7 @@ public function getCollate(): ?bool * * @param int $maxCollations * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setMaxCollations(int $maxCollations): SpellcheckInterface { @@ -207,7 +210,7 @@ public function getMaxCollations(): ?int * * @param int $maxCollationTries * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setMaxCollationTries(int $maxCollationTries): SpellcheckInterface { @@ -251,7 +254,7 @@ public function getMaxCollationEvaluations(): ?int * * @param bool $collateExtendedResults * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setCollateExtendedResults(bool $collateExtendedResults): SpellcheckInterface { @@ -273,7 +276,7 @@ public function getCollateExtendedResults(): ?bool * * @param float $accuracy * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setAccuracy(float $accuracy): SpellcheckInterface { @@ -296,7 +299,7 @@ public function getAccuracy(): ?float * @param string $param * @param mixed $value * - * @return self Provides fluent interface + * @return SpellcheckInterface Provides fluent interface */ public function setCollateParam(string $param, $value): SpellcheckInterface { diff --git a/src/Component/SpellcheckInterface.php b/src/Component/SpellcheckInterface.php index 20dd9ba94..a5fbb6c5f 100644 --- a/src/Component/SpellcheckInterface.php +++ b/src/Component/SpellcheckInterface.php @@ -57,18 +57,18 @@ public function getReload(): ?bool; * * The name of the dictionary to use * - * @param string $dictionary + * @param string|array $dictionary * * @return self Provides fluent interface */ - public function setDictionary(string $dictionary): self; + public function setDictionary($dictionary): self; /** * Get dictionary option. * - * @return string|null + * @return array|null */ - public function getDictionary(): ?string; + public function getDictionary(): ?array; /** * Set count option. diff --git a/src/Core/Query/Helper.php b/src/Core/Query/Helper.php index 472302287..e7b241638 100644 --- a/src/Core/Query/Helper.php +++ b/src/Core/Query/Helper.php @@ -193,10 +193,6 @@ public function rangeQuery(string $field, ?string $from, ?string $to, bool $incl return $field.':['.$from.' TO '.$to.']'; } - if ($inclusive) { - return $field.':['.$from.' TO '.$to.']'; - } - return $field.':{'.$from.' TO '.$to.'}'; } diff --git a/tests/Component/RequestBuilder/SpellcheckTest.php b/tests/Component/RequestBuilder/SpellcheckTest.php index c52e56025..178123d5b 100644 --- a/tests/Component/RequestBuilder/SpellcheckTest.php +++ b/tests/Component/RequestBuilder/SpellcheckTest.php @@ -38,7 +38,52 @@ public function testBuildComponent() 'spellcheck.q' => 'testquery', 'spellcheck.build' => 'false', 'spellcheck.reload' => 'true', - 'spellcheck.dictionary' => 'testdict', + 'spellcheck.dictionary' => ['testdict'], + 'spellcheck.count' => 3, + 'spellcheck.onlyMorePopular' => 'false', + 'spellcheck.extendedResults' => 'true', + 'spellcheck.collate' => 'true', + 'spellcheck.maxCollations' => 2, + 'spellcheck.maxCollationTries' => 4, + 'spellcheck.maxCollationEvaluations' => 4, + 'spellcheck.collateExtendedResults' => 'true', + 'spellcheck.accuracy' => .2, + 'spellcheck.collateParam.mm' => '100%', + ], + $request->getParams() + ); + } + + public function testBuildComponentMulipleDictionaries() + { + $builder = new RequestBuilder(); + $request = new Request(); + + $component = new Component(); + $component->setQuery('testquery'); + $component->setBuild(false); + $component->setReload(true); + $component->setDictionary(['dictionary', 'alt_dictionary']); + $component->setCount(3); + $component->setOnlyMorePopular(false); + $component->setExtendedResults(true); + $component->setCollate(true); + $component->setMaxCollations(2); + $component->setMaxCollationTries(4); + $component->setMaxCollationEvaluations(4); + $component->setCollateExtendedResults(true); + $component->setAccuracy(.2); + $component->setCollateParam('mm', '100%'); + + $request = $builder->buildComponent($component, $request); + + $this->assertEquals( + [ + 'spellcheck' => 'true', + 'spellcheck.q' => 'testquery', + 'spellcheck.build' => 'false', + 'spellcheck.reload' => 'true', + 'spellcheck.dictionary' => ['dictionary', 'alt_dictionary'], 'spellcheck.count' => 3, 'spellcheck.onlyMorePopular' => 'false', 'spellcheck.extendedResults' => 'true', diff --git a/tests/Component/SpellcheckTest.php b/tests/Component/SpellcheckTest.php index dbacdb723..7fca0411b 100644 --- a/tests/Component/SpellcheckTest.php +++ b/tests/Component/SpellcheckTest.php @@ -86,7 +86,7 @@ public function testSetAndGetDictionary() $this->spellCheck->setDictionary($value); $this->assertEquals( - $value, + [$value], $this->spellCheck->getDictionary() ); } diff --git a/tests/Integration/AbstractTechproductsTest.php b/tests/Integration/AbstractTechproductsTest.php index fdf67b7bc..97db995fc 100644 --- a/tests/Integration/AbstractTechproductsTest.php +++ b/tests/Integration/AbstractTechproductsTest.php @@ -140,6 +140,17 @@ public function testFacetHighlightSpellcheckComponent() 'card', ], $words); + $spellcheck->setDictionary(['default', 'wordbreak']); + + $result = $this->client->select($select); + $this->assertSame(0, $result->getNumFound()); + + $this->assertSame([ + 'power' => 'power', + 'cort' => 'cord', + ], + $result->getSpellcheck()->getCollations()[0]->getCorrections()); + $select->setQuery('power cord'); // Activate highlighting. $select->getHighlighting(); diff --git a/tests/QueryType/Spellcheck/QueryTest.php b/tests/QueryType/Spellcheck/QueryTest.php index c426d9a7a..314162724 100644 --- a/tests/QueryType/Spellcheck/QueryTest.php +++ b/tests/QueryType/Spellcheck/QueryTest.php @@ -50,7 +50,7 @@ public function testSetAndGetDictionary() $this->query->setDictionary($value); $this->assertSame( - $value, + [$value], $this->query->getDictionary() ); } diff --git a/tests/QueryType/Spellcheck/RequestBuilderTest.php b/tests/QueryType/Spellcheck/RequestBuilderTest.php index 54784c5b8..1539d6f97 100644 --- a/tests/QueryType/Spellcheck/RequestBuilderTest.php +++ b/tests/QueryType/Spellcheck/RequestBuilderTest.php @@ -40,7 +40,7 @@ public function testBuildParams() [ 'spellcheck' => 'true', 'spellcheck.q' => 'ap ip', - 'spellcheck.dictionary' => 'suggest', + 'spellcheck.dictionary' => ['suggest'], 'spellcheck.count' => 13, 'spellcheck.onlyMorePopular' => 'true', 'spellcheck.collate' => 'true',