PHP Класс yii\sphinx\Query

Query provides a set of methods to facilitate the specification of different clauses in a SELECT statement. 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 Sphinx query. For example, php $query = new Query(); $query->select('id, group_id') ->from('idx_item') ->limit(10); build and execute the query $command = $query->createCommand(); $command->sql returns the actual SQL $rows = $command->queryAll(); Since Sphinx does not store the original indexed text, the snippets for the rows in query result should be build separately via another query. You can simplify this workflow using [[snippetCallback]]. Warning: even if you do not set any query limit, implicit LIMIT 0,20 is present by default!
С версии: 2.0
Автор: Paul Klimov ([email protected])
Наследование: extends yii\db\Query
Показать файл Открыть проект Примеры использования класса

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

Свойство Тип Описание
$facets facet search specifications. For example: php [ 'group_id', 'brand_id' => [ 'order' => ['COUNT(*)' => SORT_ASC], ], 'price' => [ 'select' => 'INTERVAL(price,200,400,600,800) AS price', 'order' => ['FACET()' => SORT_ASC], ], 'name_in_json' => [ 'select' => [new Expression('json_attr.name AS name_in_json')], ], ] You need to use Query::search method in order to fetch facet results. Note: if you specify custom select for the facet, ensure facet name has corresponding column inside it.
$groupLimit groups limit: to return (no more than) N top matches for each group. This option will take effect only if [[groupBy]] is set.
$match text, which should be searched in fulltext mode. This value will be composed into MATCH operator inside the WHERE clause. Note: this value will be processed by [[Connection::escapeMatchValue()]], if you need to compose complex match condition use [[Expression]], see Query::match for details.
$options per-query options in format: optionName => optionValue They will compose OPTION clause. This is a Sphinx specific extension that lets you control a number of per-query options.
$showMeta whether to automatically perform 'SHOW META' query against main one. You may set this value to be string or [[Expression]] instance, in this case its value will be used as 'LIKE' condition for 'SHOW META' statement. You need to use Query::search method in order to fetch 'meta' results.
$snippetCallback PHP callback, which should be used to fetch source data for the snippets. Such callback will receive array of query result rows as an argument and must return the array of snippet source strings in the order, which match one of incoming rows. For example: php $query = new Query(); $query->from('idx_item') ->match('pencil') ->snippetCallback(function ($rows) { $result = []; foreach ($rows as $row) { $result[] = file_get_contents('/path/to/index/files/' . $row['id'] . '.txt'); } return $result; }) ->all();
$snippetOptions query options for the call snippet.
$within WITHIN GROUP ORDER BY clause. This is a Sphinx specific extension that lets you control how the best row within a group will to be selected. The possible value matches the [[orderBy]] one.

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

Метод Описание
addFacets ( array $facets ) Adds additional FACET part of the query.
addOptions ( array $options ) Adds additional query options.
addWithin ( string | array $columns ) Adds additional WITHIN GROUP ORDER BY columns to the query.
create ( Query $from ) : Query Creates a new Query object and copies its property values from an existing one.
createCommand ( Connection $db = null ) : Command Creates a Sphinx command that can be used to execute this query.
facets ( array $facets ) Sets FACET part of the query.
getConnection ( ) : Connection
groupLimit ( integer $limit ) Sets groups limit: to return (no more than) N top matches for each group.
innerJoin ( $table, $on = '', $params = [] )
join ( $type, $table, $on = '', $params = [] )
leftJoin ( $table, $on = '', $params = [] )
match ( string | yii\db\Expression | MatchExpression $query ) Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause.
one ( $db = null )
options ( array $options ) Sets the query options.
populate ( $rows )
rightJoin ( $table, $on = '', $params = [] )
search ( Connection $db = null ) : array Executes the query and returns the complete search result including e.g. hits, facets.
setConnection ( Connection $connection )
showMeta ( boolean | string | yii\db\Expression $showMeta ) Sets whether to automatically perform 'SHOW META' for the search query.
snippetCallback ( callable $callback ) Sets the PHP callback, which should be used to retrieve the source data for the snippets building.
snippetOptions ( array $options ) Sets the call snippets query options.
within ( string | array $columns ) Sets the WITHIN GROUP ORDER BY part of the query.

Защищенные методы

Метод Описание
callSnippets ( array $source ) : array Builds a snippets from provided source data.
callSnippetsInternal ( array $source, string $from ) : array Builds a snippets from provided source data by the given index.
defaultConnection ( ) : Connection
fillUpSnippets ( array $rows ) : array | ActiveRecord[] Fills the query result rows with the snippets built from source determined by [[snippetCallback]] result.
queryScalar ( $selectExpression, $db )

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

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

Adds additional FACET part of the query.
public addFacets ( array $facets )
$facets array facet specifications.

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

Adds additional query options.
См. также: options()
public addOptions ( array $options )
$options array query options in format: optionName => optionValue

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

Adds additional WITHIN GROUP ORDER BY columns to the query.
См. также: within()
public addWithin ( string | array $columns )
$columns string | array the columns (and the directions) to find best row within a group. Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. `['id' => Query::SORT_ASC, 'name' => Query::SORT_DESC]`). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression).

callSnippets() защищенный Метод

Builds a snippets from provided source data.
protected callSnippets ( array $source ) : array
$source array the source data to extract a snippet from.
Результат array snippets list.

