Skip to content

Commit 0a2ba52

Browse files
committed
Readd Nested filter
* Fix tests * Improve docker files by removing --prefer-source as it is an automatic fallback now
1 parent 3e539dc commit 0a2ba52

File tree

14 files changed

+321
-23
lines changed

14 files changed

+321
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file based on the
1212

1313
### Improvements
1414
- Update build dependency to elasticsearch 2.1.1 #997
15+
- Readd \Elastica\Filter\Nested. See https://github.com/ruflin/Elastica/issues/1001
1516

1617
### Deprecated
1718

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ RUN mkdir -p \
2525
./build/pdepend \
2626
./build/coverage
2727

28-
RUN composer install --prefer-source
28+
# Prefer source removed as automatic fallback now
29+
RUN composer install
2930

3031
# Copy rest of the files, ignoring .dockerignore files
3132
COPY lib /elastica/lib

env/elastica/Docker54

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This image is the base image for the Elastica development and includes all parts which rarely change
2-
# PHP 6 Docker file with Composer installed
2+
# PHP 5.4 Docker file with Composer installed
33
FROM php:5.4
44
MAINTAINER Nicolas Ruflin <[email protected]>
55

@@ -35,5 +35,5 @@ ENV PATH=/root/composer/vendor/bin:$PATH
3535

3636
COPY composer.json /root/composer/
3737

38-
# Install development tools
39-
RUN composer global install --prefer-source
38+
# Install development tools, prefer source removed as automatic fallback now
39+
RUN composer global install

env/elastica/Docker55

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This image is the base image for the Elastica development and includes all parts which rarely change
2-
# PHP 6 Docker file with Composer installed
2+
# PHP 5.5 Docker file with Composer installed
33
FROM php:5.5
44
MAINTAINER Nicolas Ruflin <[email protected]>
55

@@ -35,5 +35,5 @@ ENV PATH=/root/composer/vendor/bin:$PATH
3535

3636
COPY composer.json /root/composer/
3737

38-
# Install development tools
39-
RUN composer global install --prefer-source
38+
# Install development tools, prefer source removed as automatic fallback now
39+
RUN composer global install

env/elastica/Docker56

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This image is the base image for the Elastica development and includes all parts which rarely change
2-
# PHP 6 Docker file with Composer installed
2+
# PHP 5.6 Docker file with Composer installed
33
FROM php:5.6
44
MAINTAINER Nicolas Ruflin <[email protected]>
55

@@ -35,5 +35,5 @@ ENV PATH=/root/composer/vendor/bin:$PATH
3535

3636
COPY composer.json /root/composer/
3737

38-
# Install development tools
39-
RUN composer global install #--prefer-source
38+
# Install development tools, prefer source removed as automatic fallback now
39+
RUN composer global install

env/elastica/Docker70

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This image is the base image for the Elastica development and includes all parts which rarely change
2-
# PHP 6 Docker file with Composer installed
2+
# PHP 7 Docker file with Composer installed
33
FROM php:7.0
44
MAINTAINER Nicolas Ruflin <[email protected]>
55

@@ -35,5 +35,5 @@ ENV PATH=/root/composer/vendor/bin:$PATH
3535

3636
COPY composer.json /root/composer/
3737

38-
# Install development tools
39-
RUN composer global install --prefer-source
38+
# Install development tools, prefer source removed as automatic fallback now
39+
RUN composer global install

lib/Elastica/Filter/Nested.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace Elastica\Filter;
3+
4+
use Elastica\Query\AbstractQuery;
5+
6+
/**
7+
* Nested filter.
8+
*
9+
* @author Nicolas Ruflin <[email protected]>
10+
*
11+
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html
12+
*/
13+
class Nested extends AbstractFilter
14+
{
15+
/**
16+
* Adds field to mlt filter.
17+
*
18+
* @param string $path Nested object path
19+
*
20+
* @return $this
21+
*/
22+
public function setPath($path)
23+
{
24+
return $this->setParam('path', $path);
25+
}
26+
27+
/**
28+
* Sets nested query.
29+
*
30+
* @param \Elastica\Query\AbstractQuery $query
31+
*
32+
* @return $this
33+
*/
34+
public function setQuery(AbstractQuery $query)
35+
{
36+
return $this->setParam('query', $query);
37+
}
38+
39+
/**
40+
* Sets nested filter.
41+
*
42+
* @param \Elastica\Filter\AbstractFilter $filter
43+
*
44+
* @return $this
45+
*/
46+
public function setFilter(AbstractFilter $filter)
47+
{
48+
return $this->setParam('filter', $filter);
49+
}
50+
51+
/**
52+
* Set join option.
53+
*
54+
* @param bool $join
55+
*
56+
* @return $this
57+
*/
58+
public function setJoin($join)
59+
{
60+
return $this->setParam('join', (bool) $join);
61+
}
62+
}

lib/Elastica/QueryBuilder/DSL/Filter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Elastica\QueryBuilder\DSL;
33

