PHP Class 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!
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\db\Query
Show file Open project: yiisoft/yii2-sphinx Class Usage Examples

Public Properties

Property Type Description
$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.

Public Methods

Method Description
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.

Protected Methods

Method Description
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 )

Method Details

addFacets() public method

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

addOptions() public method

Adds additional query options.
See also: options()
public addOptions ( array $options )
$options array query options in format: optionName => optionValue

addWithin() public method

Adds additional WITHIN GROUP ORDER BY columns to the query.
See also: 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() protected method

Builds a snippets from provided source data.
protected callSnippets ( array $source ) : array
$source array the source data to extract a snippet from.
return array snippets list.

callSnippetsInternal() protected method

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.
return array snippets list.

create() public static method

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
return Query the new Query object

createCommand() public method

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.
return Command the created Sphinx command instance.

defaultConnection() protected method

protected defaultConnection ( ) : Connection
return Connection default connection value.

facets() public method

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

fillUpSnippets() protected method

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.
return array | ActiveRecord[] query result rows with filled up snippets.

getConnection() public method

public getConnection ( ) : Connection
return Connection Sphinx connection instance

groupLimit() public method

This option will take effect only if [[groupBy]] is set.
Since: 2.0.6
public groupLimit ( integer $limit )
$limit integer group limit.

innerJoin() public method

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

join() public method

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

leftJoin() public method

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

match() public method

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 method

public one ( $db = null )

options() public method

Sets the query options.
See also: addOptions()
public options ( array $options )
$options array query options in format: optionName => optionValue

populate() public method

public populate ( $rows )

queryScalar() protected method

protected queryScalar ( $selectExpression, $db )

rightJoin() public method

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

setConnection() public method

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

showMeta() public method

Sets whether to automatically perform 'SHOW META' for the search query.
See also: showMeta
public showMeta ( boolean | string | yii\db\Expression $showMeta )
$showMeta boolean | string | yii\db\Expression whether to automatically perform 'SHOW META'

snippetCallback() public method

Sets the PHP callback, which should be used to retrieve the source data for the snippets building.
See also: snippetCallback
public snippetCallback ( callable $callback )
$callback callable PHP callback, which should be used to fetch source data for the snippets.

snippetOptions() public method

Sets the call snippets query options.
See also: snippetCallback
public snippetOptions ( array $options )
$options array call snippet options in format: option_name => option_value

within() public method

Sets the WITHIN GROUP ORDER BY part of the query.
See also: 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).

Property Details

$facets public property

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 public property

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

$match public property

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 public property

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 public property

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 public property

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 public property

query options for the call snippet.
public $snippetOptions

$within public property

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