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

Merge branch '1.x'

# Conflicts:
#	CHANGELOG.md
#	src/SearchEndpoint/FilterEndpoint.php
#	tests/Aggregation/FilterAggregationTest.php
parents 2fc32e71 9dc483de
No related branches found
No related tags found
No related merge requests found
Showing with 55 additions and 43 deletions
CHANGELOG
=========
v2.0.0 (2016-x)
---
- [BC break] Aggregation name is not prefixed anymore
v1.1.1 (2016-01-26)
---
- Fixed query endpoint normalization when called repeatedly [#56](https://github.com/ongr-io/ElasticsearchDSL/pull/56)
- Deprecated `DslTypeAwareTrait` and `FilterOrQueryDetectionTrait` traits
v1.1.0 (2015-12-28)
---
......
......@@ -43,16 +43,22 @@ At the end it will form this query:
### Form a Filter
> Since Elasticsearch 2.0 all filters were replaced by queries. Queries acts like
> filters when you use them in filter context. For easier future migration use query
> classes instead of filter. (Note, for Elasticsearch 1.* you still should use `ScriptFilter`,
> `HasChildFilter`, `HasParentFilter`, `IndicesFilter`, `NestedFilter`, `PrefixFilter`,
> `RegexpFilter`, `TermFilter` instead of query as they has different structure.)
To add a filter is the same way like a query. First, lets create some `Filter` object.
```php
$matchAllFilter = new MatchAllFilter();
$matchAllQuery = new MatchAllQuery();
```
And simply add to the `Search`:
```php
$search->addFilter($matchAllFilter);
$search->addFilter($matchAllQuery);
```
Unlike `Query`, when we add a `Filter` with our DSL library it will add a query and all necessary stuff for you. So when we add one filter we will get this query:
......@@ -115,8 +121,8 @@ The same way it works with a `Filter`. Take a look at this example:
```php
$search = new Search();
$termFilter = new TermFilter('name', 'ongr');
$missingFilter = new MissingFilter('disabled');
$existsFilter = new ExistsFilter('tag');
$missingFilter = new MissingQuery('disabled');
$existsFilter = new ExistsQuery('tag');
$search->addFilter($termFilter);
$search->addFilter($missingFilter);
$search->addFilter($existsFilter, BoolQuery::MUST_NOT);
......
# Filtered query
__DEPRECATED__: filtered query is deprecated and will be removed in ElasticsearchDSL 2.0
> More info about filtered query is in the [official elasticsearch docs][1]
The filtered query is used to combine another query with any filter. Filters are usually faster than queries.
......
......@@ -19,6 +19,9 @@ interface BuilderInterface
/**
* Generates array which will be passed to elasticsearch-php client.
*
* WARNING: the output of this method will change in version v2.0. It will
* always return array WITH query/aggregation type as key.
*
* @return array|object
*/
public function toArray();
......
......@@ -13,6 +13,8 @@ namespace ONGR\ElasticsearchDSL;
/**
* A trait which handles dsl type.
*
* @deprecated Will be removed in 2.0.
*/
trait DslTypeAwareTrait
{
......
......@@ -13,6 +13,8 @@ namespace ONGR\ElasticsearchDSL;
/**
* A trait which can detect query or filter is passed.
*
* @deprecated Will be removed in 2.0.
*/
trait FilterOrQueryDetectionTrait
{
......
......@@ -18,7 +18,7 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html
*/
class TypeQuery implements BuilderInterface // TODO: add test
class TypeQuery implements BuilderInterface
{
/**
* @var string
......
......@@ -221,7 +221,9 @@ class Search
*/
public function addFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null)
{
// Trigger creation of QueryEndpoint as filters depends on it
$this->getEndpoint(QueryEndpoint::NAME);
$endpoint = $this->getEndpoint(FilterEndpoint::NAME);
$endpoint->addToBool($filter, $boolType, $key);
......
......@@ -11,7 +11,6 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\Query\FilteredQuery;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
......@@ -47,14 +46,4 @@ class FilterEndpoint extends QueryEndpoint
{
return 1;
}
/**
* Returns bool instance for this endpoint case.
*
* @return BoolQuery
*/
protected function getBoolInstance()
{
return new BoolQuery();
}
}
......@@ -37,20 +37,24 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$query = $this->getBool();
if ($this->hasReference('filtered_query')) {
/** @var FilteredQuery $filteredQuery */
$filteredQuery = $this->getReference('filtered_query');
$this->add($filteredQuery);
if ($query) {
$filteredQuery->setQuery($query);
}
$query = $filteredQuery;
}
if (!$this->bool) {
if (!$query) {
return null;
}
$queryArray = $this->bool->toArray();
$queryArray = [$this->bool->getType() => $queryArray];
return $queryArray;
return [$query->getType() => $query->toArray()];
}
/**
......
......@@ -11,7 +11,7 @@
namespace ONGR\ElasticsearchDSL\Tests\Query;
use ONGR\ElasticsearchDSL\Filter\GeoBoundingBoxFilter;
use ONGR\ElasticsearchDSL\Query\GeoBoundingBoxQuery;
class GeoBoundingBoxQueryTest extends \PHPUnit_Framework_TestCase
{
......@@ -20,10 +20,10 @@ class GeoBoundingBoxQueryTest extends \PHPUnit_Framework_TestCase
*
* @expectedException \LogicException
*/
public function testGeoBoundBoxFilterException()
public function testGeoBoundBoxQueryException()
{
$filter = new GeoBoundingBoxFilter('location', []);
$filter->toArray();
$query = new GeoBoundingBoxQuery('location', []);
$query->toArray();
}
/**
......@@ -80,8 +80,8 @@ class GeoBoundingBoxQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray($field, $values, $parameters, $expected)
{
$filter = new GeoBoundingBoxFilter($field, $values, $parameters);
$result = $filter->toArray();
$query = new GeoBoundingBoxQuery($field, $values, $parameters);
$result = $query->toArray();
$this->assertEquals($expected, $result);
}
}
......@@ -20,10 +20,8 @@ class HasChildQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testConstructor()
{
$missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter')
->setConstructorArgs(['test_field'])
->getMock();
$query = new HasChildQuery('test_type', $missingFilterMock, ['test_parameter1']);
$childQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$query = new HasChildQuery('test_type', $childQuery, ['test_parameter1']);
$this->assertEquals(['test_parameter1'], $query->getParameters());
}
}
......@@ -20,10 +20,8 @@ class HasParentQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testConstructor()
{
$missingFilter = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter')
->setConstructorArgs(['test_field'])
->getMock();
$query = new HasParentQuery('test_type', $missingFilter, ['test_parameter1']);
$parentQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$query = new HasParentQuery('test_type', $parentQuery, ['test_parameter1']);
$this->assertEquals(['test_parameter1'], $query->getParameters());
}
}
......@@ -11,8 +11,8 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint;
use ONGR\ElasticsearchDSL\Filter\MatchAllFilter;
use ONGR\ElasticsearchDSL\Query\FilteredQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
......@@ -55,7 +55,7 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase
$this->assertNull($instance->normalize($normalizerInterface));
$this->assertFalse($instance->hasReference('filtered_query'));
$matchAllFilter = new MatchAllFilter();
$matchAllFilter = new MatchAllQuery();
$instance->add($matchAllFilter);
$this->assertNull($instance->normalize($normalizerInterface));
......@@ -81,7 +81,7 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase
public function testEndpointGetter()
{
$filterName = 'acme_filter';
$filter = new MatchAllFilter();
$filter = new MatchAllQuery();
$endpoint = new FilterEndpoint();
$endpoint->add($filter, $filterName);
$builders = $endpoint->getAll();
......
......@@ -11,7 +11,7 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint;
use ONGR\ElasticsearchDSL\Filter\MatchAllFilter;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
......@@ -50,7 +50,7 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase
);
$this->assertNull($instance->normalize($normalizerInterface));
$matchAll = new MatchAllFilter();
$matchAll = new MatchAllQuery();
$instance->add($matchAll);
$this->assertEquals(
......@@ -65,7 +65,7 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase
public function testEndpointGetter()
{
$filterName = 'acme_post_filter';
$filter = new MatchAllFilter();
$filter = new MatchAllQuery();
$endpoint = new PostFilterEndpoint();
$endpoint->add($filter, $filterName);
......
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