Skip to content
Snippets Groups Projects
Commit d71e7291 authored by Simonas Serlinskas's avatar Simonas Serlinskas
Browse files

Merge branch '6.x'

parents 62f765c4 40434bb5
No related branches found
No related tags found
No related merge requests found
...@@ -11,15 +11,18 @@ ...@@ -11,15 +11,18 @@
} }
], ],
"require": { "require": {
"php": ">=7.0", "php": "^7.0",
"symfony/serializer": "~3.0|~4.0", "symfony/serializer": "^3.0|^4.0",
"paragonie/random_compat": "^2.0", "paragonie/random_compat": "^1.0|^2.0"
"elasticsearch/elasticsearch": "~6.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~6.0", "elasticsearch/elasticsearch": "^6.0",
"squizlabs/php_codesniffer": "~3.0", "phpunit/phpunit": "^6.0",
"satooshi/php-coveralls": "~2.0" "squizlabs/php_codesniffer": "^3.0",
"satooshi/php-coveralls": "~1.0|~2.0"
},
"suggest": {
"elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality."
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
...@@ -44,9 +44,7 @@ And now the query via DSL: ...@@ -44,9 +44,7 @@ And now the query via DSL:
```php ```php
$sort = new FieldSort('last_activity_date', FieldSort::DESC); $sort = new FieldSort('last_activity_date', FieldSort::DESC);
$sorts = new Sorts(); $topHitsAggregation = new TopHitsAggregation('top_tag_hits', 1, null, $sort);
$sorts->addSort($sort);
$topHitsAggregation = new TopHitsAggregation('top_tag_hits', 1, null, $sorts);
$topHitsAggregation->addParameter('_source', ['include' => ['title']]); $topHitsAggregation->addParameter('_source', ['include' => ['title']]);
$termsAggregation = new TermsAggregation('top-tags', 'tags'); $termsAggregation = new TermsAggregation('top-tags', 'tags');
......
...@@ -51,7 +51,8 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -51,7 +51,8 @@ class DateRangeAggregation extends AbstractAggregation
foreach ($ranges as $range) { foreach ($ranges as $range) {
$from = isset($range['from']) ? $range['from'] : null; $from = isset($range['from']) ? $range['from'] : null;
$to = isset($range['to']) ? $range['to'] : null; $to = isset($range['to']) ? $range['to'] : null;
$this->addRange($from, $to); $key = isset($range['key']) ? $range['key'] : null;
$this->addRange($from, $to, $key);
} }
} }
...@@ -78,12 +79,13 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -78,12 +79,13 @@ class DateRangeAggregation extends AbstractAggregation
* *
* @throws \LogicException * @throws \LogicException
*/ */
public function addRange($from = null, $to = null) public function addRange($from = null, $to = null, $key = null)
{ {
$range = array_filter( $range = array_filter(
[ [
'from' => $from, 'from' => $from,
'to' => $to, 'to' => $to,
'key' => $key,
] ]
); );
......
...@@ -35,9 +35,9 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -35,9 +35,9 @@ class TopHitsAggregation extends AbstractAggregation
private $from; private $from;
/** /**
* @var BuilderInterface How the top matching hits should be sorted. * @var BuilderInterface[] How the top matching hits should be sorted.
*/ */
private $sort; private $sorts = [];
/** /**
* Constructor for top hits. * Constructor for top hits.
...@@ -52,7 +52,7 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -52,7 +52,7 @@ class TopHitsAggregation extends AbstractAggregation
parent::__construct($name); parent::__construct($name);
$this->setFrom($from); $this->setFrom($from);
$this->setSize($size); $this->setSize($size);
$this->setSort($sort); $this->addSort($sort);
} }
/** /**
...@@ -76,23 +76,29 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -76,23 +76,29 @@ class TopHitsAggregation extends AbstractAggregation
} }
/** /**
* Return sort. * @return BuilderInterface[]
*
* @return BuilderInterface
*/ */
public function getSort() public function getSorts()
{ {
return $this->sort; return $this->sorts;
} }
/** /**
* Set sort. * @param BuilderInterface[] $sorts
*/
public function setSorts(array $sorts)
{
$this->sorts = $sorts;
}
/**
* Add sort.
* *
* @param BuilderInterface $sort * @param BuilderInterface $sort
*/ */
public function setSort($sort) public function addSort($sort)
{ {
$this->sort = $sort; $this->sorts[] = $sort;
} }
/** /**
...@@ -128,9 +134,19 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -128,9 +134,19 @@ class TopHitsAggregation extends AbstractAggregation
*/ */
public function getArray() public function getArray()
{ {
$sortsOutput = [];
$addedSorts = array_filter($this->getSorts());
if ($addedSorts) {
foreach ($addedSorts as $sort) {
$sortsOutput[] = $sort->toArray();
}
} else {
$sortsOutput = null;
}
$output = array_filter( $output = array_filter(
[ [
'sort' => $this->getSort() ? $this->getSort()->toArray() : null, 'sort' => $sortsOutput,
'size' => $this->getSize(), 'size' => $this->getSize(),
'from' => $this->getFrom(), 'from' => $this->getFrom(),
], ],
...@@ -141,4 +157,31 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -141,4 +157,31 @@ class TopHitsAggregation extends AbstractAggregation
return empty($output) ? new \stdClass() : $output; return empty($output) ? new \stdClass() : $output;
} }
/**
* @deprecated sorts now is a container, use `getSorts()`instead.
* Return sort.
*
* @return BuilderInterface
*/
public function getSort()
{
if (isset($this->sorts[0])) {
return $this->sorts[0];
}
return null;
}
/**
* @deprecated sorts now is a container, use `addSort()`instead.
*
* Set sort.
*
* @param BuilderInterface $sort
*/
public function setSort(BuilderInterface $sort)
{
$this->sort = $sort;
}
} }
...@@ -23,6 +23,11 @@ class GeoShapeQuery implements BuilderInterface ...@@ -23,6 +23,11 @@ class GeoShapeQuery implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
const INTERSECTS = 'intersects';
const DISJOINT = 'disjoint';
const WITHIN = 'within';
const CONTAINS = 'contains';
/** /**
* @var array * @var array
*/ */
...@@ -50,10 +55,18 @@ class GeoShapeQuery implements BuilderInterface ...@@ -50,10 +55,18 @@ class GeoShapeQuery implements BuilderInterface
* @param string $field Field name. * @param string $field Field name.
* @param string $type Shape type. * @param string $type Shape type.
* @param array $coordinates Shape coordinates. * @param array $coordinates Shape coordinates.
* @param string $relation Spatial relation.
* @param array $parameters Additional parameters. * @param array $parameters Additional parameters.
*/ */
public function addShape($field, $type, array $coordinates, array $parameters = []) public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = [])
{ {
// TODO: remove this in the next major version
if (is_array($relation)) {
$parameters = $relation;
$relation = self::INTERSECTS;
trigger_error('$parameters as parameter 4 in addShape is deprecated', E_USER_DEPRECATED);
}
$filter = array_merge( $filter = array_merge(
$parameters, $parameters,
[ [
...@@ -62,7 +75,10 @@ class GeoShapeQuery implements BuilderInterface ...@@ -62,7 +75,10 @@ class GeoShapeQuery implements BuilderInterface
] ]
); );
$this->fields[$field]['shape'] = $filter; $this->fields[$field] = [
'shape' => $filter,
'relation' => $relation,
];
} }
/** /**
......
...@@ -30,7 +30,7 @@ class TopHitsAggregationTest extends \PHPUnit\Framework\TestCase ...@@ -30,7 +30,7 @@ class TopHitsAggregationTest extends \PHPUnit\Framework\TestCase
$expected = [ $expected = [
'top_hits' => [ 'top_hits' => [
'sort' => [ 'sort' => [
'acme' => ['order' => 'asc'], ['acme' => ['order' => 'asc']],
], ],
'size' => 1, 'size' => 1,
'from' => 1, 'from' => 1,
......
...@@ -21,7 +21,7 @@ class GeoShapeQueryTest extends \PHPUnit\Framework\TestCase ...@@ -21,7 +21,7 @@ class GeoShapeQueryTest extends \PHPUnit\Framework\TestCase
public function testToArray() public function testToArray()
{ {
$filter = new GeoShapeQuery(['param1' => 'value1']); $filter = new GeoShapeQuery(['param1' => 'value1']);
$filter->addShape('location', 'envelope', [[13, 53], [14, 52]]); $filter->addShape('location', 'envelope', [[13, 53], [14, 52]], GeoShapeQuery::INTERSECTS);
$expected = [ $expected = [
'geo_shape' => [ 'geo_shape' => [
...@@ -30,6 +30,7 @@ class GeoShapeQueryTest extends \PHPUnit\Framework\TestCase ...@@ -30,6 +30,7 @@ class GeoShapeQueryTest extends \PHPUnit\Framework\TestCase
'type' => 'envelope', 'type' => 'envelope',
'coordinates' => [[13, 53], [14, 52]], 'coordinates' => [[13, 53], [14, 52]],
], ],
'relation' => 'intersects'
], ],
'param1' => 'value1', 'param1' => 'value1',
], ],
......
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