Skip to content

Commit

Permalink
Allow the Terms query to accept arrays of strings, ints and floats (#…
Browse files Browse the repository at this point in the history
…1872)

As shown in #1765 (comment) , ElasticSearch accepts all of them

Note: the change of `public function addTerm` was not previously discussed, but is made for consistency, however poses a breaking change if someone extended this class previously => https://3v4l.org/Ws5bM
  • Loading branch information
mfn authored Nov 20, 2020
1 parent 59583f3 commit a323450
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/ruflin/Elastica/compare/7.0.0...master)
### Backward Compatibility Breaks
* Allow the Terms query to accept arrays of strings, ints and floats [#1872](https://github.com/ruflin/Elastica/pull/1872)
### Added
* Ability to specify the type of authentication manually by the `auth_type` parameter (in the client class config) was added (allowed values are `basic, digest, gssnegotiate, ntlm`)
* Added `if_seq_no` / `if_primary_term` to replace `version` for [optimistic concurrency control](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html)
Expand Down
14 changes: 10 additions & 4 deletions src/Query/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Terms extends AbstractQuery
private $field;

/**
* @var string[]
* @var array<float|int|string>
*/
private $terms;

Expand All @@ -30,7 +30,7 @@ class Terms extends AbstractQuery
private $lookup;

/**
* @param string[] $terms Terms list, leave empty if building a terms-lookup query
* @param array<float|int|string> $terms Terms list, leave empty if building a terms-lookup query
*/
public function __construct(string $field, array $terms = [])
{
Expand All @@ -45,7 +45,7 @@ public function __construct(string $field, array $terms = [])
/**
* Sets terms for the query.
*
* @param string[]
* @param array<float|int|string>
*/
public function setTerms(array $terms): self
{
Expand All @@ -56,9 +56,15 @@ public function setTerms(array $terms): self

/**
* Adds a single term to the list.
*
* @param float|int|string $term
*/
public function addTerm(string $term): self
public function addTerm($term): self
{
if (!\is_string($term) && !\is_int($term) && !\is_float($term)) {
throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be of type float|int|string, %s given.', __METHOD__, \is_object($term) ? \get_class($term) : \gettype($term)));
}

$this->terms[] = $term;

return $this;
Expand Down
88 changes: 88 additions & 0 deletions tests/Query/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,92 @@ public function testFilteredSearchWithLookup(): void

$this->assertEquals(2, $resultSet->count());
}

/**
* @group functional
*/
public function testVariousDataTypesViaConstructor(): void
{
$index = $this->_createIndex();

$index->addDocuments([
new Document(1, ['some_numeric_field' => 9876]),
]);
$index->refresh();

// string
$query = new Terms('some_numeric_field', ['9876']);
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());

// int
$query = new Terms('some_numeric_field', [9876]);
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());

// float
$query = new Terms('some_numeric_field', [9876.0]);
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());
}

/**
* @group functional
*/
public function testVariousMixedDataTypesViaConstructor(): void
{
$index = $this->_createIndex();

$index->addDocuments([
new Document(1, ['some_numeric_field' => 9876]),
new Document(2, ['some_numeric_field' => 5678]),
new Document(3, ['some_numeric_field' => 1234]),
new Document(4, ['some_numeric_field' => 8899]),
]);
$index->refresh();

$query = new Terms('some_numeric_field', ['9876', 1234, 5678.0]);
$resultSet = $index->search($query);
$this->assertEquals(3, $resultSet->count());
}

/**
* @group functional
*/
public function testVariousDataTypesViaAddTerm(): void
{
$index = $this->_createIndex();

$index->addDocuments([
new Document(1, ['some_numeric_field' => 9876]),
]);
$index->refresh();

// string
$query = new Terms('some_numeric_field');
$query->addTerm('9876');
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());

// int
$query = new Terms('some_numeric_field');
$query->addTerm(9876);
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());

// float
$query = new Terms('some_numeric_field');
$query->addTerm(9876.0);
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());
}

public function testAddTermTypeError(): void
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('Argument 1 passed to "Elastica\Query\Terms::addTerm()" must be of type float|int|string, stdClass given.');

$query = new Terms('some_numeric_field');
$query->addTerm(new \stdClass());
}
}

0 comments on commit a323450

Please sign in to comment.