Class yii\sphinx\Query

Inheritanceyii\sphinx\Query » yii\db\Query
Subclassesyii\sphinx\ActiveQuery
Available since version2.0

Query represents a SELECT SQL statement.

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 createCommand(), we can get a yii\sphinx\Command instance which can be further used to perform/execute the Sphinx query.

For example,

$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!

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$facets array Facet search specifications. yii\sphinx\Query
$groupLimit integer Groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
$match string|\yii\db\Expression Text, which should be searched in fulltext mode. yii\sphinx\Query
$options array Per-query options in format: optionName => optionValue They will compose OPTION clause. yii\sphinx\Query
$showMeta boolean|string|\yii\db\Expression Whether to automatically perform 'SHOW META' query against main one. yii\sphinx\Query
$snippetCallback callable PHP callback, which should be used to fetch source data for the snippets. yii\sphinx\Query
$snippetOptions array Query options for the call snippet. yii\sphinx\Query
$within string WITHIN GROUP ORDER BY clause. yii\sphinx\Query

Public Methods

Hide inherited methods

MethodDescriptionDefined By
addFacets() Adds additional FACET part of the query. yii\sphinx\Query
addOptions() Adds additional query options. yii\sphinx\Query
addWithin() Adds additional WITHIN GROUP ORDER BY columns to the query. yii\sphinx\Query
create() Creates a new Query object and copies its property values from an existing one. yii\sphinx\Query
createCommand() Creates a Sphinx command that can be used to execute this query. yii\sphinx\Query
facets() Sets FACET part of the query. yii\sphinx\Query
getConnection() yii\sphinx\Query
getTablesUsedInFrom() yii\sphinx\Query
groupLimit() Sets groups limit: to return (no more than) N top matches for each group. yii\sphinx\Query
innerJoin() yii\sphinx\Query
join() yii\sphinx\Query
leftJoin() yii\sphinx\Query
match() Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause. yii\sphinx\Query
one() yii\sphinx\Query
options() Sets the query options. yii\sphinx\Query
populate() yii\sphinx\Query
rightJoin() yii\sphinx\Query
search() Executes the query and returns the complete search result including e.g. hits, facets. yii\sphinx\Query
setConnection() yii\sphinx\Query
showMeta() Sets whether to automatically perform 'SHOW META' for the search query. yii\sphinx\Query
snippetCallback() Sets the PHP callback, which should be used to retrieve the source data for the snippets building. yii\sphinx\Query
snippetOptions() Sets the call snippets query options. yii\sphinx\Query
within() Sets the WITHIN GROUP ORDER BY part of the query. yii\sphinx\Query

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
callSnippets() Builds a snippets from provided source data. yii\sphinx\Query
callSnippetsInternal() Builds a snippets from provided source data by the given index. yii\sphinx\Query
defaultConnection() yii\sphinx\Query
fillUpSnippets() Fills the query result rows with the snippets built from source determined by snippetCallback() result. yii\sphinx\Query
queryScalar() yii\sphinx\Query

Property Details

$facets public property

Facet search specifications. For example:

