Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of terms set query #2020

Merged
merged 2 commits into from
Dec 3, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `bytes` processor [#2008](https://github.com/ruflin/Elastica/pull/2008)
* Added `indices_boost` option to `Elastica\Query` [#2018](https://github.com/ruflin/Elastica/pull/2018)
* Added `Elastica\Query\Terms::setBoost()` method to configure boost [#2035](https://github.com/ruflin/Elastica/pull/2035)
* Added `Elastica\Query\TermsSet` query [#2020](https://github.com/ruflin/Elastica/pull/2020)
* Allowed to configure a sub key when adding a param with `Elastica\Param::addParam()` [#2030](https://github.com/ruflin/Elastica/pull/2030)
### Changed
* Triggered deprecation in `Elastica\Result::getType()` method [#2016](https://github.com/ruflin/Elastica/pull/2016)
Expand Down
57 changes: 57 additions & 0 deletions src/Query/TermsSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Elastica\Query;

use Elastica\Exception\InvalidException;
use Elastica\Script\AbstractScript;

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
*/
class TermsSet extends AbstractQuery
{
/**
* @var string
*/
private $field;

/**
* @param array<bool|float|int|string> $terms
* @param AbstractScript|string $minimumShouldMatch
*/
public function __construct(string $field, array $terms, $minimumShouldMatch)
{
if ('' === $field) {
throw new InvalidException('TermsSet field name has to be set');
}

$this->field = $field;
$this->setTerms($terms);

if (\is_string($minimumShouldMatch)) {
$this->setMinimumShouldMatchField($minimumShouldMatch);
} elseif ($minimumShouldMatch instanceof AbstractScript) {
$this->setMinimumShouldMatchScript($minimumShouldMatch);
} else {
throw new \TypeError(\sprintf('Argument 3 passed to "%s()" must be of type %s|string, %s given.', __METHOD__, AbstractScript::class, \is_object($minimumShouldMatch) ? \get_class($minimumShouldMatch) : \gettype($minimumShouldMatch)));
}
}

/**
* @param array<bool|float|int|string> $terms
*/
public function setTerms(array $terms): self
{
return $this->addParam($this->field, $terms, 'terms');
}

public function setMinimumShouldMatchField(string $minimumShouldMatchField): self
{
return $this->addParam($this->field, $minimumShouldMatchField, 'minimum_should_match_field');
}

public function setMinimumShouldMatchScript(AbstractScript $script): self
{
return $this->addParam($this->field, $script->toArray()['script'], 'minimum_should_match_script');
}
}
145 changes: 145 additions & 0 deletions tests/Query/TermsSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Exception\InvalidException;
use Elastica\Query\TermsSet;
use Elastica\Script\Script;
use Elastica\Test\Base as BaseTest;

/**
* @internal
*/
class TermsSetTest extends BaseTest
{
/**
* @group unit
*/
public function testMinimumShouldMatchField(): void
{
$expected = [
'terms_set' => [
'field' => [
'terms' => ['foo', 'bar'],
'minimum_should_match_field' => 'match_field',
],
],
];

$query = new TermsSet('field', ['foo', 'bar'], 'match_field');

$this->assertSame($expected, $query->toArray());
}

/**
* @group unit
*/
public function testEmptyField(): void
{
$this->expectException(InvalidException::class);
$this->expectExceptionMessage('TermsSet field name has to be set');

new TermsSet('', ['foo', 'bar'], 'match_field');
}

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

$index->addDocuments([
new Document(1, ['skills' => ['php', 'js']]),
new Document(2, ['skills' => ['php']]),
new Document(3, ['skills' => ['java']]),
]);

$index->refresh();

$query = new TermsSet('skills', ['php'], new Script('1'));
$resultSet = $index->search($query);

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

$query = new TermsSet('skills', ['php', 'java'], new Script('1'));
$resultSet = $index->search($query);

$this->assertEquals(3, $resultSet->count());

$query = new TermsSet('skills', ['php', 'js'], new Script('2'));
$resultSet = $index->search($query);

$this->assertEquals(1, $resultSet->count());

$query = new TermsSet('skills', ['php', 'java'], new Script('2'));
$resultSet = $index->search($query);

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

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

$index->addDocuments([
new Document(1, ['skill_count' => 2, 'skills' => ['php', 'js']]),
new Document(2, ['skill_count' => 1, 'skills' => ['php']]),
new Document(3, ['skill_count' => 1, 'skills' => ['java']]),
]);

$index->refresh();

$query = new TermsSet('skills', ['php'], 'skill_count');
$resultSet = $index->search($query);

$this->assertEquals(1, $resultSet->count());

$query = new TermsSet('skills', ['php', 'java'], 'skill_count');
$resultSet = $index->search($query);

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

$query = new TermsSet('skills', ['php', 'js'], 'skill_count');
$resultSet = $index->search($query);

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

$query = new TermsSet('skills', ['js', 'c++'], 'skill_count');
$resultSet = $index->search($query);

$this->assertEquals(0, $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 TermsSet('some_numeric_field', ['9876'], new Script('1'));
$resultSet = $index->search($query);
$this->assertEquals(1, $resultSet->count());

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

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