Skip to content

Commit

Permalink
Readd Nested filter
Browse files Browse the repository at this point in the history
 * Fix tests
 * Improve docker files by removing --prefer-source as it is an automatic fallback now
  • Loading branch information
ruflin committed Jan 5, 2016
1 parent 3e539dc commit 0a2ba52
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file based on the

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

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ RUN mkdir -p \
./build/pdepend \
./build/coverage

RUN composer install --prefer-source
# Prefer source removed as automatic fallback now
RUN composer install

# Copy rest of the files, ignoring .dockerignore files
COPY lib /elastica/lib
Expand Down
6 changes: 3 additions & 3 deletions env/elastica/Docker54
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This image is the base image for the Elastica development and includes all parts which rarely change
# PHP 6 Docker file with Composer installed
# PHP 5.4 Docker file with Composer installed
FROM php:5.4
MAINTAINER Nicolas Ruflin <spam@ruflin.com>

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

COPY composer.json /root/composer/

# Install development tools
RUN composer global install --prefer-source
# Install development tools, prefer source removed as automatic fallback now
RUN composer global install
6 changes: 3 additions & 3 deletions env/elastica/Docker55
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This image is the base image for the Elastica development and includes all parts which rarely change
# PHP 6 Docker file with Composer installed
# PHP 5.5 Docker file with Composer installed
FROM php:5.5
MAINTAINER Nicolas Ruflin <spam@ruflin.com>

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

COPY composer.json /root/composer/

# Install development tools
RUN composer global install --prefer-source
# Install development tools, prefer source removed as automatic fallback now
RUN composer global install
6 changes: 3 additions & 3 deletions env/elastica/Docker56
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This image is the base image for the Elastica development and includes all parts which rarely change
# PHP 6 Docker file with Composer installed
# PHP 5.6 Docker file with Composer installed
FROM php:5.6
MAINTAINER Nicolas Ruflin <spam@ruflin.com>

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

COPY composer.json /root/composer/

# Install development tools
RUN composer global install #--prefer-source
# Install development tools, prefer source removed as automatic fallback now
RUN composer global install
6 changes: 3 additions & 3 deletions env/elastica/Docker70
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This image is the base image for the Elastica development and includes all parts which rarely change
# PHP 6 Docker file with Composer installed
# PHP 7 Docker file with Composer installed
FROM php:7.0
MAINTAINER Nicolas Ruflin <spam@ruflin.com>

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

COPY composer.json /root/composer/

# Install development tools
RUN composer global install --prefer-source
# Install development tools, prefer source removed as automatic fallback now
RUN composer global install
62 changes: 62 additions & 0 deletions lib/Elastica/Filter/Nested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace Elastica\Filter;

use Elastica\Query\AbstractQuery;

/**
* Nested filter.
*
* @author Nicolas Ruflin <spam@ruflin.com>
*
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html
*/
class Nested extends AbstractFilter
{
/**
* Adds field to mlt filter.
*
* @param string $path Nested object path
*
* @return $this
*/
public function setPath($path)
{
return $this->setParam('path', $path);
}

/**
* Sets nested query.
*
* @param \Elastica\Query\AbstractQuery $query
*
* @return $this
*/
public function setQuery(AbstractQuery $query)
{
return $this->setParam('query', $query);
}

/**
* Sets nested filter.
*
* @param \Elastica\Filter\AbstractFilter $filter
*
* @return $this
*/
public function setFilter(AbstractFilter $filter)
{
return $this->setParam('filter', $filter);
}

/**
* Set join option.
*
* @param bool $join
*
* @return $this
*/
public function setJoin($join)
{
return $this->setParam('join', (bool) $join);
}
}
4 changes: 2 additions & 2 deletions lib/Elastica/QueryBuilder/DSL/Filter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Elastica\QueryBuilder\DSL;

