Class Yiisoft\Db\ElasticSearch\QueryBuilder
| Inheritance | Yiisoft\Db\ElasticSearch\QueryBuilder » yii\base\BaseObject |
|---|---|
| Available since version | 2.0 |
QueryBuilder builds an elasticsearch query based on the specification given as a Yiisoft\Db\ElasticSearch\Query object.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $db | Yiisoft\Db\ElasticSearch\Connection | The database connection. | Yiisoft\Db\ElasticSearch\QueryBuilder |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Constructor. | Yiisoft\Db\ElasticSearch\QueryBuilder |
| build() | Generates query from a Yiisoft\Db\ElasticSearch\Query object. | Yiisoft\Db\ElasticSearch\QueryBuilder |
| buildCondition() | Parses the condition specification and generates the corresponding SQL expression. | Yiisoft\Db\ElasticSearch\QueryBuilder |
| buildOrderBy() | Adds order by condition to the query | Yiisoft\Db\ElasticSearch\QueryBuilder |
| buildQueryFromWhere() | Yiisoft\Db\ElasticSearch\QueryBuilder |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildCompositeInCondition() | Yiisoft\Db\ElasticSearch\QueryBuilder |
Property Details
Method Details
Constructor.
| public mixed __construct ( Yiisoft\Db\ElasticSearch\Connection $connection, array $config = [] ) | ||
| $connection | Yiisoft\Db\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);
}
Generates query from a Yiisoft\Db\ElasticSearch\Query object.
| public array build ( Yiisoft\Db\ElasticSearch\Query $query ) | ||
| $query | Yiisoft\Db\ElasticSearch\Query |
The Yiisoft\Db\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->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;
}
$whereQuery = $this->buildQueryFromWhere($query->where);
if ($whereQuery) {
$parts['query'] = $whereQuery;
} elseif ($query->query) {
$parts['query'] = $query->query;
}
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;
}
$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,
];
}
| protected mixed buildCompositeInCondition ( mixed $operator, mixed $columns, mixed $values ) | ||
| $operator | mixed | |
| $columns | mixed | |
| $values | mixed | |
protected function buildCompositeInCondition($operator, $columns, $values)
{
throw new NotSupportedException('composite in is not supported by elasticsearch.');
}
Parses the condition specification and generates the corresponding SQL expression.
| public string buildCondition ( array|string $condition ) | ||
| $condition | array|string |
The condition specification. Please refer to Query::where() on how to specify a condition. |
| return | string |
The generated SQL expression |
|---|---|---|
| throws | \yii\base\InvalidParamException |
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',
];
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);
}
throw new InvalidParamException('Found unknown operator in query: ' . $operator);
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return $this->buildHashCondition($condition);
}
}
Adds order by condition to the query
| public mixed buildOrderBy ( mixed $columns ) | ||
| $columns | mixed | |
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 ($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;
}
| public mixed buildQueryFromWhere ( mixed $condition ) | ||
| $condition | mixed | |
public function buildQueryFromWhere($condition)
{
$where = $this->buildCondition($condition);
if ($where) {
return [
'constant_score' => [
'filter' => $where,
],
];
}
return null;
}
Signup or Login in order to comment.