PHP Class yii\elasticsearch\Query

Query provides a set of methods to facilitate the specification of different parameters of the query. These methods can be chained together. By calling Query::createCommand, we can get a 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: - Query::one: returns a single record populated with the first row of data. - Query::all: returns all records based on the query results. - Query::count: returns the number of records. - Query::scalar: returns the value of the first column in the first row of the query result. - Query::column: returns the value of the first column in the query result. - Query::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.
Since: 2.0
Author: Carsten Brandt ([email protected])
Inheritance: extends yii\base\Component, implements yii\db\QueryInterface, use trait yii\db\QueryTrait
Datei anzeigen Open project: yiisoft/yii2-elasticsearch Class Usage Examples

Public Properties

Property Type Description
$aggregations List of aggregations to add to this query.
$fields 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: php $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. [always returned as arrays]: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/_return_values.html#_return_values [script field]: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
$filter The filter part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
$highlight The highlight part of this search query. This is an array that allows to highlight search results on one or more fields.
$index 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.
$minScore Exclude documents which have a _score less than the minimum specified in min_score
$options list of options that will passed to commands created by this query.
$postFilter The post_filter part of the search query for differentially filter search results and aggregations.
$query The query part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
$source 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.
$stats the 'stats' part of the query. An array of groups to maintain a statistics aggregation for.
$suggest list of suggesters to add to this query.
$timeout 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.
$type 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.

Public Methods

Method Description
addAgg ( string $name, string $type, string | array $options )
addAggregate ( string $name, string | array $options ) Adds an aggregation to this query. Supports nested aggregations.
addAggregation ( string $name, string $type, string | array $options )
addOptions ( array $options ) Adds more options, overwriting existing options.
addSuggester ( string $name, string | array $definition ) Adds a suggester to this query.
all ( Connection $db = null ) : array Executes the query and returns all results as an array.
batch ( string $scrollWindow = '1m', Connection $db = null ) : BatchQueryResult Starts a batch query.
column ( string $field, Connection $db = null ) : array Executes the query and returns the first column of the result.
count ( string $q = '*', Connection $db = null ) : integer Returns the number of records.
createCommand ( Connection $db = null ) : Command Creates a DB command that can be used to execute this query.
delete ( Connection $db = null, array $options = [] ) : array Executes the query and deletes all matching documents.
each ( string $scrollWindow = '1m', Connection $db = null ) : BatchQueryResult Starts a batch query and retrieves data row by row.
exists ( Connection $db = null ) : boolean Returns a value indicating whether the query result contains any row of data.
fields ( array $fields ) Sets the fields to retrieve from the documents.
filter ( string $filter ) Sets the filter part of this search query.
from ( string | array $index, string | array $type = null ) Sets the index and type to retrieve documents from.
highlight ( array $highlight ) Sets a highlight parameters to retrieve from the documents.
init ( )
minScore ( float $minScore ) : static
one ( Connection $db = null ) : array | boolean Executes the query and returns a single row of result.
options ( array $options ) Sets the options to be passed to the command created by this query.
populate ( array $rows ) : array Converts the raw query results into the format as specified by this query.
postFilter ( string | array $filter ) Set the post_filter part of the search query.
query ( string $query ) Sets the querypart of this search query.
scalar ( string $field, Connection $db = null ) : string Returns the query result as a scalar value.
search ( Connection $db = null, array $options = [] ) : array Executes the query and returns the complete search result including e.g. hits, facets, totalCount.
source ( array $source ) Sets the source filtering, specifying how the _source field of the document should be returned.
stats ( array $groups ) Adds a 'stats' part to the query.
timeout ( integer $timeout ) Sets the search timeout.

Method Details

addAgg() public method

See also: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
Deprecation: since 2.0.5 use addAggragate() instead Adds an aggregation to this query. This is an alias for [[addAggregation]].
public addAgg ( string $name, string $type, string | array $options )
$name string the name of the aggregation
$type string the aggregation type. e.g. `terms`, `range`, `histogram`...
$options string | array the configuration options for this aggregation. Can be an array or a json string.

addAggregate() public method

Adds an aggregation to this query. Supports nested aggregations.
See also: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations.html
public addAggregate ( string $name, string | array $options )
$name string the name of the aggregation
$options string | array the configuration options for this aggregation. Can be an array or a json string.

addAggregation() public method

See also: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
Deprecation: since 2.0.5 use addAggragate() instead Adds an aggregation to this query.
public addAggregation ( string $name, string $type, string | array $options )
$name string the name of the aggregation
$type string the aggregation type. e.g. `terms`, `range`, `histogram`...
$options string | array the configuration options for this aggregation. Can be an array or a json string.

addOptions() public method

Adds more options, overwriting existing options.
See also: options()
Since: 2.0.4
public addOptions ( array $options )
$options array the options to be added.

addSuggester() public method

