Class yii\elasticsearch\QueryBuilder
Inheritance | yii\elasticsearch\QueryBuilder » yii\base\BaseObject |
---|---|
Available since extension's version | 2.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
Property | Type | Description | Defined By |
---|---|---|---|
$db | yii\elasticsearch\Connection | The database connection. | yii\elasticsearch\QueryBuilder |
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | Constructor. | yii\elasticsearch\QueryBuilder |
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 |
Protected Methods
Method | Description | Defined By |
---|---|---|
buildCompositeInCondition() | yii\elasticsearch\QueryBuilder |
Property Details
Method Details
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);
}
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,
];
}
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.');
}
Parses the condition specification and generates the corresponding SQL expression.
public string buildCondition ( $condition ) | ||
$condition | string|array |
The condition specification. Please refer to 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);
}
}
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;
}
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;
}
}