[
    '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 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 array $facets = []
$groupLimit public property (available since version 2.0.6)

Groups limit: to return (no more than) N top matches for each group. This option will take effect only if \yii\sphinx\groupBy is set.

public integer $groupLimit null
$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 yii\sphinx\Connection::escapeMatchValue(), if you need to compose complex match condition use \yii\db\Expression, see match() for details.

public string|\yii\db\Expression $match null
$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 array $options null
$showMeta public property

Whether to automatically perform 'SHOW META' query against main one. You may set this value to be string or \yii\db\Expression instance, in this case its value will be used as 'LIKE' condition for 'SHOW META' statement. You need to use search() method in order to fetch 'meta' results.

public boolean|string|\yii\db\Expression $showMeta null
$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:

$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 callable $snippetCallback null
$snippetOptions public property

Query options for the call snippet.

public array $snippetOptions null
$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 \yii\sphinx\orderBy one.

public string $within null

Method Details

addFacets() public method

Adds additional FACET part of the query.

public $this addFacets ( $facets )
$facets array

Facet specifications.

return $this

The query object itself

addOptions() public method

Adds additional query options.

See also options().

public $this addOptions ( $options )
$options array

Query options in format: optionName => optionValue

return $this

The query object itself

addWithin() public method

Adds additional WITHIN GROUP ORDER BY columns to the query.

See also within().

public $this addWithin ( $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).

return $this

The query object itself

callSnippets() protected method

Builds a snippets from provided source data.

protected array callSnippets ( array $source )
$source array

The source data to extract a snippet from.

return array

Snippets list.

throws \yii\base\InvalidCallException

in case match() is not specified.

callSnippetsInternal() protected method

Builds a snippets from provided source data by the given index.

protected array callSnippetsInternal ( array $source, $from )
$source array

The source data to extract a snippet from.

$from string

Name of the source index.

return array

Snippets list.

throws \yii\base\InvalidCallException

in case match() is not specified.

create() public static method

Creates a new Query object and copies its property values from an existing one.

The properties being copies are the ones to be used by query builders.

public static yii\sphinx\Query create ( $from )
$from yii\sphinx\Query

The source query object

return yii\sphinx\Query

The new Query object

createCommand() public method

Creates a Sphinx command that can be used to execute this query.

public yii\sphinx\Command createCommand ( $db null )
$db yii\sphinx\Connection

The Sphinx connection used to generate the SQL statement. If this parameter is not given, the sphinx application component will be used.

return yii\sphinx\Command

The created Sphinx command instance.

defaultConnection() protected method

protected yii\sphinx\Connection defaultConnection ( )
return yii\sphinx\Connection

Default connection value.

facets() public method

Sets FACET part of the query.

public $this facets ( $facets )
$facets array

Facet specifications.

return $this

The query object itself

fillUpSnippets() protected method

Fills the query result rows with the snippets built from source determined by snippetCallback() result.

protected array|yii\sphinx\ActiveRecord[] fillUpSnippets ( $rows )
$rows array

Raw query result rows.

return array|yii\sphinx\ActiveRecord[]

Query result rows with filled up snippets.

getConnection() public method

public yii\sphinx\Connection getConnection ( )
return yii\sphinx\Connection

Sphinx connection instance

getTablesUsedInFrom() public method (available since version 2.0.9)

public void getTablesUsedInFrom ( )
groupLimit() public method (available since version 2.0.6)

Sets groups limit: to return (no more than) N top matches for each group.

This option will take effect only if \yii\sphinx\groupBy is set.

public $this groupLimit ( $limit )
$limit integer

Group limit.

return $this

The query object itself.

innerJoin() public method

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

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

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

Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause.

Note: this value will be processed by yii\sphinx\Connection::escapeMatchValue(), if you need to compose complex match condition use \yii\db\Expression:

$query = new Query();
$query->from('my_index')
    ->match(new Expression(':match', ['match' => '@(content) ' . Yii::$app->sphinx->escapeMatchValue($matchValue)]))
    ->all();
public $this match ( $query )
$query string|\yii\db\Expression|yii\sphinx\MatchExpression

Fulltext query text.

return $this

The query object itself.

one() public method

public void one ( $db null )
$db
options() public method

Sets the query options.

See also addOptions().

public $this options ( $options )
$options array

Query options in format: optionName => optionValue

return $this

The query object itself

populate() public method

public void populate ( $rows )
$rows
queryScalar() protected method

protected void queryScalar ( $selectExpression, $db )
$selectExpression
$db
rightJoin() public method

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

Executes the query and returns the complete search result including e.g. hits, facets.

public array search ( $db null )
$db yii\sphinx\Connection

The Sphinx connection used to generate the SQL statement.

return array

The query results.

setConnection() public method

public $this setConnection ( $connection )
$connection yii\sphinx\Connection

Sphinx connection instance

return $this

The query object itself

showMeta() public method

Sets whether to automatically perform 'SHOW META' for the search query.

See also showMeta().

public $this showMeta ( $showMeta )
$showMeta boolean|string|\yii\db\Expression

Whether to automatically perform 'SHOW META'

return $this

The query object itself

snippetCallback() public method

Sets the PHP callback, which should be used to retrieve the source data for the snippets building.

See also snippetCallback().

public $this snippetCallback ( $callback )
$callback callable

PHP callback, which should be used to fetch source data for the snippets.

return $this

The query object itself

snippetOptions() public method

Sets the call snippets query options.

See also snippetCallback().

public $this snippetOptions ( $options )
$options array

Call snippet options in format: option_name => option_value

return $this

The query object itself

within() public method

Sets the WITHIN GROUP ORDER BY part of the query.

See also addWithin().

public $this within ( $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).

return $this

The query object itself