|
| 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