Skip to content
Snippets Groups Projects
Unverified Commit 59f9e445 authored by Milroy E. Fraser's avatar Milroy E. Fraser Committed by Simonas Šerlinskas
Browse files

Implemented TermsSetQuery

parent 7ddc361f
No related branches found
No related tags found
No related merge requests found
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Query\TermLevel;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "terms_set" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
*/
class TermsSetQuery implements BuilderInterface
{
use ParametersTrait;
const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field';
const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script';
/**
* @var string
*/
private $field;
/**
* @var array
*/
private $terms;
/**
* Constructor.
*
* @param string $field Field name
* @param array $terms An array of terms
* @param array $parameters Parameters
*/
public function __construct($field, $terms, array $parameters)
{
$this->field = $field;
$this->terms = $terms;
$this->validateParameters($parameters);
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'terms_set';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$query = [
'terms' => $this->terms,
];
return [$this->getType() => [
$this->field => $this->processArray($query),
]];
}
private function validateParameters(array $parameters)
{
if (
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) &&
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT])
) {
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
throw new \InvalidArgumentException($message);
}
}
}
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery;
class TermsSetQueryTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests toArray().
*/
public function testToArray()
{
$terms = ['php', 'c++', 'java'];
$parameters = ['minimum_should_match_field' => 'required_matches'];
$query = new TermsSetQuery('programming_languages', $terms, $parameters);
$expected = [
'terms_set' => [
'programming_languages' => [
'terms' => ['php', 'c++', 'java'],
'minimum_should_match_field' => 'required_matches',
]
],
];
$this->assertEquals($expected, $query->toArray());
}
public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven()
{
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);
$terms = ['php', 'c++', 'java'];
new TermsSetQuery('programming_languages', $terms, []);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment