Class yii\elasticsearch\QueryBuilder

Inheritanceyii\elasticsearch\QueryBuilder » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2-elasticsearch/blob/master/QueryBuilder.php

QueryBuilder builds an Elasticsearch query based on the specification given as a yii\elasticsearch\Query object.

Public Properties

Hide inherited properties

Property Type Description Defined By
$db yii\elasticsearch\Connection The database connection. yii\elasticsearch\QueryBuilder

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\elasticsearch\QueryBuilder
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
build() Generates query from a yii\elasticsearch\Query object. yii\elasticsearch\QueryBuilder
buildCondition() Parses the condition specification and generates the corresponding SQL expression. yii\elasticsearch\QueryBuilder
buildOrderBy() Adds order by condition to the query yii\elasticsearch\QueryBuilder
buildQueryFromWhere() yii\elasticsearch\QueryBuilder
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject

Property Details

Hide inherited properties

$db public property

The database connection.

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Constructor.

public void __construct ( $connection, $config = [] )
$connection yii\elasticsearch\Connection

The database connection.

$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($connection, $config = [])
{
    $this->db = $connection;
    parent::__construct($config);
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
build() public method

Generates query from a yii\elasticsearch\Query object.

public array build ( $query )
$query yii\elasticsearch\Query

The yii\elasticsearch\Query object from which the query will be generated

return array

The generated SQL statement (the first array element) and the corresponding parameters to be bound to the SQL statement (the second array element).

                public function build($query)
{
    $parts = [];
    if ($query->storedFields !== null) {
        $parts['stored_fields'] = $query->storedFields;
    }
    if ($query->scriptFields !== null) {
        $parts['script_fields'] = $query->scriptFields;
    }
    if ($query->runtimeMappings !== null) {
        $parts['runtime_mappings'] = $query->runtimeMappings;
    }
    if ($query->fields !== null) {
        $parts['fields'] = $query->fields;
    }
    if ($query->source !== null) {
        $parts['_source'] = $query->source;
    }
    if ($query->limit !== null && $query->limit >= 0) {
        $parts['size'] = $query->limit;
    }
    if ($query->offset > 0) {
        $parts['from'] = (int)$query->offset;
    }
    if (isset($query->minScore)) {
        $parts['min_score'] = (float)$query->minScore;
    }
    if (isset($query->explain)) {
        $parts['explain'] = $query->explain;
    }
    // combine query with where
    $conditionals = [];
    $whereQuery = $this->buildQueryFromWhere($query->where);
    if ($whereQuery) {
        $conditionals[] = $whereQuery;
    }
    if ($query->query) {
        $conditionals[] = $query->query;
    }
    if (count($conditionals) === 2) {
        $parts['query'] = ['bool' => ['must' => $conditionals]];
    } elseif (count($conditionals) === 1) {
        $parts['query'] = reset($conditionals);
    }
    if (!empty($query->highlight)) {
        $parts['highlight'] = $query->highlight;
    }
    if (!empty($query->aggregations)) {
        $parts['aggregations'] = $query->aggregations;
    }
    if (!empty($query->stats)) {
        $parts['stats'] = $query->stats;
    }
    if (!empty($query->suggest)) {
        $parts['suggest'] = $query->suggest;
    }
    if (!empty($query->postFilter)) {
        $parts['post_filter'] = $query->postFilter;
    }
    if (!empty($query->collapse)) {
        $parts['collapse'] = $query->collapse;
    }
    $sort = $this->buildOrderBy($query->orderBy);
    if (!empty($sort)) {
        $parts['sort'] = $sort;
    }
    $options = $query->options;
    if ($query->timeout !== null) {
        $options['timeout'] = $query->timeout;
    }
    return [
        'queryParts' => $parts,
        'index' => $query->index,
        'type' => $query->type,
        'options' => $options,
    ];
}

            
buildCompositeInCondition() protected method

protected void buildCompositeInCondition ( $operator, $columns, $values )
$operator
$columns
$values

                protected function buildCompositeInCondition($operator, $columns, $values)
{
    throw new NotSupportedException('composite in is not supported by Elasticsearch.');
}

            
buildCondition() public method

Parses the condition specification and generates the corresponding SQL expression.

public string buildCondition ( $condition )
$condition string|array

The condition specification. Please refer to yii\elasticsearch\Query::where() on how to specify a condition.

return string

The generated SQL expression

throws yii\base\InvalidArgumentException

if unknown operator is used in query

throws yii\base\NotSupportedException

if string conditions are used in where

                public function buildCondition($condition)
{
    static $builders = [
        'not' => 'buildNotCondition',
        'and' => 'buildBoolCondition',
        'or' => 'buildBoolCondition',
        'between' => 'buildBetweenCondition',
        'not between' => 'buildBetweenCondition',
        'in' => 'buildInCondition',
        'not in' => 'buildInCondition',
        'like' => 'buildLikeCondition',
        'not like' => 'buildLikeCondition',
        'or like' => 'buildLikeCondition',
        'or not like' => 'buildLikeCondition',
        'lt' => 'buildHalfBoundedRangeCondition',
        '<' => 'buildHalfBoundedRangeCondition',
        'lte' => 'buildHalfBoundedRangeCondition',
        '<=' => 'buildHalfBoundedRangeCondition',
        'gt' => 'buildHalfBoundedRangeCondition',
        '>' => 'buildHalfBoundedRangeCondition',
        'gte' => 'buildHalfBoundedRangeCondition',
        '>=' => 'buildHalfBoundedRangeCondition',
        'match' => 'buildMatchCondition',
        'match_phrase' => 'buildMatchCondition',
    ];
    if (empty($condition)) {
        return [];
    }
    if (!is_array($condition)) {
        throw new NotSupportedException('String conditions in where() are not supported by Elasticsearch.');
    }
    if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
        $operator = strtolower($condition[0]);
        if (isset($builders[$operator])) {
            $method = $builders[$operator];
            array_shift($condition);
            return $this->$method($operator, $condition);
        } else {
            throw new InvalidArgumentException('Found unknown operator in query: ' . $operator);
        }
    } else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
        return $this->buildHashCondition($condition);
    }
}

            
buildOrderBy() public method

Adds order by condition to the query

public void buildOrderBy ( $columns )
$columns

                public function buildOrderBy($columns)
{
    if (empty($columns)) {
        return [];
    }
    $orders = [];
    foreach ($columns as $name => $direction) {
        if (is_string($direction)) {
            $column = $direction;
            $direction = SORT_ASC;
        } else {
            $column = $name;
        }
        if ($this->db->dslVersion < 7) {
            if ($column == '_id') {
                $column = '_uid';
            }
        }
        // allow Elasticsearch extended syntax as described in https://www.elastic.co/guide/en/elasticsearch/guide/master/_sorting.html
        if (is_array($direction)) {
            $orders[] = [$column => $direction];
        } else {
            $orders[] = [$column => ($direction === SORT_DESC ? 'desc' : 'asc')];
        }
    }
    return $orders;
}

            
buildQueryFromWhere() public method

public void buildQueryFromWhere ( $condition )
$condition

                public function buildQueryFromWhere($condition) {
    $where = $this->buildCondition($condition);
    if ($where) {
        $query = [
            'constant_score' => [
                'filter' => $where,
            ],
        ];
        return $query;
    } else {
        return null;
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}