Skip to content
Snippets Groups Projects
Commit abc79f35 authored by luciansabo's avatar luciansabo Committed by Simonas Šerlinskas
Browse files

added new NestedSort for replacing deprecated nested_filter and nested_path (#267)

* added new NestedSort for replacing deprecated nested_filter and nested_path

* made the filter optional
parent 73a1b123
No related branches found
No related tags found
No related merge requests found
......@@ -123,7 +123,7 @@ class FieldSort implements BuilderInterface
}
if ($this->nestedFilter) {
$this->addParameter('nested_filter', $this->nestedFilter->toArray());
$this->addParameter('nested', $this->nestedFilter->toArray());
}
$output = [
......
<?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\Sort;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "nested" sort filter.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/filter-dsl-nested-filter.html
*/
class NestedSort implements BuilderInterface
{
use ParametersTrait;
/**
* @var string
*/
private $path;
/**
* @var BuilderInterface
*/
private $filter;
/**
* @var BuilderInterface
*/
private $nestedFilter;
/**
* @param string $path
* @param BuilderInterface $filter
* @param array $parameters
*/
public function __construct(
$path,
BuilderInterface $filter = null,
array $parameters = []
) {
$this->path = $path;
$this->filter = $filter;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'nested';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$output = [
'path' => $this->path,
];
if ($this->filter) {
$output['filter'] = $this->filter->toArray();
}
if ($this->nestedFilter) {
$output[$this->getType()] = $this->nestedFilter->toArray();
}
return $this->processArray($output);
}
/**
* Returns nested filter object.
*
* @return BuilderInterface
*/
public function getFilter()
{
return $this->filter;
}
/**
* Returns path this filter is set for.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return BuilderInterface
*/
public function getNestedFilter()
{
return $this->nestedFilter;
}
/**
* @param BuilderInterface $nestedFilter
*/
public function setNestedFilter(BuilderInterface $nestedFilter)
{
$this->nestedFilter = $nestedFilter;
}
}
<?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\Sort;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use ONGR\ElasticsearchDSL\Sort\NestedSort;
class FieldSortTest extends \PHPUnit\Framework\TestCase
{
/**
* Test for toArray() method.
*
*/
public function testToArray()
{
$nestedFilter = new NestedSort('somePath', new TermQuery('somePath.id', 10));
$sort = new FieldSort('someField', 'asc');
$sort->setNestedFilter($nestedFilter);
$expected = [
'someField' => [
'nested' => [
'path' => 'somePath',
'filter' => [
'term' => [
'somePath.id' => 10,
]
]
],
'order' => 'asc'
],
];
$result = $sort->toArray();
$this->assertEquals($expected, $result);
}
}
<?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\Sort;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Sort\NestedSort;
class NestedSortTest extends \PHPUnit\Framework\TestCase
{
/**
* Test for single nested.
*
*/
public function testSingle()
{
$query = new NestedSort('somePath', new TermQuery('somePath.id', 10));
$expected = [
'path' => 'somePath',
'filter' => [
'term' => [
'somePath.id' => 10,
]
]
];
$result = $query->toArray();
$this->assertEquals($expected, $result);
}
/**
* Test for single nested, no filter.
*
*/
public function testNoFilter()
{
$query = new NestedSort('somePath');
$expected = [
'path' => 'somePath',
];
$result = $query->toArray();
$this->assertEquals($expected, $result);
}
/**
* Test for single nested.
*
*/
public function testMultipleNesting()
{
$query = new NestedSort('somePath', new TermQuery('somePath.id', 10));
$nestedFilter1 = new NestedSort('secondPath', new TermQuery('secondPath.foo', 'bar'));
$nestedFilter2 = new NestedSort('thirdPath', new TermQuery('thirdPath.x', 'y'));
$nestedFilter1->setNestedFilter($nestedFilter2);
$query->setNestedFilter($nestedFilter1);
$expected = [
'path' => 'somePath',
'filter' => [
'term' => [
'somePath.id' => 10,
]
],
'nested' => [
'path' => 'secondPath',
'filter' => [
'term' => [
'secondPath.foo' => 'bar',
]
],
'nested' => [
'path' => 'thirdPath',
'filter' => [
'term' => [
'thirdPath.x' => 'y',
]
],
]
]
];
$result = $query->toArray();
$this->assertEquals($expected, $result);
}
}
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