yii\elasticsearch\Query::one PHP Méthode

one() public méthode

Executes the query and returns a single row of result.
public one ( Connection $db = null ) : array | boolean
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
Résultat array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.
    public function one($db = null)
    {
        $result = $this->createCommand($db)->search(['size' => 1]);
        if (empty($result['hits']['hits'])) {
            return false;
        }
        $record = reset($result['hits']['hits']);
        return $record;
    }

Usage Example

Exemple #1
0
 public function testOne()
 {
     $query = new Query();
     $query->from('yiitest', 'user');
     $result = $query->one($this->getConnection());
     $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);
     $result = $query->where(['name' => 'user1'])->one($this->getConnection());
     $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']);
     $result = $query->where(['name' => 'user5'])->one($this->getConnection());
     $this->assertFalse($result);
 }
All Usage Examples Of yii\elasticsearch\Query::one