Class yii\sphinx\Query
Inheritance | yii\sphinx\Query » yii\db\Query |
---|---|
Subclasses | yii\sphinx\ActiveQuery |
Available since version | 2.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
Property | Type | Description | Defined 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
Method | Description | Defined 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
Method | Description | Defined 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
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.
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.
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.
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.
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.
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();
Query options for the call snippet.
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.
Method Details
Adds additional FACET part of the query.
public $this addFacets ( $facets ) | ||
$facets | array | Facet specifications. |
return | $this | The query object itself |
---|
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 |
---|
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. |
return | $this | The query object itself |
---|
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. |
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. |
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 |
---|
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 |
return | yii\sphinx\Command | The created Sphinx command instance. |
---|
protected yii\sphinx\Connection defaultConnection ( ) | ||
return | yii\sphinx\Connection | Default connection value. |
---|
Sets FACET part of the query.
public $this facets ( $facets ) | ||
$facets | array | Facet specifications. |
return | $this | The query object itself |
---|
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. |
---|
public yii\sphinx\Connection getConnection ( ) | ||
return | yii\sphinx\Connection | Sphinx connection instance |
---|
public void getTablesUsedInFrom ( ) |
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. |
---|
public void innerJoin ( $table, $on = '', $params = [] ) | ||
$table | ||
$on | ||
$params |
public void join ( $type, $table, $on = '', $params = [] ) | ||
$type | ||
$table | ||
$on | ||
$params |
public void leftJoin ( $table, $on = '', $params = [] ) | ||
$table | ||
$on | ||
$params |
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. |
---|
public void one ( $db = null ) | ||
$db |
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 |
---|
public void populate ( $rows ) | ||
$rows |
protected void queryScalar ( $selectExpression, $db ) | ||
$selectExpression | ||
$db |
public void rightJoin ( $table, $on = '', $params = [] ) | ||
$table | ||
$on | ||
$params |
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. |
---|
public $this setConnection ( $connection ) | ||
$connection | yii\sphinx\Connection | Sphinx connection instance |
return | $this | The query object itself |
---|
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 |
---|
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 |
---|
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 |
---|
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. |
return | $this | The query object itself |
---|