4-
use Elastica\Exception\DeprecatedException;
54
use Elastica\Filter\AbstractFilter;
65
use Elastica\Filter\BoolAnd;
76
use Elastica\Filter\BoolFilter;
@@ -22,6 +21,7 @@
2221
use Elastica\Filter\Limit;
2322
use Elastica\Filter\MatchAll;
2423
use Elastica\Filter\Missing;
24+
use Elastica\Filter\Nested;
2525
use Elastica\Filter\NumericRange;
2626
use Elastica\Filter\Prefix;
2727
use Elastica\Filter\Query as QueryFilter;
@@ -315,7 +315,7 @@ public function missing($field = '')
315315
*/
316316
public function nested()
317317
{
318-
throw new DeprecatedException('Removed in ES 2.0');
318+
return new Nested();
319319
}
320320

321321
/**

test/lib/Elastica/Test/Base.php

-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77

88
class Base extends \PHPUnit_Framework_TestCase
99
{
10-
protected function es20($message = '')
11-
{
12-
parent::setUp();
13-
$this->markTestIncomplete($message."\nTest skipped because of current incompatibility with ES 2.0");
14-
}
15-
1610
/**
1711
* @param array $params Additional configuration params. Host and Port are already set
1812
* @param callback $callback

test/lib/Elastica/Test/BulkTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public function testMemoryUsage()
676676

677677
$endMemory = memory_get_usage();
678678

679-
$this->es20('Failed asserting that 2.2414096568375803 is less than 1.3.');
679+
$this->markTestIncomplete('Failed asserting that 2.2414096568375803 is less than 1.3.');
680680
$this->assertLessThan(1.3, $endMemory / $startMemory);
681681
}
682682
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
namespace Elastica\Test\Filter;
3+
4+
use Elastica\Document;
5+
use Elastica\Filter\Nested;
6+
use Elastica\Filter\Terms;
7+
use Elastica\Search;
8+
use Elastica\Test\Base as BaseTest;
9+
use Elastica\Type\Mapping;
10+
11+
class NestedFilterWithSetFilterTest extends BaseTest
12+
{
13+
protected function _getIndexForTest()
14+
{
15+
$index = $this->_createIndex();
16+
$type = $index->getType('user');
17+
18+
$type->setMapping(new Mapping(null, array(
19+
'firstname' => array('type' => 'string', 'store' => 'yes'),
20+
// default is store => no expected
21+
'lastname' => array('type' => 'string'),
22+
'hobbies' => array(
23+
'type' => 'nested',
24+
'include_in_parent' => true,
25+
'properties' => array('hobby' => array('type' => 'string')),
26+
),
27+
)));
28+
29+
$type->addDocuments(array(
30+
new Document(1, array(
31+
'firstname' => 'Nicolas',
32+
'lastname' => 'Ruflin',
33+
'hobbies' => array(
34+
array('hobby' => 'opensource'),
35+
),
36+
)),
37+
new Document(2, array(
38+
'firstname' => 'Nicolas',
39+
'lastname' => 'Ippolito',
40+
'hobbies' => array(
41+
array('hobby' => 'opensource'),
42+
array('hobby' => 'guitar'),
43+
),
44+
)),
45+
));
46+
47+
$index->refresh();
48+
49+
return $index;
50+
}
51+
52+
/**
53+
* @group unit
54+
*/
55+
public function testToArray()
56+
{
57+
$filter = new Nested();
58+
$this->assertEquals(array('nested' => array()), $filter->toArray());
59+
$query = new Terms();
60+
$query->setTerms('hobby', array('guitar'));
61+
$filter->setPath('hobbies');
62+
$filter->setFilter($query);
63+
64+
$expectedArray = array(
65+
'nested' => array(
66+
'path' => 'hobbies',
67+
'filter' => array('terms' => array(
68+
'hobby' => array('guitar'),
69+
)),
70+
),
71+
);
72+
73+
$this->assertEquals($expectedArray, $filter->toArray());
74+
}
75+
76+
/**
77+
* @group functional
78+
*/
79+
public function testShouldReturnTheRightNumberOfResult()
80+
{
81+
$filter = new Nested();
82+
$this->assertEquals(array('nested' => array()), $filter->toArray());
83+
$query = new Terms();
84+
$query->setTerms('hobbies.hobby', array('guitar'));
85+
$filter->setPath('hobbies');
86+
$filter->setFilter($query);
87+
88+
$client = $this->_getClient();
89+
$search = new Search($client);
90+
$index = $this->_getIndexForTest();
91+
$search->addIndex($index);
92+
$resultSet = $search->search($filter);
93+
94+
$this->assertEquals(1, $resultSet->getTotalHits());
95+
96+
$filter = new Nested();
97+
$this->assertEquals(array('nested' => array()), $filter->toArray());
98+
$query = new Terms();
99+
$query->setTerms('hobbies.hobby', array('opensource'));
100+
$filter->setPath('hobbies');
101+
$filter->setFilter($query);
102+
103+
$client = $this->_getClient();
104+
$search = new Search($client);
105+
$index = $this->_getIndexForTest();
106+
$search->addIndex($index);
107+
$resultSet = $search->search($filter);
108+
109+
$this->assertEquals(2, $resultSet->getTotalHits());
110+
}
111+
}

0 commit comments

Comments
 (0)