# Class yii\elasticsearch\ActiveQuery

Inheritance | yii\elasticsearch\ActiveQuery » yii\elasticsearch\Query » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable, yii\db\ActiveQueryInterface, yii\db\QueryInterface
Uses Traits | yii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait, yii\db\QueryTrait
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2-elasticsearch/blob/master/ActiveQuery.php

ActiveQuery represents a yii\elasticsearch\Query associated with an yii\elasticsearch\ActiveRecord class.

An ActiveQuery can be a normal query or be used in a relational context.

ActiveQuery instances are usually created by yii\elasticsearch\ActiveRecord::find(). Relational queries are created by yii\elasticsearch\ActiveRecord::hasOne() and yii\elasticsearch\ActiveRecord::hasMany().

## Normal Query ¶

ActiveQuery mainly provides the following methods to retrieve the query results:

- one(): returns a single record populated with the first row of data.
- all(): returns all records based on the query results.
- count(): returns the number of records.
- scalar(): returns the value of the first column in the first row of the query result.
- column(): returns the value of the first column in the query result.
- exists(): returns a value indicating whether the query result has data or not.

Because ActiveQuery extends from yii\elasticsearch\Query, one can use query methods, such as where(), orderBy() to customize the query options.

ActiveQuery also provides the following additional query options:

- with(): list of relations that this query should be performed with.
- indexBy(): the name of the column by which the query result should be indexed.
- asArray(): whether to return each record as an array.

These options can be configured using methods of the same name. For example:

    $customers = Customer::find()->with('orders')->asArray()->all();

> Note: Elasticsearch limits the number of records returned to 10 records by default. If you expect to get more records you should specify limit explicitly.

## Relational query ¶

In relational context ActiveQuery represents a relation between two Active Record classes.

Relational ActiveQuery instances are usually created by calling yii\elasticsearch\ActiveRecord::hasOne() and yii\elasticsearch\ActiveRecord::hasMany(). An Active Record class declares a relation by defining a getter method which calls one of the above methods and returns the created ActiveQuery object.

A relation is specified by $link which represents the association between columns of different tables; and the multiplicity of the relation is indicated by $multiple.

If a relation involves a junction table, it may be specified by via(). This methods may only be called in a relational context. Same is true for inverseOf(), which marks a relation as inverse of another relation.

> Note: Elasticsearch limits the number of records returned by any query to 10 records by default. If you expect to get more records you should specify limit explicitly in relation definition. This is also important for relations that use via() so that if via records are limited to 10 the relations records can also not be more than 10.

