Skip to content

Commit

Permalink
support multiple spellcheck dictionaries (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kalkbrenner committed Apr 18, 2019
1 parent 025639f commit 7f28d2f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 28 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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()
Expand Down
33 changes: 18 additions & 15 deletions src/Component/ComponentTraits/SpellcheckTrait.php
Expand Up @@ -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');
}
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
8 changes: 4 additions & 4 deletions src/Component/SpellcheckInterface.php
Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions src/Core/Query/Helper.php
Expand Up @@ -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.'}';
}

Expand Down
47 changes: 46 additions & 1 deletion tests/Component/RequestBuilder/SpellcheckTest.php
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/SpellcheckTest.php
Expand Up @@ -86,7 +86,7 @@ public function testSetAndGetDictionary()
$this->spellCheck->setDictionary($value);

$this->assertEquals(
$value,
[$value],
$this->spellCheck->getDictionary()
);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/AbstractTechproductsTest.php
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryType/Spellcheck/QueryTest.php
Expand Up @@ -50,7 +50,7 @@ public function testSetAndGetDictionary()
$this->query->setDictionary($value);

$this->assertSame(
$value,
[$value],
$this->query->getDictionary()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryType/Spellcheck/RequestBuilderTest.php
Expand Up @@ -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',
Expand Down

0 comments on commit 7f28d2f

Please sign in to comment.