Skip to content

Commit

Permalink
[PHP 8.0] Deprecate Match query class and introduce MatchQuery (#1799)
Browse files Browse the repository at this point in the history
  • Loading branch information
deguif authored Sep 28, 2020
1 parent 39ecb49 commit 66dc2ff
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 202 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791)
* Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791)
### Deprecated
* Deprecated Match query class and introduced MatchQuery instead for PHP 8.0 compatibility reason [#1799](https://github.com/ruflin/Elastica/pull/1799)
### Removed
### Fixed
* fixed issue [1789](https://github.com/ruflin/Elastica/issues/1789)
Expand Down
177 changes: 7 additions & 170 deletions src/Query/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,180 +2,17 @@

namespace Elastica\Query;

\trigger_error('Elastica\Query\Match is deprecated. Use Elastica\Query\MatchQuery instead. From PHP 8 match is reserved word and this class will be removed in further Elastica releases', \E_USER_DEPRECATED);

/**
* Match query.
*
* @author F21
* @author WONG Wing Lun <luiges90@gmail.com>
* This class is for backward compatibility reason. For PHP 8 and above use MatchQuery as Match is reserved.
*
* @author Nicolas Ruflin <spam@ruflin.com>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
* @deprecated Use MatchQuery instead. From PHP 8 match is reserved word and this class will be removed in further Elastica releases
*/
class Match extends AbstractQuery
class Match extends MatchQuery
{
public const OPERATOR_OR = 'or';
public const OPERATOR_AND = 'and';

public const ZERO_TERM_NONE = 'none';
public const ZERO_TERM_ALL = 'all';

public const FUZZINESS_AUTO = 'AUTO';

/**
* @param string $field
* @param mixed $values
*/
public function __construct(?string $field = null, $values = null)
{
if (null !== $field && null !== $values) {
$this->setParam($field, $values);
}
}

/**
* Sets a param for the message array.
*
* @param mixed $values
*
* @return $this
*/
public function setField(string $field, $values): self
{
return $this->setParam($field, $values);
}

/**
* Sets a param for the given field.
*
* @return $this
*/
public function setFieldParam(string $field, string $key, string $value): self
{
if (!isset($this->_params[$field])) {
$this->_params[$field] = [];
}

$this->_params[$field][$key] = $value;

return $this;
}

/**
* Sets the query string.
*
* @return $this
*/
public function setFieldQuery(string $field, string $query): self
{
return $this->setFieldParam($field, 'query', $query);
}

/**
* Set field operator.
*
* @return $this
*/
public function setFieldOperator(string $field, string $operator = self::OPERATOR_OR): self
{
return $this->setFieldParam($field, 'operator', $operator);
}

/**
* Set field analyzer.
*
* @return $this
*/
public function setFieldAnalyzer(string $field, string $analyzer): self
{
return $this->setFieldParam($field, 'analyzer', $analyzer);
}

/**
* Set field boost value.
*
* If not set, defaults to 1.0.
*
* @return $this
*/
public function setFieldBoost(string $field, float $boost = 1.0): self
{
return $this->setFieldParam($field, 'boost', $boost);
}

/**
* Set field minimum should match.
*
* @param int|string $minimumShouldMatch
*
* @return $this
*
* @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
*/
public function setFieldMinimumShouldMatch(string $field, $minimumShouldMatch): self
{
return $this->setFieldParam($field, 'minimum_should_match', $minimumShouldMatch);
}

/**
* Set field fuzziness.
*
* @param mixed $fuzziness
*
* @return $this
*/
public function setFieldFuzziness(string $field, $fuzziness): self
{
return $this->setFieldParam($field, 'fuzziness', $fuzziness);
}

/**
* Set field fuzzy rewrite.
*
* @return $this
*/
public function setFieldFuzzyRewrite(string $field, string $fuzzyRewrite): self
{
return $this->setFieldParam($field, 'fuzzy_rewrite', $fuzzyRewrite);
}

/**
* Set field prefix length.
*
* @return $this
*/
public function setFieldPrefixLength(string $field, int $prefixLength): self
{
return $this->setFieldParam($field, 'prefix_length', $prefixLength);
}

/**
* Set field max expansions.
*
* @return $this
*/
public function setFieldMaxExpansions(string $field, int $maxExpansions): self
{
return $this->setFieldParam($field, 'max_expansions', $maxExpansions);
}

/**
* Set zero terms query.
*
* If not set, default to 'none'
*
* @return $this
*/
public function setFieldZeroTermsQuery(string $field, string $zeroTermQuery = self::ZERO_TERM_NONE): self
{
return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
}

/**
* Set cutoff frequency.
*
* @return $this
*/
public function setFieldCutoffFrequency(string $field, float $cutoffFrequency): self
{
return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
}
}
181 changes: 181 additions & 0 deletions src/Query/MatchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace Elastica\Query;

/**
* Match query.
*
* @author F21
* @author WONG Wing Lun <luiges90@gmail.com>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
*/
class MatchQuery extends AbstractQuery
{
public const OPERATOR_OR = 'or';
public const OPERATOR_AND = 'and';

public const ZERO_TERM_NONE = 'none';
public const ZERO_TERM_ALL = 'all';

public const FUZZINESS_AUTO = 'AUTO';

/**
* @param string $field
* @param mixed $values
*/
public function __construct(?string $field = null, $values = null)
{
if (null !== $field && null !== $values) {
$this->setParam($field, $values);
}
}

/**
* Sets a param for the message array.
*
* @param mixed $values
*
* @return $this
*/
public function setField(string $field, $values): self
{
return $this->setParam($field, $values);
}

/**
* Sets a param for the given field.
*
* @return $this
*/
public function setFieldParam(string $field, string $key, string $value): self
{
if (!isset($this->_params[$field])) {
$this->_params[$field] = [];
}

$this->_params[$field][$key] = $value;

return $this;
}

/**
* Sets the query string.
*
* @return $this
*/
public function setFieldQuery(string $field, string $query): self
{
return $this->setFieldParam($field, 'query', $query);
}

/**
* Set field operator.
*
* @return $this
*/
public function setFieldOperator(string $field, string $operator = self::OPERATOR_OR): self
{
return $this->setFieldParam($field, 'operator', $operator);
}

/**
* Set field analyzer.
*
* @return $this
*/
public function setFieldAnalyzer(string $field, string $analyzer): self
{
return $this->setFieldParam($field, 'analyzer', $analyzer);
}

/**
* Set field boost value.
*
* If not set, defaults to 1.0.
*
* @return $this
*/
public function setFieldBoost(string $field, float $boost = 1.0): self
{
return $this->setFieldParam($field, 'boost', $boost);
}

/**
* Set field minimum should match.
*
* @param int|string $minimumShouldMatch
*
* @return $this
*
* @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
*/
public function setFieldMinimumShouldMatch(string $field, $minimumShouldMatch): self
{
return $this->setFieldParam($field, 'minimum_should_match', $minimumShouldMatch);
}

/**
* Set field fuzziness.
*
* @param mixed $fuzziness
*
* @return $this
*/
public function setFieldFuzziness(string $field, $fuzziness): self
{
return $this->setFieldParam($field, 'fuzziness', $fuzziness);
}

/**
* Set field fuzzy rewrite.
*
* @return $this
*/
public function setFieldFuzzyRewrite(string $field, string $fuzzyRewrite): self
{
return $this->setFieldParam($field, 'fuzzy_rewrite', $fuzzyRewrite);
}

/**
* Set field prefix length.
*
* @return $this
*/
public function setFieldPrefixLength(string $field, int $prefixLength): self
{
return $this->setFieldParam($field, 'prefix_length', $prefixLength);
}

/**
* Set field max expansions.
*
* @return $this
*/
public function setFieldMaxExpansions(string $field, int $maxExpansions): self
{
return $this->setFieldParam($field, 'max_expansions', $maxExpansions);
}

/**
* Set zero terms query.
*
* If not set, default to 'none'
*
* @return $this
*/
public function setFieldZeroTermsQuery(string $field, string $zeroTermQuery = self::ZERO_TERM_NONE): self
{
return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
}

/**
* Set cutoff frequency.
*
* @return $this
*/
public function setFieldCutoffFrequency(string $field, float $cutoffFrequency): self
{
return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
}
}
Loading

0 comments on commit 66dc2ff

Please sign in to comment.