> Note: Currently with() is not supported in combination with asArray().

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$aggregations | array | List of aggregations to add to this query. | yii\elasticsearch\Query
$asArray | boolean | Whether to return each record as an array. | yii\db\ActiveQueryTrait
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$collapse | array | List of collapse to add to this query. | yii\elasticsearch\Query
$emulateExecution | boolean | Whether to emulate the actual query execution, returning empty or false results. | yii\db\QueryTrait
$explain | boolean | Enables explanation for each hit on how its score was computed. | yii\elasticsearch\Query
$filter | array|string | The filter part of this search query. | yii\elasticsearch\Query
$highlight | array | The highlight part of this search query. | yii\elasticsearch\Query
$index | string|array | The index to retrieve data from. | yii\elasticsearch\Query
$indexBy | string|callable | The name of the column by which the query results should be indexed by. | yii\db\QueryTrait
$inverseOf | string | The name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait
$limit | integer|yii\db\ExpressionInterface | Maximum number of records to be returned. | yii\db\QueryTrait
$link | array | The columns of the primary and foreign tables that establish a relation. | yii\db\ActiveRelationTrait
$minScore | float | Exclude documents which have a _score less than the minimum specified in min_score | yii\elasticsearch\Query
$modelClass | string | The name of the ActiveRecord class. | yii\db\ActiveQueryTrait
$multiple | boolean | Whether this query represents a relation to more than one record. | yii\db\ActiveRelationTrait
$offset | integer|yii\db\ExpressionInterface | Zero-based offset from where the records are to be returned. | yii\db\QueryTrait
$options | array | List of options that will passed to commands created by this query. | yii\elasticsearch\Query
$orderBy | array | How to sort the query results. | yii\db\QueryTrait
$postFilter | string|array | The `post_filter` part of the search query for differentially filter search results and aggregations. | yii\elasticsearch\Query
$primaryModel | yii\db\ActiveRecord | The primary model of a relational query. | yii\db\ActiveRelationTrait
$query | array|string | The query part of this search query. | yii\elasticsearch\Query
$scriptFields | array | The scripted fields being retrieved from the documents. | yii\elasticsearch\Query
$source | array | This option controls how the `_source` field is returned from the documents. | yii\elasticsearch\Query
$stats | array | The 'stats' part of the query. | yii\elasticsearch\Query
$storedFields | array | The fields being retrieved from the documents. | yii\elasticsearch\Query
$suggest | array | List of suggesters to add to this query. | yii\elasticsearch\Query
$timeout | integer | A search timeout, bounding the search request to be executed within the specified time value and bail with the hits accumulated up to that point when expired. | yii\elasticsearch\Query
$type | string|array | The type to retrieve data from. | yii\elasticsearch\Query
$via | array|object | The query associated with the junction table. | yii\db\ActiveRelationTrait
$where | string|array|yii\db\ExpressionInterface | Query condition. | yii\db\QueryTrait
$with | array | A list of relations that this query should be performed with | yii\db\ActiveQueryTrait

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
() | ActiveRecordInterface[] all($db = null) | yii\db\ActiveRelationTrait
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | Clones internal objects. | yii\db\ActiveRelationTrait
__construct() | Constructor. | yii\elasticsearch\ActiveQuery
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
addAgg() |  | yii\elasticsearch\Query
addAggregate() | Adds an aggregation to this query. Supports nested aggregations. | yii\elasticsearch\Query
addAggregation() |  | yii\elasticsearch\Query
addCollapse() | Adds a collapse to this query. | yii\elasticsearch\Query
addOptions() | Adds more options, overwriting existing options. | yii\elasticsearch\Query
addOrderBy() | Adds additional ORDER BY columns to the query. | yii\db\QueryTrait
addSuggester() | Adds a suggester to this query. | yii\elasticsearch\Query
all() | Executes query and returns all results as an array. | yii\elasticsearch\ActiveQuery
andFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait
andWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait
asArray() | Sets the asArray() property. | yii\db\ActiveQueryTrait
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
batch() | Starts a batch query. | yii\elasticsearch\Query
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
column() | Executes the query and returns the first column of the result. | yii\elasticsearch\ActiveQuery
count() | Returns the number of records. | yii\elasticsearch\Query
createCommand() | Creates a DB command that can be used to execute this query. | yii\elasticsearch\ActiveQuery
delete() | Executes the query and deletes all matching documents. | yii\elasticsearch\Query
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
each() | Starts a batch query and retrieves data row by row. | yii\elasticsearch\Query
emulateExecution() | Sets whether to emulate query execution, preventing any interaction with data storage. | yii\db\QueryTrait
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
exists() | Returns a value indicating whether the query result contains any row of data. | yii\elasticsearch\Query
explain() | Explain for how the score of each document was computer | yii\elasticsearch\Query
filterWhere() | Sets the WHERE part of the query but ignores empty operands. | yii\db\QueryTrait
findFor() | Finds the related records for the specified primary record. | yii\db\ActiveRelationTrait
findWith() | Finds records corresponding to one or multiple relations and populates them into the primary models. | yii\db\ActiveQueryTrait
from() | Sets the index and type to retrieve documents from. | yii\elasticsearch\Query
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
highlight() | Sets a highlight parameters to retrieve from the documents. | yii\elasticsearch\Query
indexBy() | Sets the indexBy() property. | yii\db\QueryTrait
init() | Initializes the object. | yii\elasticsearch\ActiveQuery
inverseOf() | Sets the name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait
limit() | Sets the LIMIT part of the query. | yii\db\QueryTrait
minScore() |  | yii\elasticsearch\Query
off() | Detaches an existing event handler from this component. | yii\base\Component
offset() | Sets the OFFSET part of the query. | yii\db\QueryTrait
on() | Attaches an event handler to an event. | yii\base\Component
one() | Executes query and returns a single row of result. | yii\elasticsearch\ActiveQuery
options() | Sets the options to be passed to the command created by this query. | yii\elasticsearch\Query
orFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait
orWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait
orderBy() | Sets the ORDER BY part of the query. | yii\db\QueryTrait
populate() | Converts the raw query results into the format as specified by this query. This method is internally used to convert the data fetched from database into the format as required by this query. | yii\elasticsearch\ActiveQuery
populateRelation() | Finds the related records and populates them into the primary models. | yii\db\ActiveRelationTrait
postFilter() | Set the `post_filter` part of the search query. | yii\elasticsearch\Query
query() | Sets the query part of this search query. | yii\elasticsearch\Query
scalar() | Returns the query result as a scalar value. The value returned will be the specified field in the first document of the query results. | yii\elasticsearch\Query
scriptFields() | Sets the script fields to retrieve from the documents. | yii\elasticsearch\Query
search() | Executes the query and returns the complete search result including e.g. | yii\elasticsearch\ActiveQuery
source() | Sets the source filtering, specifying how the `_source` field of the document should be returned. | yii\elasticsearch\Query
stats() | Adds a 'stats' part to the query. | yii\elasticsearch\Query
storedFields() | Sets the fields to retrieve from the documents. | yii\elasticsearch\Query
timeout() | Sets the search timeout. | yii\elasticsearch\Query
trigger() | Triggers an event. | yii\base\Component
via() | Specifies the relation associated with the junction table. | yii\db\ActiveRelationTrait
where() | Sets the WHERE part of the query. | yii\db\QueryTrait
with() | Specifies the relations with which this query should be performed. | yii\db\ActiveQueryTrait

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
createModels() | Converts found rows into model instances. | yii\db\ActiveQueryTrait
filterCondition() | Removes empty operands from the given query condition. | yii\db\QueryTrait
isEmpty() | Returns a value indicating whether the give value is "empty". | yii\db\QueryTrait
normalizeOrderBy() | Normalizes format of ORDER BY data. | yii\db\QueryTrait

