From 1f4c53e10e6a34e8c4d431b558f211be81b3294e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Fri, 25 Sep 2020 11:02:59 +0200 Subject: [PATCH] Replace Match query class by MatchQuery --- src/Query/Match.php | 177 ++------------------------- src/QueryBuilder/DSL/Query.php | 6 +- src/Util.php | 2 +- tests/Query/MatchTest.php | 24 ++-- tests/Query/PostFilterTest.php | 4 +- tests/Query/RescoreTest.php | 12 +- tests/QueryBuilder/DSL/QueryTest.php | 10 +- tests/ReindexTest.php | 4 +- 8 files changed, 38 insertions(+), 201 deletions(-) diff --git a/src/Query/Match.php b/src/Query/Match.php index bef2f3803f..26b467827c 100644 --- a/src/Query/Match.php +++ b/src/Query/Match.php @@ -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 + * This class is for backward compatibility reason. For PHP 8 and above use MatchQuery as Match is reserved. + * + * @author Nicolas Ruflin * - * @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); - } } diff --git a/src/QueryBuilder/DSL/Query.php b/src/QueryBuilder/DSL/Query.php index ac164a5f1d..12a185c778 100644 --- a/src/QueryBuilder/DSL/Query.php +++ b/src/QueryBuilder/DSL/Query.php @@ -20,11 +20,11 @@ use Elastica\Query\HasChild; use Elastica\Query\HasParent; use Elastica\Query\Ids; -use Elastica\Query\Match; use Elastica\Query\MatchAll; use Elastica\Query\MatchNone; use Elastica\Query\MatchPhrase; use Elastica\Query\MatchPhrasePrefix; +use Elastica\Query\MatchQuery; use Elastica\Query\MoreLikeThis; use Elastica\Query\MultiMatch; use Elastica\Query\Nested; @@ -72,9 +72,9 @@ public function getType(): string * * @param mixed $values */ - public function match(?string $field = null, $values = null): Match + public function match(?string $field = null, $values = null): MatchQuery { - return new Match($field, $values); + return new MatchQuery($field, $values); } /** diff --git a/src/Util.php b/src/Util.php index 5e3f68ed82..8c8297e69b 100644 --- a/src/Util.php +++ b/src/Util.php @@ -221,7 +221,7 @@ public static function getParamName($class) $parts = \explode('\\', $class); $last = \array_pop($parts); - $last = \preg_replace('/Query$/', '', $last); // for BoolQuery + $last = \preg_replace('/Query$/', '', $last); // for BoolQuery and MatchQuery return self::toSnakeCase($last); } diff --git a/tests/Query/MatchTest.php b/tests/Query/MatchTest.php index ee1676a781..6fe9b97791 100644 --- a/tests/Query/MatchTest.php +++ b/tests/Query/MatchTest.php @@ -3,7 +3,7 @@ namespace Elastica\Test\Query; use Elastica\Document; -use Elastica\Query\Match; +use Elastica\Query\MatchQuery; use Elastica\Test\Base as BaseTest; /** @@ -27,7 +27,7 @@ public function testToArray(): void $prefixLength = 3; $maxExpansions = 12; - $query = new Match(); + $query = new MatchQuery(); $query->setFieldQuery($field, $testQuery); $this->hideDeprecated(); $this->showDeprecated(); @@ -80,7 +80,7 @@ public function testMatch(): void $field = 'name'; $operator = 'or'; - $query = new Match(); + $query = new MatchQuery(); $query->setFieldQuery($field, 'Basel New'); $query->setFieldOperator($field, $operator); @@ -110,7 +110,7 @@ public function testMatchSetFieldBoost(): void $field = 'name'; $operator = 'or'; - $query = new Match(); + $query = new MatchQuery(); $query->setFieldQuery($field, 'Basel New'); $query->setFieldOperator($field, $operator); $query->setFieldBoost($field, 1.2); @@ -141,7 +141,7 @@ public function testMatchSetFieldBoostWithString(): void $field = 'name'; $operator = 'or'; - $query = new Match(); + $query = new MatchQuery(); $query->setFieldQuery($field, 'Basel New'); $query->setFieldOperator($field, $operator); $query->setFieldBoost($field, '1.2'); @@ -167,9 +167,9 @@ public function testMatchZeroTerm(): void $index->refresh(); - $query = new Match(); + $query = new MatchQuery(); $query->setFieldQuery('name', ''); - $query->setFieldZeroTermsQuery('name', Match::ZERO_TERM_ALL); + $query->setFieldZeroTermsQuery('name', MatchQuery::ZERO_TERM_ALL); $resultSet = $index->search($query); @@ -182,7 +182,7 @@ public function testMatchZeroTerm(): void public function testMatchFuzzinessType(): void { $field = 'test'; - $query = new Match(); + $query = new MatchQuery(); $fuzziness = 'AUTO'; $query->setFieldFuzziness($field, $fuzziness); @@ -202,14 +202,14 @@ public function testMatchFuzzinessType(): void */ public function testConstruct(): void { - $match = new Match(null, 'values'); + $match = new MatchQuery(null, 'values'); $this->assertEquals(['match' => []], $match->toArray()); - $match = new Match('field', null); + $match = new MatchQuery('field', null); $this->assertEquals(['match' => []], $match->toArray()); - $match1 = new Match('field', 'values'); - $match2 = new Match(); + $match1 = new MatchQuery('field', 'values'); + $match2 = new MatchQuery(); $match2->setField('field', 'values'); $this->assertEquals($match1->toArray(), $match2->toArray()); } diff --git a/tests/Query/PostFilterTest.php b/tests/Query/PostFilterTest.php index d800cf0b20..483d65d54d 100644 --- a/tests/Query/PostFilterTest.php +++ b/tests/Query/PostFilterTest.php @@ -4,7 +4,7 @@ use Elastica\Document; use Elastica\Query; -use Elastica\Query\Match; +use Elastica\Query\MatchQuery; use Elastica\Query\Term; use Elastica\Test\Base as BaseTest; @@ -36,7 +36,7 @@ public function testQuery(): void { $query = new Query(); - $match = new Match(); + $match = new MatchQuery(); $match->setField('make', 'ford'); $query->setQuery($match); diff --git a/tests/Query/RescoreTest.php b/tests/Query/RescoreTest.php index f2cfdb5b7e..9c528cce9b 100644 --- a/tests/Query/RescoreTest.php +++ b/tests/Query/RescoreTest.php @@ -3,7 +3,7 @@ namespace Elastica\Test\Query; use Elastica\Query; -use Elastica\Query\Match; +use Elastica\Query\MatchQuery; use Elastica\Query\Term; use Elastica\Rescore\Query as QueryRescore; use Elastica\Test\Base as BaseTest; @@ -19,7 +19,7 @@ class RescoreTest extends BaseTest public function testToArray(): void { $query = new Query(); - $mainQuery = new Match(); + $mainQuery = new MatchQuery(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery = new Term(); $secQuery = $secQuery->setTerm('test2', 'bar', 2); @@ -59,7 +59,7 @@ public function testToArray(): void public function testSetSize(): void { $query = new Query(); - $mainQuery = new Match(); + $mainQuery = new MatchQuery(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery = new Term(); $secQuery = $secQuery->setTerm('test2', 'bar', 2); @@ -101,7 +101,7 @@ public function testSetSize(): void public function testSetWeights(): void { $query = new Query(); - $mainQuery = new Match(); + $mainQuery = new MatchQuery(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery = new Term(); $secQuery = $secQuery->setTerm('test2', 'bar', 2); @@ -147,7 +147,7 @@ public function testSetWeights(): void public function testMultipleQueries(): void { $query = new Query(); - $mainQuery = new Match(); + $mainQuery = new MatchQuery(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery1 = new Term(); @@ -219,7 +219,7 @@ public function testMultipleQueries(): void public function testQuery(): void { $query = new Query(); - $mainQuery = new Match(); + $mainQuery = new MatchQuery(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery = new Term(); $secQuery = $secQuery->setTerm('test2', 'bar', 2); diff --git a/tests/QueryBuilder/DSL/QueryTest.php b/tests/QueryBuilder/DSL/QueryTest.php index 8829f0cfb9..0129bec69d 100644 --- a/tests/QueryBuilder/DSL/QueryTest.php +++ b/tests/QueryBuilder/DSL/QueryTest.php @@ -3,7 +3,7 @@ namespace Elastica\Test\QueryBuilder\DSL; use Elastica\Query; -use Elastica\Query\Match; +use Elastica\Query\MatchQuery; use Elastica\QueryBuilder\DSL; /** @@ -31,7 +31,7 @@ public function testMatch(): void $match = $queryDSL->match('field', 'match'); $this->assertEquals('match', $match->getParam('field')); - $this->assertInstanceOf(Match::class, $match); + $this->assertInstanceOf(MatchQuery::class, $match); } /** @@ -48,10 +48,10 @@ public function testInterface(): void $this->_assertImplemented($queryDSL, 'distance_feature', Query\DistanceFeature::class, ['field', 'now', '7d']); $this->_assertImplemented($queryDSL, 'function_score', Query\FunctionScore::class, []); $this->_assertImplemented($queryDSL, 'fuzzy', Query\Fuzzy::class, ['field', 'type']); - $this->_assertImplemented($queryDSL, 'has_child', Query\HasChild::class, [new Match()]); - $this->_assertImplemented($queryDSL, 'has_parent', Query\HasParent::class, [new Match(), 'type']); + $this->_assertImplemented($queryDSL, 'has_child', Query\HasChild::class, [new MatchQuery()]); + $this->_assertImplemented($queryDSL, 'has_parent', Query\HasParent::class, [new MatchQuery(), 'type']); $this->_assertImplemented($queryDSL, 'ids', Query\Ids::class, [[]]); - $this->_assertImplemented($queryDSL, 'match', Match::class, ['field', 'values']); + $this->_assertImplemented($queryDSL, 'match', MatchQuery::class, ['field', 'values']); $this->_assertImplemented($queryDSL, 'match_all', Query\MatchAll::class, []); $this->_assertImplemented($queryDSL, 'match_none', Query\MatchNone::class, []); $this->_assertImplemented($queryDSL, 'more_like_this', Query\MoreLikeThis::class, []); diff --git a/tests/ReindexTest.php b/tests/ReindexTest.php index 4ac4b4d848..d9023fa256 100644 --- a/tests/ReindexTest.php +++ b/tests/ReindexTest.php @@ -8,7 +8,7 @@ use Elastica\Pipeline; use Elastica\Processor\Rename; use Elastica\Processor\Uppercase; -use Elastica\Query\Match; +use Elastica\Query\MatchQuery; use Elastica\Reindex; use Elastica\Script\Script; @@ -98,7 +98,7 @@ public function testReindexWithQueryOption(): void $newIndex = $this->_createIndex('idx2', true, 2); - $query = new Match('id', 8); + $query = new MatchQuery('id', 8); $reindex = new Reindex($oldIndex, $newIndex, [ Reindex::QUERY => $query,