callSnippetsInternal() защищенный Метод

Builds a snippets from provided source data by the given index.
protected callSnippetsInternal ( array $source, string $from ) : array
$source array the source data to extract a snippet from.
$from string name of the source index.
Результат array snippets list.

create() публичный статический Метод

The properties being copies are the ones to be used by query builders.
public static create ( Query $from ) : Query
$from Query the source query object
Результат Query the new Query object

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

Creates a Sphinx command that can be used to execute this query.
public createCommand ( Connection $db = null ) : Command
$db Connection the Sphinx connection used to generate the SQL statement. If this parameter is not given, the `sphinx` application component will be used.
Результат Command the created Sphinx command instance.

defaultConnection() защищенный Метод

protected defaultConnection ( ) : Connection
Результат Connection default connection value.

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

Sets FACET part of the query.
public facets ( array $facets )
$facets array facet specifications.

fillUpSnippets() защищенный Метод

Fills the query result rows with the snippets built from source determined by [[snippetCallback]] result.
protected fillUpSnippets ( array $rows ) : array | ActiveRecord[]
$rows array raw query result rows.
Результат array | ActiveRecord[] query result rows with filled up snippets.

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

public getConnection ( ) : Connection
Результат Connection Sphinx connection instance

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

This option will take effect only if [[groupBy]] is set.
С версии: 2.0.6
public groupLimit ( integer $limit )
$limit integer group limit.

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

public innerJoin ( $table, $on = '', $params = [] )

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

public join ( $type, $table, $on = '', $params = [] )

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

public leftJoin ( $table, $on = '', $params = [] )

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

Note: this value will be processed by [[Connection::escapeMatchValue()]], if you need to compose complex match condition use [[Expression]]: php $query = new Query(); $query->from('my_index') ->match(new Expression(':match', ['match' => '@(content) ' . Yii::$app->sphinx->escapeMatchValue($matchValue)])) ->all();
public match ( string | yii\db\Expression | MatchExpression $query )
$query string | yii\db\Expression | MatchExpression fulltext query text.

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

public one ( $db = null )

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

Sets the query options.
См. также: addOptions()
public options ( array $options )
$options array query options in format: optionName => optionValue

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

public populate ( $rows )

queryScalar() защищенный Метод

protected queryScalar ( $selectExpression, $db )

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

public rightJoin ( $table, $on = '', $params = [] )

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

public setConnection ( Connection $connection )
$connection Connection Sphinx connection instance

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

Sets whether to automatically perform 'SHOW META' for the search query.
См. также: showMeta
public showMeta ( boolean | string | yii\db\Expression $showMeta )
$showMeta boolean | string | yii\db\Expression whether to automatically perform 'SHOW META'

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

Sets the PHP callback, which should be used to retrieve the source data for the snippets building.
См. также: snippetCallback
public snippetCallback ( callable $callback )
$callback callable PHP callback, which should be used to fetch source data for the snippets.

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

Sets the call snippets query options.
См. также: snippetCallback
public snippetOptions ( array $options )
$options array call snippet options in format: option_name => option_value

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

Sets the WITHIN GROUP ORDER BY part of the query.
См. также: addWithin()
public within ( string | array $columns )
$columns string | array the columns (and the directions) to find best row within a group. Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. `['id' => Query::SORT_ASC, 'name' => Query::SORT_DESC]`). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression).

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

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

facet search specifications. For example: php [ 'group_id', 'brand_id' => [ 'order' => ['COUNT(*)' => SORT_ASC], ], 'price' => [ 'select' => 'INTERVAL(price,200,400,600,800) AS price', 'order' => ['FACET()' => SORT_ASC], ], 'name_in_json' => [ 'select' => [new Expression('json_attr.name AS name_in_json')], ], ] You need to use Query::search method in order to fetch facet results. Note: if you specify custom select for the facet, ensure facet name has corresponding column inside it.
public $facets

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

groups limit: to return (no more than) N top matches for each group. This option will take effect only if [[groupBy]] is set.
С версии: 2.0.6
public $groupLimit

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

text, which should be searched in fulltext mode. This value will be composed into MATCH operator inside the WHERE clause. Note: this value will be processed by [[Connection::escapeMatchValue()]], if you need to compose complex match condition use [[Expression]], see Query::match for details.
public $match

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

per-query options in format: optionName => optionValue They will compose OPTION clause. This is a Sphinx specific extension that lets you control a number of per-query options.
public $options

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

whether to automatically perform 'SHOW META' query against main one. You may set this value to be string or [[Expression]] instance, in this case its value will be used as 'LIKE' condition for 'SHOW META' statement. You need to use Query::search method in order to fetch 'meta' results.
public $showMeta

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

PHP callback, which should be used to fetch source data for the snippets. Such callback will receive array of query result rows as an argument and must return the array of snippet source strings in the order, which match one of incoming rows. For example: php $query = new Query(); $query->from('idx_item') ->match('pencil') ->snippetCallback(function ($rows) { $result = []; foreach ($rows as $row) { $result[] = file_get_contents('/path/to/index/files/' . $row['id'] . '.txt'); } return $result; }) ->all();
public $snippetCallback

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

query options for the call snippet.
public $snippetOptions

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

WITHIN GROUP ORDER BY clause. This is a Sphinx specific extension that lets you control how the best row within a group will to be selected. The possible value matches the [[orderBy]] one.
public $within