Class yii\elasticsearch\Query
Inheritance | yii\elasticsearch\Query » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable, yii\db\QueryInterface |
Uses Traits | yii\db\QueryTrait |
Subclasses | yii\elasticsearch\ActiveQuery |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-elasticsearch/blob/master/Query.php |
Query represents a query to the search API of elasticsearch.
Query provides a set of methods to facilitate the specification of different parameters of the query. These methods can be chained together.
By calling createCommand(), we can get a yii\elasticsearch\Command instance which can be further used to perform/execute the DB query against a database.
For example,
$query = new Query;
$query->fields('id, name')
->from('myindex', 'users')
->limit(10);
// build and execute the query
$command = $query->createCommand();
$rows = $command->search(); // this way you get the raw output of elasticsearch.
You would normally call $query->search()
instead of creating a command as this method
adds the indexBy()
feature and also removes some inconsistencies from the response.
Query also provides some methods to easier get some parts of the result only:
- 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.
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.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$aggregations | array | List of aggregations to add to this query. | yii\elasticsearch\Query |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$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 |
$indexBy | string|callable | The name of the column by which the query results should be indexed by. | yii\db\QueryTrait |
$limit | integer | Maximum number of records to be returned. | yii\db\QueryTrait |
$offset | integer | Zero-based offset from where the records are to be returned. | yii\db\QueryTrait |
$orderBy | array | How to sort the query results. | yii\db\QueryTrait |
$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 |
$where | string|array | Query condition. | yii\db\QueryTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Object |
__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() | Adds an aggregation to this query. | yii\elasticsearch\Query |
addAggregation() | Adds an aggregation to this query. | 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 the query and returns all results as an array. | yii\elasticsearch\Query |
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 |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
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\Object |
column() | Executes the query and returns the first column of the result. | yii\elasticsearch\Query |
count() | Returns the number of records. | yii\elasticsearch\Query |
createCommand() | Creates a DB command that can be used to execute this query. | yii\elasticsearch\Query |
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 |
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 |
fields() | Sets the fields to retrieve from the documents. | yii\elasticsearch\Query |
filter() | Sets the filter part of this search query. | yii\elasticsearch\Query |
filterWhere() | Sets the WHERE part of the query but ignores empty operands. | yii\db\QueryTrait |
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\Query |
limit() | Sets the LIMIT part of the query. | yii\db\QueryTrait |
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 the query and returns a single row of result. | 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 |
query() | Sets the querypart 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\Query |
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 |
trigger() | Triggers an event. | yii\base\Component |
where() | Sets the WHERE part of the query. | yii\db\QueryTrait |
Protected Methods
Method | Description | Defined By |
---|---|---|
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 |
Property Details
List of aggregations to add to this query.
See also http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html.
The fields being retrieved from the documents. For example, ['id', 'name']
.
If not set, this option will not be applied to the query and no fields will be returned.
In this case the _source
field will be returned by default which can be configured using source().
Setting this to an empty array will result in no fields being retrieved, which means that only the primaryKey
of a record will be available in the result.
For each field you may also add an array representing a script field. Example:
$query->fields = [
'id',
'name',
'value_times_two' => [
'script' => "doc['my_field_name'].value * 2",
],
'value_times_factor' => [
'script' => "doc['my_field_name'].value * factor",
'params' => [
'factor' => 2.0
],
],
]
Note: Field values are always returned as arrays even if they only have one value.
See also:
- http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html#search-request-fields
- http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
- fields()
- source()
The filter part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
The highlight part of this search query. This is an array that allows to highlight search results on one or more fields.
See also http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-request-highlighting.html.
The index to retrieve data from. This can be a string representing a single index or a an array of multiple indexes. If this is not set, indexes are being queried.
See also from().
The query part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
This option controls how the _source
field is returned from the documents. For example, ['id', 'name']
means that only the id
and name
field should be returned from _source
.
If not set, it means retrieving the full _source
field unless fields() are specified.
Setting this option to false
will disable return of the _source
field, this means that only the primaryKey
of a record will be available in the result.
See also:
- http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
- source()
- fields()
The 'stats' part of the query. An array of groups to maintain a statistics aggregation for.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search.html#stats-groups.
List of suggesters to add to this query.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html.
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. Defaults to no timeout.
See also:
- timeout()
- http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#_parameters_5
The type to retrieve data from. This can be a string representing a single type or a an array of multiple types. If this is not set, all types are being queried.
See also from().
Method Details
Adds an aggregation to this query.
This is an alias for addAggregation().
See also http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html.
static addAgg( $name, $type, $options ) | ||
$name | string | The name of the aggregation |
$type | string | The aggregation type. e.g. |
$options | string|array | The configuration options for this aggregation. Can be an array or a json string. |
return | static | The query object itself |
---|
Adds an aggregation to this query.
See also http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html.
static addAggregation( $name, $type, $options ) | ||
$name | string | The name of the aggregation |
$type | string | The aggregation type. e.g. |
$options | string|array | The configuration options for this aggregation. Can be an array or a json string. |
return | static | The query object itself |
---|
Adds a suggester to this query.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html.
static addSuggester( $name, $definition ) | ||
$name | string | The name of the suggester |
$definition | string|array | The configuration options for this suggester. Can be an array or a json string. |
return | static | The query object itself |
---|
Executes the query and returns all results as an array.
array all( $db = null ) | ||
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
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.
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. |
---|
Returns the number of records.
integer count( $q = '*', $db = null ) | ||
$q | string | The COUNT expression. This parameter is ignored by this implementation. |
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | integer | Number of records |
---|
Creates a DB command that can be used to execute this query.
yii\elasticsearch\Command createCommand( $db = null ) | ||
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | yii\elasticsearch\Command | The created DB command instance. |
---|
Executes the query and deletes all matching documents.
Everything except query and filter will be ignored.
array delete( $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. |
return | array | The query results. |
---|
Returns a value indicating whether the query result contains any row of data.
boolean exists( $db = null ) | ||
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | boolean | Whether the query result contains any row of data. |
---|
Sets the fields to retrieve from the documents.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html.
static fields( $fields ) | ||
$fields | array | The fields to be selected. |
return | static | The query object itself |
---|
Sets the filter part of this search query.
static filter( $filter ) | ||
$filter | string | |
return | static | The query object itself |
---|
Sets the index and type to retrieve documents from.
static from( $index, $type = null ) | ||
$index | string|array | The index to retrieve data from. This can be a string representing a single index
or a an array of multiple indexes. If this is |
$type | string|array | The type to retrieve data from. This can be a string representing a single type
or a an array of multiple types. If this is |
return | static | The query object itself |
---|
Sets a highlight parameters to retrieve from the documents.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html.
static highlight( $highlight ) | ||
$highlight | array | Array of parameters to highlight results. |
return | static | The query object itself |
---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
void init( ) |
Executes the query and returns a single row of result.
array|boolean one( $db = null ) | ||
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | array|boolean | The first row (in terms of an array) of the query result. False is returned if the query results in nothing. |
---|
Sets the querypart of this search query.
static query( $query ) | ||
$query | string | |
return | static | The query object itself |
---|
Returns the query result as a scalar value.
The value returned will be the specified field in the first document of the query results.
string scalar( $field, $db = null ) | ||
$field | string | Name of the attribute to select |
$db | yii\elasticsearch\Connection | The database connection used to execute the query.
If this parameter is not given, the |
return | string | The value of the specified attribute in the first record of the query result. Null is returned if the query result is empty or the field does not exist. |
---|
Executes the query and returns the complete search result including e.g. hits, facets, totalCount.
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. |
---|
Sets the source filtering, specifying how the _source
field of the document should be returned.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html.
static source( $source ) | ||
$source | array | The source patterns to be selected. |
return | static | The query object itself |
---|
Adds a 'stats' part to the query.
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search.html#stats-groups.
static stats( $groups ) | ||
$groups | array | An array of groups to maintain a statistics aggregation for. |
return | static | The query object itself |
---|
Sets the search timeout.
static timeout( $timeout ) | ||
$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. Defaults to no timeout. |
return | static | The query object itself |
---|