yii\elasticsearch\QueryBuilder::build PHP Method

build() public method

Generates query from a Query object.
public build ( Query $query ) : array
$query Query the [[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->fields === []) {
            $parts['fields'] = [];
        } elseif ($query->fields !== null) {
            $fields = [];
            $scriptFields = [];
            foreach ($query->fields as $key => $field) {
                if (is_int($key)) {
                    $fields[] = $field;
                } else {
                    $scriptFields[$key] = $field;
                }
            }
            if (!empty($fields)) {
                $parts['fields'] = $fields;
            }
            if (!empty($scriptFields)) {
                $parts['script_fields'] = $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'] = (double) $query->minScore;
        }
        if (empty($query->query)) {
            $parts['query'] = ["match_all" => (object) []];
        } else {
            $parts['query'] = $query->query;
        }
        $whereFilter = $this->buildCondition($query->where);
        if (is_string($query->filter)) {
            if (empty($whereFilter)) {
                $parts['filter'] = $query->filter;
            } else {
                $parts['filter'] = '{"and": [' . $query->filter . ', ' . Json::encode($whereFilter) . ']}';
            }
        } elseif ($query->filter !== null) {
            if (empty($whereFilter)) {
                $parts['filter'] = $query->filter;
            } else {
                $parts['filter'] = ['and' => [$query->filter, $whereFilter]];
            }
        } elseif (!empty($whereFilter)) {
            $parts['filter'] = $whereFilter;
        }
        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];
    }

Usage Example

コード例 #1
0
 /**
  * Generates query from a [[Query]] object.
  * @param Query $query the [[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)
 {
     if ($query->query instanceof Param) {
         $query->query = $query->query->toArray();
     }
     return parent::build($query);
 }
All Usage Examples Of yii\elasticsearch\QueryBuilder::build