yii\elasticsearch\Query::all PHP Method

all() public method

Executes the query and returns all results as an array.
public all ( Connection $db = null ) : array
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return array the query results. If the query results in nothing, an empty array will be returned.
    public function all($db = null)
    {
        $result = $this->createCommand($db)->search();
        if (empty($result['hits']['hits'])) {
            return [];
        }
        $rows = $result['hits']['hits'];
        return $this->populate($rows);
    }

Usage Example

示例#1
0
 public function testAll()
 {
     $query = new Query();
     $query->from('yiitest', 'user');
     $results = $query->all($this->getConnection());
     $this->assertEquals(4, count($results));
     $result = reset($results);
     $this->assertEquals(3, count($result['_source']));
     $this->assertArrayHasKey('status', $result['_source']);
     $this->assertArrayHasKey('email', $result['_source']);
     $this->assertArrayHasKey('name', $result['_source']);
     $this->assertArrayHasKey('_id', $result);
     $query = new Query();
     $query->from('yiitest', 'user');
     $results = $query->where(['name' => 'user1'])->all($this->getConnection());
     $this->assertEquals(1, count($results));
     $result = reset($results);
     $this->assertEquals(3, count($result['_source']));
     $this->assertArrayHasKey('status', $result['_source']);
     $this->assertArrayHasKey('email', $result['_source']);
     $this->assertArrayHasKey('name', $result['_source']);
     $this->assertArrayHasKey('_id', $result);
     $this->assertEquals(1, $result['_id']);
     // indexBy
     $query = new Query();
     $query->from('yiitest', 'user');
     $results = $query->indexBy('name')->all($this->getConnection());
     $this->assertEquals(4, count($results));
     ksort($results);
     $this->assertEquals(['user1', 'user2', 'user3', 'user4'], array_keys($results));
 }
All Usage Examples Of yii\elasticsearch\Query::all