Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed 3rd `$method` argument from the `Elastica\SearchableInterface::search` and `Elastica\SearchableInterface::count` as they are not unused anymore. The following classes are affected: `Elastica\Search` and `Elastica\Index` [#2256](https://github.com/ruflin/Elastica/pull/2256)
* Removed `Elastica\Request` class as constants are not used anymore and no longer needed. [#2256](https://github.com/ruflin/Elastica/pull/2256)
### Added
* `Elastica\Query\Fuzzy::setRewrite` rewrite param setter and possible values as constants [#2242](https://github.com/ruflin/Elastica/pull/2242)
* `Elastica\Query\Prefix::setRewrite` rewrite param setter and possible values as constants [#2242](https://github.com/ruflin/Elastica/pull/2242)
* `Elastica\Query\Regexp::setRewrite` rewrite param setter and possible values as constants [#2242](https://github.com/ruflin/Elastica/pull/2242)
* Add constants for all possible rewrite values in `Elastica\Query\Wildcard` [#2242](https://github.com/ruflin/Elastica/pull/2242)
* Added support for PHP 8.5 [#2253](https://github.com/ruflin/Elastica/pull/2253)

### Changed
* Upgrade PHPUnit from 9.5 to 10.5 [#2251](https://github.com/ruflin/Elastica/pull/2251)
* Update phpstan to 2.x [#2252](https://github.com/ruflin/Elastica/pull/2252)
Expand Down
23 changes: 23 additions & 0 deletions src/Query/Fuzzy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
*/
class Fuzzy extends AbstractQuery
{
/**
* Rewrite methods: @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html.
*/
public const REWRITE_CONSTANT_SCORE = 'constant_score';
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these const always the same. I wonder if we should define these once in AbstractQuery. But then, they don't apply to all queries. What is the pattern of all the queries this applies to? Should we have an abstract query or similar that all these extend?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These constants are always the same. The only pattern between these, is that they are all Term-level queries that use wildcards in some shape or form.

When writing the PR I was also debating puttings the constants in AbstractQuery but reached the same conclusion.

I guess we could have a new level of abstract query for these. Not sure how to name it though.

public const REWRITE_CONSTANT_SCORE_BOOLEAN = 'constant_score_boolean';
public const REWRITE_SCORING_BOOLEAN = 'scoring_boolean';
public const REWRITE_TOP_TERMS_BLENDED_FREQS_N = 'top_terms_blended_freqs_N';
public const REWRITE_TOP_TERMS_BOOST_N = 'top_terms_boost_N';
public const REWRITE_TOP_TERMS_N = 'top_terms_N';

/**
* Construct a fuzzy query.
*
Expand Down Expand Up @@ -62,4 +72,17 @@ public function setFieldOption(string $option, $value): self

return $this->setParam($key, $params[$key]);
}

/**
* Set the method used to rewrite the query.
* Use one of the Fuzzy::REWRITE_* constants, or provide your own.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html
*/
public function setRewrite(string $rewriteMode): self
{
$this->setFieldOption('rewrite', $rewriteMode);

return $this;
}
}
37 changes: 37 additions & 0 deletions src/Query/Prefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@

namespace Elastica\Query;

use Elastica\Exception\InvalidException;

/**
* Prefix query.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
*/
class Prefix extends AbstractQuery
{
/**
* Rewrite methods: @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html.
*/
public const REWRITE_CONSTANT_SCORE = 'constant_score';
public const REWRITE_CONSTANT_SCORE_BOOLEAN = 'constant_score_boolean';
public const REWRITE_SCORING_BOOLEAN = 'scoring_boolean';
public const REWRITE_TOP_TERMS_BLENDED_FREQS_N = 'top_terms_blended_freqs_N';
public const REWRITE_TOP_TERMS_BOOST_N = 'top_terms_boost_N';
public const REWRITE_TOP_TERMS_N = 'top_terms_N';

/**
* @var string|null
*/
private $field;

/**
* @param array $prefix OPTIONAL Calls setRawPrefix with the given $prefix array
*/
Expand All @@ -29,6 +46,8 @@ public function __construct(array $prefix = [])
*/
public function setRawPrefix(array $prefix): self
{
$this->field = \array_key_first($prefix);

return $this->setParams($prefix);
}

Expand All @@ -45,4 +64,22 @@ public function setPrefix(string $key, $value, float $boost = 1.0): self
{
return $this->setRawPrefix([$key => ['value' => $value, 'boost' => $boost]]);
}

/**
* Set the method used to rewrite the query.
* Use one of the Prefix::REWRITE_* constants, or provide your own.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html
*/
public function setRewrite(string $rewriteMode): self
{
if (null === $this->field) {
throw new InvalidException('No field has been set');
}

$data = $this->getParam($this->field);
$this->setParam($this->field, \array_merge($data, ['rewrite' => $rewriteMode]));

return $this;
}
}
39 changes: 38 additions & 1 deletion src/Query/Regexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Elastica\Query;

use Elastica\Exception\InvalidException;

/**
* Regexp query.
*
Expand All @@ -13,6 +15,21 @@
*/
class Regexp extends AbstractQuery
{
/**
* Rewrite methods: @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html.
*/
public const REWRITE_CONSTANT_SCORE = 'constant_score';
public const REWRITE_CONSTANT_SCORE_BOOLEAN = 'constant_score_boolean';
public const REWRITE_SCORING_BOOLEAN = 'scoring_boolean';
public const REWRITE_TOP_TERMS_BLENDED_FREQS_N = 'top_terms_blended_freqs_N';
public const REWRITE_TOP_TERMS_BOOST_N = 'top_terms_boost_N';
public const REWRITE_TOP_TERMS_N = 'top_terms_N';

/**
* @var string|null
*/
private $field;

/**
* Construct regexp query.
*
Expand All @@ -32,8 +49,28 @@ public function __construct(string $key = '', ?string $value = null, float $boos
*
* @return $this
*/
public function setValue(string $key, ?string $value = null, float $boost = 1.0)
public function setValue(string $key, ?string $value = null, float $boost = 1.0): self
{
$this->field = $key;

return $this->setParam($key, ['value' => $value, 'boost' => $boost]);
}

/**
* Set the method used to rewrite the query.
* Use one of the Regexp::REWRITE_* constants, or provide your own.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html
*/
public function setRewrite(string $rewriteMode): self
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods are always identical?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same for Prefix and Regexp, also for Wildcard except the null check on $this->field since it's set in the constructor.
For Fuzzy I used the setFieldOption shortcut since it was already present but it can rewritten the same as the others.

IMHO, these queries could use a refactor in the long run, to harmonize them and have consistent constructors.
Right now, Prefix takes an array, the others a field and value, but some are nullable and some are not.
It would create breaking changes though.

{
if (null === $this->field) {
throw new InvalidException('No field has been set');
}

$data = $this->getParam($this->field);
$this->setParam($this->field, \array_merge($data, ['rewrite' => $rewriteMode]));

return $this;
}
}
3 changes: 3 additions & 0 deletions src/Query/Wildcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Wildcard extends AbstractQuery
public const REWRITE_CONSTANT_SCORE = 'constant_score';
public const REWRITE_CONSTANT_SCORE_BOOLEAN = 'constant_score_boolean';
public const REWRITE_SCORING_BOOLEAN = 'scoring_boolean';
public const REWRITE_TOP_TERMS_BLENDED_FREQS_N = 'top_terms_blended_freqs_N';
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that we had some of the const already

public const REWRITE_TOP_TERMS_BOOST_N = 'top_terms_boost_N';
public const REWRITE_TOP_TERMS_N = 'top_terms_N';

/**
* @var string
Expand Down
2 changes: 2 additions & 0 deletions tests/Query/FuzzyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public function testToArray(): void

$fuzzy->setField('user', 'Nicolas');
$fuzzy->setFieldOption('boost', 1.0);
$fuzzy->setRewrite(Fuzzy::REWRITE_SCORING_BOOLEAN);

$expectedArray = [
'fuzzy' => [
'user' => [
'value' => 'Nicolas',
'boost' => 1.0,
'rewrite' => 'scoring_boolean',
],
],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/Query/PrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public function testToArray(): void
$value = 'ni';
$boost = 2;
$query->setPrefix($key, $value, $boost);
$query->setRewrite(Prefix::REWRITE_SCORING_BOOLEAN);

$data = $query->toArray();

$this->assertIsArray($data['prefix']);
$this->assertIsArray($data['prefix'][$key]);
$this->assertEquals($data['prefix'][$key]['value'], $value);
$this->assertEquals($data['prefix'][$key]['boost'], $boost);
$this->assertEquals($data['prefix'][$key]['rewrite'], 'scoring_boolean');
}
}
2 changes: 2 additions & 0 deletions tests/Query/RegexpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public function testToArray(): void
$boost = 2;

$query = new Regexp($field, $value, $boost);
$query->setRewrite(Regexp::REWRITE_SCORING_BOOLEAN);

$expectedArray = [
'regexp' => [
$field => [
'value' => $value,
'boost' => $boost,
'rewrite' => 'scoring_boolean',
],
],
];
Expand Down