## Events

Hide inherited events

Event | Type | Description | Defined By
--- | --- | --- | ---
EVENT_INIT | \yii\elasticsearch\Event | An event that is triggered when the query is initialized via init(). | yii\elasticsearch\ActiveQuery

## Method Details

__construct() public method

Constructor.

public void __construct ( $modelClass, $config = [] )
$modelClass | string | The model class associated with this query
$config | array | Configurations to be applied to the newly created query object

all() public method

Executes query and returns all results as an array.

public array all ( $db = null )
$db | yii\elasticsearch\Connection | The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.
return | array | The query results. If the query results in nothing, an empty array will be returned.

column() public method

Executes the query and returns the first column of the result.

public array column ( $field, $db = null )
$field | string | The field to query over
$db | yii\elasticsearch\Connection | The database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return | array | The first column of the query result. An empty array is returned if the query results in nothing.

createCommand() public method

Creates a DB command that can be used to execute this query.

public yii\elasticsearch\Command createCommand ( $db = null )
$db | yii\elasticsearch\Connection | The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.
return | yii\elasticsearch\Command | The created DB command instance.

init() public method

Initializes the object.

This method is called at the end of the constructor. The default implementation will trigger an EVENT_INIT event. If you override this method, make sure you call the parent implementation at the end to ensure triggering of the event.

public void init ( )

one() public method

Executes query and returns a single row of result.

public yii\elasticsearch\ActiveRecord|array|null one ( $db = null )
$db | yii\elasticsearch\Connection | The DB connection used to create the DB command. If null, the DB connection returned by $modelClass will be used.
return | yii\elasticsearch\ActiveRecord|array|null | A single row of query result. Depending on the setting of asArray(), the query result may be either an array or an ActiveRecord object. Null will be returned if the query results in nothing.

populate() public method (available since version 2.0.4)

Converts the raw query results into the format as specified by this query. This method is internally used to convert the data fetched from database into the format as required by this query.

public array populate ( $rows )
$rows | array | The raw query result from database
return | array | The converted query result

search() public method

Executes the query and returns the complete search result including e.g.

hits, aggregations, suggesters, totalCount.

public array search ( $db = null, $options = [] )
$db | yii\elasticsearch\Connection | The database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
$options | array | The options given with this query. Possible options are: routing
return | array | The query results.

## Event Details

EVENT_INIT event of type \yii\elasticsearch\Event

An event that is triggered when the query is initialized via init().