Class yii\elasticsearch\ActiveQuery
Inheritance | yii\elasticsearch\ActiveQuery » yii\elasticsearch\Query » yii\base\Component |
---|---|
Implements | yii\db\ActiveQueryInterface, yii\db\QueryInterface |
Uses Traits | yii\db\ActiveQueryTrait, yii\db\ActiveRelationTrait, yii\db\QueryTrait |
Available since version | 2.0 |
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 \yii\elasticsearch\where(), \yii\elasticsearch\orderBy() to customize the query options.
ActiveQuery also provides the following additional query options:
- \yii\elasticsearch\with(): list of relations that this query should be performed with.
- \yii\elasticsearch\indexBy(): the name of the column by which the query result should be indexed.
- \yii\elasticsearch\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 \yii\elasticsearch\link which represents the association between columns of different tables; and the multiplicity of the relation is indicated by \yii\elasticsearch\multiple.
If a relation involves a junction table, it may be specified by \yii\elasticsearch\via(). This methods may only be called in a relational context. Same is true for \yii\elasticsearch\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 \yii\elasticsearch\via() so that if via records are limited to 10 the relations records can also not be more than 10.
Note: Currently \yii\elasticsearch\with is not supported in combination with \yii\elasticsearch\asArray.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$aggregations | array | List of aggregations to add to this query. | yii\elasticsearch\Query |
$explain | boolean | Enables explanation for each hit on how its score was computed. | yii\elasticsearch\Query |
$fields | array | The fields being retrieved from the documents. | 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 |
$minScore | float | Exclude documents which have a _score less than the minimum specified in min_score | yii\elasticsearch\Query |
$options | array | List of options that will passed to commands created by this query. | yii\elasticsearch\Query |
$postFilter | string|array | The post_filter part of the search query for differentially filter search results and aggregations. |
yii\elasticsearch\Query |
$query | array|string | The query part of this search query. | 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 |
$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 |
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | Constructor. | yii\elasticsearch\ActiveQuery |
addAgg() | yii\elasticsearch\Query | |
addAggregate() | Adds an aggregation to this query. Supports nested aggregations. | yii\elasticsearch\Query |
addAggregation() | yii\elasticsearch\Query | |
addOptions() | Adds more options, overwriting existing options. | yii\elasticsearch\Query |
addSuggester() | Adds a suggester to this query. | yii\elasticsearch\Query |
all() | Executes query and returns all results as an array. | yii\elasticsearch\ActiveQuery |
batch() | Starts a batch query. | yii\elasticsearch\Query |
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 |
each() | Starts a batch query and retrieves data row by row. | yii\elasticsearch\Query |
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 |
fields() | Sets the fields to retrieve from the documents. | yii\elasticsearch\Query |
filter() | Sets the filter part of this search query. | yii\elasticsearch\Query |
from() | Sets the index and type to retrieve documents from. | yii\elasticsearch\Query |
highlight() | Sets a highlight parameters to retrieve from the documents. | yii\elasticsearch\Query |
init() | Initializes the object. | yii\elasticsearch\ActiveQuery |
minScore() | yii\elasticsearch\Query | |
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 |
populate() | Converts the raw query results into the format as specified by this query. | yii\elasticsearch\ActiveQuery |
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. | yii\elasticsearch\Query |
search() | Executes the query and returns the complete search result including e.g. hits, facets, totalCount. | 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 |
timeout() | Sets the search timeout. | yii\elasticsearch\Query |
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
Constructor.
public void __construct ( $modelClass, $config = [] ) | ||
$modelClass | array | The model class associated with this query |
$config | array | Configurations to be applied to the newly created query object |
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 \yii\elasticsearch\modelClass will be used. |
return | array | The query results. If the query results in nothing, an empty array will be returned. |
---|
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 |
return | array | The first column of the query result. An empty array is returned if the query results in nothing. |
---|
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 \yii\elasticsearch\modelClass will be used. |
return | yii\elasticsearch\Command | The created DB command instance. |
---|
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 ( ) |
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 \yii\elasticsearch\modelClass will be used. |
return | yii\elasticsearch\ActiveRecord|array|null | A single row of query result. Depending on the setting of \yii\elasticsearch\asArray, the query result may be either an array or an ActiveRecord object. Null will be returned if the query results in nothing. |
---|
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 |
---|
Executes the query and returns the complete search result including e.g. hits, facets, 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 |
$options | array | The options given with this query. Possible options are: |
return | array | The query results. |
---|
Event Details
An event that is triggered when the query is initialized via init().