Skip to content
Snippets Groups Projects
Commit 30fb9aef authored by Mantas Varatiejus's avatar Mantas Varatiejus
Browse files

Deprecate filters

parent a644572a
No related branches found
No related tags found
No related merge requests found
Showing
with 822 additions and 17 deletions
<?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;
use ONGR\ElasticsearchDSL\BuilderInterface;
/**
* Represents Elasticsearch "limit" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-limit-query.html
*/
class LimitQuery implements BuilderInterface
{
/**
* @var int
*/
private $value;
/**
* @param int $value Number of documents (per shard) to execute on
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'limit';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return [
'value' => $this->value,
];
}
}
......@@ -15,7 +15,9 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch match_all query class.
* Represents Elasticsearch "bool" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
*/
class MatchAllQuery implements BuilderInterface
{
......
<?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;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "missing" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-missing-query.html
*/
class MissingQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var string
*/
private $field;
/**
* Constructor.
*
* @param string $field Field name
* @param array $parameters Optional parameters
*/
public function __construct($field, array $parameters = [])
{
$this->field = $field;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'missing';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$query = ['field' => $this->field];
$output = $this->processArray($query);
return $output;
}
}
......@@ -15,7 +15,9 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch nested query class.
* Represents Elasticsearch "nested" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
*/
class NestedQuery implements BuilderInterface
{
......@@ -34,11 +36,13 @@ class NestedQuery implements BuilderInterface
/**
* @param string $path
* @param BuilderInterface $query
* @param array $parameters
*/
public function __construct($path, BuilderInterface $query)
public function __construct($path, BuilderInterface $query, array $parameters = [])
{
$this->path = $path;
$this->query = $query;
$this->parameters = $parameters;
}
/**
......
......@@ -11,13 +11,48 @@
namespace ONGR\ElasticsearchDSL\Query;
use ONGR\ElasticsearchDSL\Filter\PrefixFilter;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "prefix" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
*/
class PrefixQuery extends PrefixFilter
class PrefixQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var string
*/
protected $field;
/**
* @var string
*/
protected $value;
/**
* @param string $field Field name.
* @param string $value Value.
* @param array $parameters Optional parameters.
*/
public function __construct($field, $value, array $parameters = [])
{
$this->field = $field;
$this->value = $value;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'prefix';
}
/**
* {@inheritdoc}
*/
......
......@@ -15,7 +15,9 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Regexp query class.
* Represents Elasticsearch "regexp" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
*/
class RegexpQuery implements BuilderInterface
{
......
<?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;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "script" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html
*/
class ScriptQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var string
*/
private $script;
/**
* @param string $script Script
* @param array $parameters Optional parameters
*/
public function __construct($script, array $parameters = [])
{
$this->script = $script;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'script';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$query = ['inline' => $this->script];
$output = $this->processArray($query);
return ['script' => $output];
}
}
......@@ -15,7 +15,9 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch term query class.
* Represents Elasticsearch "term" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
*/
class TermQuery implements BuilderInterface
{
......
......@@ -15,7 +15,9 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch terms query class.
* Represents Elasticsearch "terms" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
*/
class TermsQuery implements BuilderInterface
{
......@@ -29,17 +31,19 @@ class TermsQuery implements BuilderInterface
/**
* @var array
*/
private $tags;
private $terms;
/**
* @param string $field
* @param array $tags
* @param array $parameters
* Constructor.
*
* @param string $field Field name
* @param array $terms An array of terms
* @param array $parameters Optional parameters
*/
public function __construct($field, $tags, array $parameters = [])
public function __construct($field, $terms, array $parameters = [])
{
$this->field = $field;
$this->tags = $tags;
$this->terms = $terms;
$this->setParameters($parameters);
}
......@@ -57,7 +61,7 @@ class TermsQuery implements BuilderInterface
public function toArray()
{
$query = [
$this->field => $this->tags,
$this->field => $this->terms,
];
$output = $this->processArray($query);
......
<?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;
use ONGR\ElasticsearchDSL\BuilderInterface;
/**
* Represents Elasticsearch "type" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html
*/
class TypeQuery implements BuilderInterface // TODO: add test
{
/**
* @var string
*/
private $type;
/**
* Constructor.
*
* @param string $type Type name
*/
public function __construct($type)
{
$this->type = $type;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'type';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return [
'value' => $this->type,
];
}
}
......@@ -35,9 +35,14 @@ class RangeFilterTest extends \PHPUnit_Framework_TestCase
// Case #1.
['', [], [], ['' => []]],
// Case #2.
['foo', [1, 5], [], ['foo' => [0 => 1, 1 => 5]]],
['foo', ['gte' => 1, 'lte' => 5], [], ['foo' => ['gte' => 1, 'lte' => 5]]],
// Case #3.
['test', ['foo', 'bar'], ['type' => 'acme'], ['test' => [0 => 'foo', 1 => 'bar'], 'type' => 'acme']],
[
'test',
['gte' => 1, 'lte' => 5],
['type' => 'acme'],
['test' => ['gte' => 1, 'lte' => 5, 'type' => 'acme']]
],
];
}
......
<?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\Query;
use ONGR\ElasticsearchDSL\Query\ExistsQuery;
/**
* Unit test for ExistsQuery.
*/
class ExistsQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests ExistsQuery#toArray() method.
*/
public function testToArray()
{
$query = new ExistsQuery('bar');
$this->assertEquals(['field' => 'bar'], $query->toArray());
}
}
<?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\DSL\Query;
use ONGR\ElasticsearchDSL\Filter\GeoBoundingBoxFilter;
class GeoBoundingBoxQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if exception is thrown when geo points are not set.
*
* @expectedException \LogicException
*/
public function testGeoBoundBoxFilterException()
{
$filter = new GeoBoundingBoxFilter('location', []);
$filter->toArray();
}
/**
* Data provider for testToArray().
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case #1 (2 values).
[
'location',
[
['lat' => 40.73, 'lon' => -74.1],
['lat' => 40.01, 'lon' => -71.12],
],
['parameter' => 'value'],
[
'location' => [
'top_left' => ['lat' => 40.73, 'lon' => -74.1],
'bottom_right' => ['lat' => 40.01, 'lon' => -71.12],
],
'parameter' => 'value',
],
],
// Case #2 (4 values).
[
'location',
[40.73, -74.1, 40.01, -71.12],
['parameter' => 'value'],
[
'location' => [
'top' => 40.73,
'left' => -74.1,
'bottom' => 40.01,
'right' => -71.12,
],
'parameter' => 'value',
],
],
];
}
/**
* Tests toArray method.
*
* @param string $field Field name.
* @param array $values Bounding box values.
* @param array $parameters Optional parameters.
* @param array $expected Expected result.
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $values, $parameters, $expected)
{
$filter = new GeoBoundingBoxFilter($field, $values, $parameters);
$result = $filter->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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\GeoDistanceQuery;
class GeoDistanceQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray().
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case #1.
[
'location',
'200km',
['lat' => 40, 'lon' => -70],
[],
['distance' => '200km', 'location' => ['lat' => 40, 'lon' => -70]],
],
// Case #2.
[
'location',
'20km',
['lat' => 0, 'lon' => 0],
['parameter' => 'value'],
['distance' => '20km', 'location' => ['lat' => 0, 'lon' => 0], 'parameter' => 'value'],
],
];
}
/**
* Tests toArray() method.
*
* @param string $field Field name.
* @param string $distance Distance.
* @param array $location Location.
* @param array $parameters Optional parameters.
* @param array $expected Expected result.
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $distance, $location, $parameters, $expected)
{
$query = new GeoDistanceQuery($field, $distance, $location, $parameters);
$result = $query->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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\GeoDistanceRangeQuery;
class GeoDistanceRangeQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider to testToArray.
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case #1.
[
'location',
['from' => '200km', 'to' => '400km'],
['lat' => 40, 'lon' => -70],
[],
['from' => '200km', 'to' => '400km', 'location' => ['lat' => 40, 'lon' => -70]],
],
// Case #2.
[
'location',
['from' => '150km', 'to' => '180km'],
['lat' => 0, 'lon' => 0],
['parameter' => 'value'],
['from' => '150km', 'to' => '180km', 'location' => ['lat' => 0, 'lon' => 0], 'parameter' => 'value'],
],
];
}
/**
* Tests toArray method.
*
* @param string $field Field name.
* @param array $range Distance range.
* @param array $location Location.
* @param array $parameters Optional parameters.
* @param array $expected Expected result.
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $range, $location, $parameters, $expected)
{
$query = new GeoDistanceRangeQuery($field, $range, $location, $parameters);
$result = $query->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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\GeoPolygonQuery;
class GeoPolygonQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider to testToArray.
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case #1.
[
'location',
[
['lat' => 20, 'lon' => -80],
['lat' => 30, 'lon' => -40],
['lat' => 70, 'lon' => -90],
],
[],
[
'location' => [
'points' => [
['lat' => 20, 'lon' => -80],
['lat' => 30, 'lon' => -40],
['lat' => 70, 'lon' => -90],
],
],
],
],
// Case #2.
[
'location',
[],
['parameter' => 'value'],
[
'location' => ['points' => []],
'parameter' => 'value',
],
],
// Case #3.
[
'location',
[
['lat' => 20, 'lon' => -80],
],
['parameter' => 'value'],
[
'location' => [
'points' => [['lat' => 20, 'lon' => -80]],
],
'parameter' => 'value',
],
],
];
}
/**
* Tests toArray method.
*
* @param string $field Field name.
* @param array $points Polygon's points.
* @param array $parameters Optional parameters.
* @param array $expected Expected result.
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $points, $parameters, $expected)
{
$filter = new GeoPolygonQuery($field, $points, $parameters);
$result = $filter->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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\GeoShapeQuery;
class GeoShapeQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests toArray() method.
*/
public function testToArray()
{
$filter = new GeoShapeQuery(['param1' => 'value1']);
$filter->addShape('location', 'envelope', [[13, 53], [14, 52]]);
$expected = [
'location' => [
'shape' => [
'type' => 'envelope',
'coordinates' => [[13, 53], [14, 52]],
],
],
'param1' => 'value1',
];
$this->assertEquals($expected, $filter->toArray());
}
/**
* Test for toArray() in case of pre-indexed shape.
*/
public function testToArrayIndexed()
{
$filter = new GeoShapeQuery(['param1' => 'value1']);
$filter->addPreIndexedShape('location', 'DEU', 'countries', 'shapes', 'location');
$expected = [
'location' => [
'indexed_shape' => [
'id' => 'DEU',
'type' => 'countries',
'index' => 'shapes',
'path' => 'location',
],
],
'param1' => 'value1',
];
$this->assertEquals($expected, $filter->toArray());
}
}
<?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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\GeohashCellQuery;
class GeohashCellQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray().
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case #1.
[
'location',
['lat' => 40, 'lon' => -70],
[],
['location' => ['lat' => 40, 'lon' => -70]],
],
// Case #2.
[
'location',
['lat' => 0, 'lon' => 0],
['parameter' => 'value'],
['location' => ['lat' => 0, 'lon' => 0], 'parameter' => 'value'],
],
];
}
/**
* Tests toArray() method.
*
* @param string $field Field name.
* @param array $location Location.
* @param array $parameters Optional parameters.
* @param array $expected Expected result.
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $location, $parameters, $expected)
{
$query = new GeohashCellQuery($field, $location, $parameters);
$result = $query->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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\LimitQuery;
class LimitQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Test for query toArray() method.
*/
public function testToArray()
{
$query = new LimitQuery(3);
$expectedResult = ['value' => 3];
$this->assertEquals($expectedResult, $query->toArray());
}
}
<?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\DSL\Query;
use ONGR\ElasticsearchDSL\Query\MissingQuery;
class MissingQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider to testGetToArray.
*
* @return array
*/
public function getArrayDataProvider()
{
return [
// Case 1.
['user', [], ['field' => 'user']],
// Case 2.
['user', ['existence' => true], ['field' => 'user', 'existence' => true]],
];
}
/**
* Test for query toArray() method.
*
* @param string $field
* @param array $parameters
* @param array $expected
*
* @dataProvider getArrayDataProvider
*/
public function testToArray($field, $parameters, $expected)
{
$query = new MissingQuery($field, $parameters);
$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