Adds a suggester to this query.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
public addSuggester ( string $name, string | array $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.

all() public method

Executes the query and returns all results as an array.
public all ( Connection $db = null ) : array
$db 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 query results. If the query results in nothing, an empty array will be returned.

batch() public method

A batch query supports fetching data in batches, which can keep the memory usage under a limit. This method will return a BatchQueryResult object which implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches. For example, php $query = (new Query)->from('user'); foreach ($query->batch() as $rows) { $rows is an array of 10 or fewer rows from user table } Batch size is determined by the limit setting (note that in scan mode batch limit is per shard).
Since: 2.0.4
public batch ( string $scrollWindow = '1m', Connection $db = null ) : BatchQueryResult
$scrollWindow string how long Elasticsearch should keep the search context alive, in [time units](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units)
$db Connection the database connection. If not set, the `elasticsearch` application component will be used.
return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches.

column() public method

Executes the query and returns the first column of the result.
public column ( string $field, Connection $db = null ) : array
$field string the field to query over
$db 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.

count() public method

Returns the number of records.
public count ( string $q = '*', Connection $db = null ) : integer
$q string the COUNT expression. This parameter is ignored by this implementation.
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return integer number of records

createCommand() public method

Creates a DB command that can be used to execute this query.
public createCommand ( Connection $db = null ) : Command
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return Command the created DB command instance.

delete() public method

Everything except query and filter will be ignored.
public delete ( Connection $db = null, array $options = [] ) : array
$db 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.
return array the query results.

each() public method

This method is similar to Query::batch except that in each iteration of the result, only one row of data is returned. For example, php $query = (new Query)->from('user'); foreach ($query->each() as $row) { }
Since: 2.0.4
public each ( string $scrollWindow = '1m', Connection $db = null ) : BatchQueryResult
$scrollWindow string how long Elasticsearch should keep the search context alive, in [time units](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units)
$db Connection the database connection. If not set, the `elasticsearch` application component will be used.
return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches.

exists() public method

Returns a value indicating whether the query result contains any row of data.
public exists ( Connection $db = null ) : boolean
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return boolean whether the query result contains any row of data.

fields() public method

Sets the fields to retrieve from the documents.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html
public fields ( array $fields )
$fields array the fields to be selected.

filter() public method

Sets the filter part of this search query.
public filter ( string $filter )
$filter string

from() public method

Sets the index and type to retrieve documents from.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type
public from ( string | array $index, string | array $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 `null` it means that all indexes are being queried.
$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 `null` it means that all types are being queried.

highlight() public method

Sets a highlight parameters to retrieve from the documents.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html
public highlight ( array $highlight )
$highlight array array of parameters to highlight results.

init() public method

public init ( )

minScore() public method

See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-min-score.html
Since: 2.0.4
public minScore ( float $minScore ) : static
$minScore float Exclude documents which have a `_score` less than the minimum specified minScore
return static the query object itself

one() public method

Executes the query and returns a single row of result.
public one ( Connection $db = null ) : array | boolean
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
return array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.

options() public method

Sets the options to be passed to the command created by this query.
See also: Command::$options
Since: 2.0.4
public options ( array $options )
$options array the options to be set.

populate() public method

This method is internally used to convert the data fetched from database into the format as required by this query.
Since: 2.0.4
public populate ( array $rows ) : array
$rows array the raw query result from database
return array the converted query result

postFilter() public method

Set the post_filter part of the search query.
Since: 2.0.5
public postFilter ( string | array $filter )
$filter string | array

query() public method

Sets the querypart of this search query.
public query ( string $query )
$query string

scalar() public method

The value returned will be the specified field in the first document of the query results.
public scalar ( string $field, Connection $db = null ) : string
$field string name of the attribute to select
$db Connection the database connection used to execute the query. If this parameter is not given, the `elasticsearch` application component will be used.
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.

source() public method

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
public source ( array $source )
$source array the source patterns to be selected.

stats() public method

Adds a 'stats' part to the query.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search.html#stats-groups
public stats ( array $groups )
$groups array an array of groups to maintain a statistics aggregation for.

timeout() public method

Sets the search timeout.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#_parameters_5
public timeout ( integer $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.

Property Details

$aggregations public_oe property

List of aggregations to add to this query.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
public $aggregations

$fields public_oe property

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: php $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. [always returned as arrays]: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/_return_values.html#_return_values [script field]: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html#search-request-fields
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html
See also: fields()
See also: source
public $fields

$filter public_oe property

The filter part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
public $filter

$highlight public_oe property

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
public $highlight

$index public_oe property

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()
public $index

$minScore public_oe property

Exclude documents which have a _score less than the minimum specified in min_score
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-min-score.html
Since: 2.0.4
public $minScore

$options public_oe property

list of options that will passed to commands created by this query.
See also: Command::$options
Since: 2.0.4
public $options

$postFilter public_oe property

The post_filter part of the search query for differentially filter search results and aggregations.
See also: https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html
Since: 2.0.5
public $postFilter

$query public_oe property

The query part of this search query. This is an array or json string that follows the format of the elasticsearch Query DSL.
public $query

$source public_oe property

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
See also: source()
See also: fields
public $source

$stats public_oe property

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
public $stats

$suggest public_oe property

list of suggesters to add to this query.
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
public $suggest

$timeout public_oe property

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()
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#_parameters_5
public $timeout

$type public_oe property

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()
public $type