diff options
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php')
-rw-r--r-- | vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php | 311 |
1 files changed, 229 insertions, 82 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php index e08d2d60..905f8462 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/SearchTest.php @@ -1,15 +1,14 @@ <?php - namespace Elastica\Test; -use Elastica\Client; +use Elastica\Aggregation; use Elastica\Document; +use Elastica\Exception\ResponseException; use Elastica\Index; -use Elastica\Query\Builder; +use Elastica\Query; +use Elastica\Query\FunctionScore; use Elastica\Query\MatchAll; use Elastica\Query\QueryString; -use Elastica\Query\FunctionScore; -use Elastica\Query; use Elastica\Script; use Elastica\Search; use Elastica\Test\Base as BaseTest; @@ -17,6 +16,9 @@ use Elastica\Type; class SearchTest extends BaseTest { + /** + * @group unit + */ public function testConstruct() { $client = $this->_getClient(); @@ -26,13 +28,16 @@ class SearchTest extends BaseTest $this->assertSame($client, $search->getClient()); } + /** + * @group functional + */ public function testAddIndex() { $client = $this->_getClient(); $search = new Search($client); - $index1 = $this->_createIndex('test1'); - $index2 = $this->_createIndex('test2'); + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); $search->addIndex($index1); $indices = $search->getIndices(); @@ -55,6 +60,9 @@ class SearchTest extends BaseTest $this->assertTrue(in_array('test3', $indices)); } + /** + * @group unit + */ public function testAddIndices() { $client = $this->_getClient(); @@ -69,6 +77,9 @@ class SearchTest extends BaseTest $this->assertEquals(2, count($search->getIndices())); } + /** + * @group functional + */ public function testAddType() { $client = $this->_getClient(); @@ -102,12 +113,15 @@ class SearchTest extends BaseTest $this->assertTrue(in_array('test3', $types)); } + /** + * @group unit + */ public function testAddTypes() { $client = $this->_getClient(); $search = new Search($client); - $index = $this->_createIndex(); + $index = $client->getIndex('foo'); $types = array(); $types[] = $index->getType('type1'); @@ -119,6 +133,7 @@ class SearchTest extends BaseTest } /** + * @group unit * @expectedException \Elastica\Exception\InvalidException */ public function testAddTypeInvalid() @@ -130,6 +145,7 @@ class SearchTest extends BaseTest } /** + * @group unit * @expectedException \Elastica\Exception\InvalidException */ public function testAddIndexInvalid() @@ -140,14 +156,30 @@ class SearchTest extends BaseTest $search->addIndex(new \stdClass()); } + /** + * @group unit + */ + public function testAddNumericIndex() + { + $client = $this->_getClient(); + $search = new Search($client); + + $search->addIndex(1); + + $this->assertContains('1', $search->getIndices(), 'Make sure it has been added and converted to string'); + } + + /** + * @group functional + */ public function testGetPath() { $client = $this->_getClient(); $search1 = new Search($client); $search2 = new Search($client); - $index1 = $this->_createIndex('test1'); - $index2 = $this->_createIndex('test2'); + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); $type1 = $index1->getType('type1'); $type2 = $index1->getType('type2'); @@ -157,32 +189,35 @@ class SearchTest extends BaseTest // Only index $search1->addIndex($index1); - $this->assertEquals($index1->getName() . '/_search', $search1->getPath()); + $this->assertEquals($index1->getName().'/_search', $search1->getPath()); // MUltiple index, no types $search1->addIndex($index2); - $this->assertEquals($index1->getName() . ',' . $index2->getName() . '/_search', $search1->getPath()); + $this->assertEquals($index1->getName().','.$index2->getName().'/_search', $search1->getPath()); // Single type, no index $search2->addType($type1); - $this->assertEquals('_all/' . $type1->getName() . '/_search', $search2->getPath()); + $this->assertEquals('_all/'.$type1->getName().'/_search', $search2->getPath()); // Multiple types $search2->addType($type2); - $this->assertEquals('_all/' . $type1->getName() . ',' . $type2->getName() . '/_search', $search2->getPath()); + $this->assertEquals('_all/'.$type1->getName().','.$type2->getName().'/_search', $search2->getPath()); // Combine index and types $search2->addIndex($index1); - $this->assertEquals($index1->getName() . '/' . $type1->getName() . ',' . $type2->getName() . '/_search', $search2->getPath()); + $this->assertEquals($index1->getName().'/'.$type1->getName().','.$type2->getName().'/_search', $search2->getPath()); } + /** + * @group functional + */ public function testSearchRequest() { $client = $this->_getClient(); $search1 = new Search($client); - $index1 = $this->_createIndex('test1'); - $index2 = $this->_createIndex('test2'); + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); $type1 = $index1->getType('hello1'); @@ -205,11 +240,14 @@ class SearchTest extends BaseTest $this->assertFalse($result->getResponse()->hasError()); } + /** + * @group functional + */ public function testSearchScrollRequest() { $client = $this->_getClient(); - $index = $this->_createIndex('test'); + $index = $this->_createIndex(); $type = $index->getType('scrolltest'); $docs = array(); @@ -265,7 +303,9 @@ class SearchTest extends BaseTest } /** - * Default Limit tests for \Elastica\Search + * Default Limit tests for \Elastica\Search. + * + * @group functional */ public function testLimitDefaultSearch() { @@ -275,20 +315,20 @@ class SearchTest extends BaseTest $index = $client->getIndex('zero'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); - $docs = array(); - $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); $type = $index->getType('zeroType'); - $type->addDocuments($docs); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + )); $index->refresh(); $search->addIndex($index)->addType($type); @@ -303,6 +343,7 @@ class SearchTest extends BaseTest } /** + * @group functional * @expectedException \Elastica\Exception\InvalidException */ public function testArrayConfigSearch() @@ -357,6 +398,13 @@ class SearchTest extends BaseTest $this->assertTrue(($resultSet->count() === 0) && $resultSet->getTotalHits() === 11); //Timeout - this one is a bit more tricky to test + $mockResponse = new \Elastica\Response(json_encode(array('timed_out' => true))); + $client = $this->getMockBuilder('Elastica\\Client') + ->disableOriginalConstructor() + ->getMock(); + $client->method('request') + ->will($this->returnValue($mockResponse)); + $search = new Search($client); $script = new Script('Thread.sleep(100); return _score;'); $query = new FunctionScore(); $query->addScriptScoreFunction($script); @@ -367,9 +415,12 @@ class SearchTest extends BaseTest $resultSet = $search->search('test', array('invalid_option' => 'invalid_option_value')); } + /** + * @group functional + */ public function testSearchWithVersionOption() { - $index = $this->_createIndex('test1'); + $index = $this->_createIndex(); $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'ruflin')); $index->getType('test')->addDocument($doc); $index->refresh(); @@ -388,6 +439,9 @@ class SearchTest extends BaseTest $this->assertEquals(1, $hit->getParam('_version')); } + /** + * @group functional + */ public function testCountRequest() { $client = $this->_getClient(); @@ -396,21 +450,20 @@ class SearchTest extends BaseTest $index = $client->getIndex('zero'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); - $docs = array(); - $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')); - $type = $index->getType('zeroType'); - $type->addDocuments($docs); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'marley')), + )); $index->refresh(); $search->addIndex($index)->addType($type); @@ -431,6 +484,9 @@ class SearchTest extends BaseTest $this->assertEquals(0, $count); } + /** + * @group functional + */ public function testEmptySearch() { $client = $this->_getClient(); @@ -438,20 +494,20 @@ class SearchTest extends BaseTest $index = $client->getIndex('zero'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); - $docs = array(); - $docs[] = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')); - $docs[] = new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); - $docs[] = new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); - $docs[] = new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); - $docs[] = new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')); $type = $index->getType('zeroType'); - $type->addDocuments($docs); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(2, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(3, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(4, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(5, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(6, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(7, array('id' => 1, 'email' => 'test@test.com', 'username' => 'farrelley')), + new Document(8, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(9, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(10, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + new Document(11, array('id' => 1, 'email' => 'test@test.com', 'username' => 'bunny')), + )); $index->refresh(); $search->addIndex($index)->addType($type); @@ -470,31 +526,122 @@ class SearchTest extends BaseTest $source = $resultSet->current()->getSource(); $this->assertEquals('bunny', $source['username']); } - - public function testCount() { - $index = $this->_createIndex('eeee'); + + /** + * @group functional + */ + public function testCount() + { + $index = $this->_createIndex(); $search = new Search($index->getClient()); - $type = $index->getType('test'); - + $type = $index->getType('test'); + $doc = new Document(1, array('id' => 1, 'username' => 'ruflin')); - - $type->addDocument($doc); - $index->refresh(); - - $search->addIndex($index); - $search->addType($type); - - $result1 = $search->count(new \Elastica\Query\MatchAll()); - $this->assertEquals(1, $result1); - - - $result2 = $search->count(new \Elastica\Query\MatchAll(), true); - $this->assertInstanceOf('\Elastica\ResultSet', $result2); - $this->assertEquals(1, $result2->getTotalHits()); - } - - public function testScanAndScroll() { + + $type->addDocument($doc); + $index->refresh(); + + $search->addIndex($index); + $search->addType($type); + + $result1 = $search->count(new \Elastica\Query\MatchAll()); + $this->assertEquals(1, $result1); + + $result2 = $search->count(new \Elastica\Query\MatchAll(), true); + $this->assertInstanceOf('\Elastica\ResultSet', $result2); + $this->assertEquals(1, $result2->getTotalHits()); + } + + /** + * @group functional + */ + public function testScanAndScroll() + { $search = new Search($this->_getClient()); $this->assertInstanceOf('Elastica\ScanAndScroll', $search->scanAndScroll()); } + + /** + * @group functional + */ + public function testIgnoreUnavailableOption() + { + $client = $this->_getClient(); + $index = $client->getIndex('elastica_7086b4c2ee585bbb6740ece5ed7ece01'); + $query = new MatchAll(); + + $search = new Search($client); + $search->addIndex($index); + + $exception = null; + try { + $search->search($query); + } catch (ResponseException $e) { + $exception = $e; + } + $this->assertEquals('IndexMissingException', $exception->getElasticsearchException()->getExceptionName()); + + $results = $search->search($query, array(Search::OPTION_SEARCH_IGNORE_UNAVAILABLE => true)); + $this->assertInstanceOf('\Elastica\ResultSet', $results); + } + + /** + * @group functional + */ + public function testQueryCacheOption() + { + $client = $this->_getClient(); + + $index = $client->getIndex('zero'); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + $type = $index->getType('zeroType'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'username' => 'farrelley')), + new Document(2, array('id' => 2, 'username' => 'bunny')), + )); + $index->refresh(); + + $aggregation = new Aggregation\Terms('username'); + $aggregation->setField('username'); + + $query = new Query(); + $query->addAggregation($aggregation); + + $search = new Search($client); + $search->addIndex($index); + $search->setQuery($query); + $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT); + $search->setOption(Search::OPTION_QUERY_CACHE, true); + + // before search query cache should be empty + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(0, $queryCache['hit_count']); + $this->assertEquals(0, $queryCache['miss_count']); + + // first search should result in cache miss and save data to cache + $search->search(); + $index->getStats()->refresh(); + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(0, $queryCache['hit_count']); + $this->assertEquals(1, $queryCache['miss_count']); + + // next search should result in cache hit + $search->search(); + $index->getStats()->refresh(); + $statsData = $index->getStats()->getData(); + $queryCache = $statsData['_all']['primaries']['query_cache']; + + $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']); + $this->assertEquals(0, $queryCache['evictions']); + $this->assertEquals(1, $queryCache['hit_count']); + $this->assertEquals(1, $queryCache['miss_count']); + } } |