PHP Класс 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.
С версии: 2.0
Автор: Carsten Brandt ([email protected])
Наследование: extends yii\base\Component, implements yii\db\QueryInterface, use trait yii\db\QueryTrait
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
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.

Описание методов

addAgg() публичный Метод

См. также: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
Устаревший: 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() публичный Метод

Adds an aggregation to this query. Supports nested aggregations.
См. также: 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() публичный Метод

См. также: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
Устаревший: 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() публичный Метод

Adds more options, overwriting existing options.
См. также: options()
С версии: 2.0.4
public addOptions ( array $options )
$options array the options to be added.

addSuggester() публичный Метод

Adds a suggester to this query.
См. также: 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() публичный Метод

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.
Результат array the query results. If the query results in nothing, an empty array will be returned.

batch() публичный Метод

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).
С версии: 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.
Результат BatchQueryResult the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches.

column() публичный Метод

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.
Результат array the first column of the query result. An empty array is returned if the query results in nothing.

count() публичный Метод

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.
Результат integer number of records

createCommand() публичный Метод

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.
Результат Command the created DB command instance.

delete() публичный Метод

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.
Результат array the query results.

each() публичный Метод

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) { }
С версии: 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.
Результат BatchQueryResult the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches.

exists() публичный Метод

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.
Результат boolean whether the query result contains any row of data.

fields() публичный Метод

Sets the fields to retrieve from the documents.
См. также: 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() публичный Метод

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

from() публичный Метод

Sets the index and type to retrieve documents from.
См. также: 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() публичный Метод

Sets a highlight parameters to retrieve from the documents.
См. также: 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 init ( )

minScore() публичный Метод

См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-min-score.html
С версии: 2.0.4
public minScore ( float $minScore ) : static
$minScore float Exclude documents which have a `_score` less than the minimum specified minScore
Результат static the query object itself

one() публичный Метод

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.
Результат array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.

options() публичный Метод

Sets the options to be passed to the command created by this query.
См. также: Command::$options
С версии: 2.0.4
public options ( array $options )
$options array the options to be set.

populate() публичный Метод

This method is internally used to convert the data fetched from database into the format as required by this query.
С версии: 2.0.4
public populate ( array $rows ) : array
$rows array the raw query result from database
Результат array the converted query result

postFilter() публичный Метод

Set the post_filter part of the search query.
С версии: 2.0.5
public postFilter ( string | array $filter )
$filter string | array

query() публичный Метод

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

scalar() публичный Метод

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.
Результат 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() публичный Метод

Sets the source filtering, specifying how the _source field of the document should be returned.
См. также: 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() публичный Метод

Adds a 'stats' part to the query.
См. также: 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() публичный Метод

Sets the search timeout.
См. также: 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.

Описание свойств

$aggregations публичное свойство

List of aggregations to add to this query.
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html
public $aggregations

$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
См. также: 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
public $fields

$filter публичное свойство

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 публичное свойство

The highlight part of this search query. This is an array that allows to highlight search results on one or more fields.
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-request-highlighting.html
public $highlight

$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.
См. также: from()
public $index

$minScore публичное свойство

Exclude documents which have a _score less than the minimum specified in min_score
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-min-score.html
С версии: 2.0.4
public $minScore

$options публичное свойство

list of options that will passed to commands created by this query.
См. также: Command::$options
С версии: 2.0.4
public $options

$postFilter публичное свойство

The post_filter part of the search query for differentially filter search results and aggregations.
См. также: https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html
С версии: 2.0.5
public $postFilter

$query публичное свойство

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 публичное свойство

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.
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
См. также: source()
См. также: fields
public $source

$stats публичное свойство

the 'stats' part of the query. An array of groups to maintain a statistics aggregation for.
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search.html#stats-groups
public $stats

$suggest публичное свойство

list of suggesters to add to this query.
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
public $suggest

$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.
См. также: timeout()
См. также: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#_parameters_5
public $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.
См. также: from()
public $type