Permalink
Browse files

Added Search::hasType and hasIndex methods. Fixed Index::count() method

  • Loading branch information...
1 parent 59aa996 commit 8ca115678c166ca70b098aabf4031de431ff0b40 @munkie munkie committed Mar 18, 2013
View
@@ -273,8 +273,9 @@ public function send()
/**
* @param \Elastica\Response $response
+ * @throws Exception\Bulk\ResponseException
+ * @throws Exception\InvalidException
* @return \Elastica\Bulk\ResponseSet
- * @throws \Elastica\Exception\Bulk\ResponseException
*/
protected function _processResponse(Response $response)
{
@@ -291,11 +292,13 @@ protected function _processResponse(Response $response)
throw new InvalidException('No response found for action #' . $key);
}
+ $action = $actions[$key];
+
$opType = key($item);
$bulkResponseData = reset($item);
- if ($actions[$key] instanceof AbstractDocumentAction) {
- $document = $actions[$key]->getDocument();
+ if ($action instanceof AbstractDocumentAction) {
+ $document = $action->getDocument();
if ($document->isAutoPopulate()
|| $this->_client->getConfigValue(array('document', 'autoPopulate'), false)
) {
@@ -308,7 +311,7 @@ protected function _processResponse(Response $response)
}
}
- $bulkResponses[] = new BulkResponse($bulkResponseData, $actions[$key], $opType);
+ $bulkResponses[] = new BulkResponse($bulkResponseData, $action, $opType);
}
}
View
@@ -232,6 +232,7 @@ public function createSearch($query = '', $options = null)
{
$search = new Search($this->getClient());
$search->addIndex($this);
+ $search->setOptionsAndQuery($options, $query);
return $search;
}
@@ -248,7 +249,7 @@ public function search($query = '', $options = null)
{
$search = $this->createSearch($query, $options);
- return $search->search($query, $options);
+ return $search->search();
}
/**
View
@@ -298,6 +298,19 @@ public function hasIndices()
}
/**
+ * @param Index|string $index
+ * @return bool
+ */
+ public function hasIndex($index)
+ {
+ if ($index instanceof Index) {
+ $index = $index->getName();
+ }
+
+ return in_array($index, $this->_indices);
+ }
+
+ /**
* Return array of types
*
* @return array List of types
@@ -316,6 +329,19 @@ public function hasTypes()
}
/**
+ * @param \Elastica\Type|string $type
+ * @return bool
+ */
+ public function hasType($type)
+ {
+ if ($type instanceof Type) {
+ $type = $type->getName();
+ }
+
+ return in_array($type, $this->_types);
+ }
+
+ /**
* @return \Elastica\Query
*/
public function getQuery()
@@ -5,6 +5,8 @@
use Elastica\Client;
use Elastica\Document;
use Elastica\Exception\ResponseException;
+use Elastica\Index;
+use Elastica\Query\QueryString;
use Elastica\Status;
use Elastica\Type;
use Elastica\Type\Mapping;
@@ -501,4 +503,64 @@ public function testCreateArray()
$status = new Status($client);
$this->assertTrue($status->indexExists($indexName));
}
+
+ public function testCreateSearch()
+ {
+ $client = $this->_getClient();
+ $index = new Index($client, 'test');
+
+ $query = new QueryString('test');
+ $options = 5;
+
+ $search = $index->createSearch($query, $options);
+
+ $expected = array(
+ 'query' => array(
+ 'query_string' => array(
+ 'query' => 'test'
+ )
+ ),
+ 'size' => 5
+ );
+ $this->assertEquals($expected, $search->getQuery()->toArray());
+ $this->assertEquals(array('test'), $search->getIndices());
+ $this->assertTrue($search->hasIndices());
+ $this->assertTrue($search->hasIndex('test'));
+ $this->assertTrue($search->hasIndex($index));
+ $this->assertEquals(array(), $search->getTypes());
+ $this->assertFalse($search->hasTypes());
+ $this->assertFalse($search->hasType('test_type'));
+
+ $type = new Type($index, 'test_type2');
+ $this->assertFalse($search->hasType($type));
+ }
+
+ public function testSearch()
+ {
+ $index = $this->_createIndex();
+
+ $type = new Type($index, 'user');
+
+ $docs = array();
+ $docs[] = new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5')));
+ $docs[] = new Document(2, array('username' => 'john', 'test' => array('1', '3', '6')));
+ $docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')));
+ $type->addDocuments($docs);
+ $index->refresh();
+
+ $resultSet = $index->search('rolf');
+ $this->assertEquals(1, $resultSet->count());
+
+ $count = $index->count('rolf');
+ $this->assertEquals(1, $count);
+
+ // Test if source is returned
+ $result = $resultSet->current();
+ $this->assertEquals(3, $result->getId());
+ $data = $result->getData();
+ $this->assertEquals('rolf', $data['username']);
+
+ $count = $index->count();
+ $this->assertEquals(3, $count);
+ }
}
@@ -54,6 +54,42 @@ public function testSearch()
$this->assertEquals('rolf', $data['username']);
}
+ public function testCreateSearch()
+ {
+ $client = $this->_getClient();
+ $index = new Index($client, 'test_index');
+ $type = new Type($index, 'test_type');
+
+ $query = new Query\QueryString('test');
+ $options = array(
+ 'limit' => 5,
+ 'explain' => true,
+ );
+
+ $search = $type->createSearch($query, $options);
+
+ $expected = array(
+ 'query' => array(
+ 'query_string' => array(
+ 'query' => 'test'
+ )
+ ),
+ 'size' => 5,
+ 'explain' => true
+ );
+ $this->assertEquals($expected, $search->getQuery()->toArray());
+ $this->assertEquals(array('test_index'), $search->getIndices());
+ $this->assertTrue($search->hasIndices());
+ $this->assertTrue($search->hasIndex($index));
+ $this->assertTrue($search->hasIndex('test_index'));
+ $this->assertFalse($search->hasIndex('test'));
+ $this->assertEquals(array('test_type'), $search->getTypes());
+ $this->assertTrue($search->hasTypes());
+ $this->assertTrue($search->hasType($type));
+ $this->assertTrue($search->hasType('test_type'));
+ $this->assertFalse($search->hasType('test_type2'));
+ }
+
public function testNoSource()
{
$index = $this->_createIndex();

0 comments on commit 8ca1156

Please sign in to comment.