use Elastica\Exception\DeprecatedException;
use Elastica\Filter\AbstractFilter;
use Elastica\Filter\BoolAnd;
use Elastica\Filter\BoolFilter;
Expand All @@ -22,6 +21,7 @@
use Elastica\Filter\Limit;
use Elastica\Filter\MatchAll;
use Elastica\Filter\Missing;
use Elastica\Filter\Nested;
use Elastica\Filter\NumericRange;
use Elastica\Filter\Prefix;
use Elastica\Filter\Query as QueryFilter;
Expand Down Expand Up @@ -315,7 +315,7 @@ public function missing($field = '')
*/
public function nested()
{
throw new DeprecatedException('Removed in ES 2.0');
return new Nested();
}

/**
Expand Down
6 changes: 0 additions & 6 deletions test/lib/Elastica/Test/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

class Base extends \PHPUnit_Framework_TestCase
{
protected function es20($message = '')
{
parent::setUp();
$this->markTestIncomplete($message."\nTest skipped because of current incompatibility with ES 2.0");
}

/**
* @param array $params Additional configuration params. Host and Port are already set
* @param callback $callback
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public function testMemoryUsage()

$endMemory = memory_get_usage();

$this->es20('Failed asserting that 2.2414096568375803 is less than 1.3.');
$this->markTestIncomplete('Failed asserting that 2.2414096568375803 is less than 1.3.');
$this->assertLessThan(1.3, $endMemory / $startMemory);
}
}
111 changes: 111 additions & 0 deletions test/lib/Elastica/Test/Filter/NestedFilterWithSetFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
namespace Elastica\Test\Filter;

use Elastica\Document;
use Elastica\Filter\Nested;
use Elastica\Filter\Terms;
use Elastica\Search;
use Elastica\Test\Base as BaseTest;
use Elastica\Type\Mapping;

class NestedFilterWithSetFilterTest extends BaseTest
{
protected function _getIndexForTest()
{
$index = $this->_createIndex();
$type = $index->getType('user');

$type->setMapping(new Mapping(null, array(
'firstname' => array('type' => 'string', 'store' => 'yes'),
// default is store => no expected
'lastname' => array('type' => 'string'),
'hobbies' => array(
'type' => 'nested',
'include_in_parent' => true,
'properties' => array('hobby' => array('type' => 'string')),
),
)));

$type->addDocuments(array(
new Document(1, array(
'firstname' => 'Nicolas',
'lastname' => 'Ruflin',
'hobbies' => array(
array('hobby' => 'opensource'),
),
)),
new Document(2, array(
'firstname' => 'Nicolas',
'lastname' => 'Ippolito',
'hobbies' => array(
array('hobby' => 'opensource'),
array('hobby' => 'guitar'),
),
)),
));

$index->refresh();

return $index;
}

/**
* @group unit
*/
public function testToArray()
{
$filter = new Nested();
$this->assertEquals(array('nested' => array()), $filter->toArray());
$query = new Terms();
$query->setTerms('hobby', array('guitar'));
$filter->setPath('hobbies');
$filter->setFilter($query);

$expectedArray = array(
'nested' => array(
'path' => 'hobbies',
'filter' => array('terms' => array(
'hobby' => array('guitar'),
)),
),
);

$this->assertEquals($expectedArray, $filter->toArray());
}

/**
* @group functional
*/
public function testShouldReturnTheRightNumberOfResult()
{
$filter = new Nested();
$this->assertEquals(array('nested' => array()), $filter->toArray());
$query = new Terms();
$query->setTerms('hobbies.hobby', array('guitar'));
$filter->setPath('hobbies');
$filter->setFilter($query);

$client = $this->_getClient();
$search = new Search($client);
$index = $this->_getIndexForTest();
$search->addIndex($index);
$resultSet = $search->search($filter);

$this->assertEquals(1, $resultSet->getTotalHits());

$filter = new Nested();
$this->assertEquals(array('nested' => array()), $filter->toArray());
$query = new Terms();
$query->setTerms('hobbies.hobby', array('opensource'));
$filter->setPath('hobbies');
$filter->setFilter($query);

$client = $this->_getClient();
$search = new Search($client);
$index = $this->_getIndexForTest();
$search->addIndex($index);
$resultSet = $search->search($filter);

$this->assertEquals(2, $resultSet->getTotalHits());
}
}
Loading

0 comments on commit 0a2ba52

Please sign in to comment.