Class yii\sphinx\Query
Inheritance | yii\sphinx\Query » yii\db\Query » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable, yii\db\QueryInterface |
Uses Traits | yii\db\QueryTrait |
Subclasses | yii\sphinx\ActiveQuery |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/Query.php |
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 |
---|---|---|---|
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$connection | yii\sphinx\Connection | Sphinx connection instance | yii\sphinx\Query |
$distinct | boolean | Whether to select distinct rows of data only. | yii\db\Query |
$facets | array | Facet search specifications. | yii\sphinx\Query |
$from | array | The table(s) to be selected from. | yii\db\Query |
$groupBy | array | How to group the query results. | yii\db\Query |
$having | string|array | The condition to be applied in the GROUP BY clause. | yii\db\Query |
$indexBy | string|callable | The name of the column by which the query results should be indexed by. | yii\db\QueryTrait |
$join | array | How to join with other tables. | yii\db\Query |
$limit | integer | Maximum number of records to be returned. | yii\db\QueryTrait |
$match | string|yii\db\Expression | Text, which should be searched in fulltext mode. | yii\sphinx\Query |
$offset | integer | Zero-based offset from where the records are to be returned. | yii\db\QueryTrait |
$options | array | Per-query options in format: optionName => optionValue They will compose OPTION clause. | yii\sphinx\Query |
$orderBy | array | How to sort the query results. | yii\db\QueryTrait |
$params | array | List of query parameter values indexed by parameter placeholders. | yii\db\Query |
$select | array | The columns being selected. | yii\db\Query |
$selectOption | string | Additional option that should be appended to the 'SELECT' keyword. | yii\db\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 |
$union | array | This is used to construct the UNION clause(s) in a SQL statement. | yii\db\Query |
$where | string|array | Query condition. | yii\db\QueryTrait |
$within | string | WITHIN GROUP ORDER BY clause. | yii\sphinx\Query |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Object |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
addFacets() | Adds additional FACET part of the query. | yii\sphinx\Query |
addGroupBy() | Adds additional group-by columns to the existing ones. | yii\db\Query |
addOptions() | Adds additional query options. | yii\sphinx\Query |
addOrderBy() | Adds additional ORDER BY columns to the query. | yii\db\QueryTrait |
addParams() | Adds additional parameters to be bound to the query. | yii\db\Query |
addSelect() | Add more columns to the SELECT part of the query. | yii\db\Query |
addWithin() | Adds additional WITHIN GROUP ORDER BY columns to the query. | yii\sphinx\Query |
all() | Executes the query and returns all results as an array. | yii\db\Query |
andFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait |
andHaving() | Adds an additional HAVING condition to the existing one. | yii\db\Query |
andWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
average() | Returns the average of the specified column values. | yii\db\Query |
batch() | Starts a batch query. | yii\db\Query |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\Object |
column() | Executes the query and returns the first column of the result. | yii\db\Query |
count() | Returns the number of records. | yii\db\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 |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
distinct() | Sets the value indicating whether to SELECT DISTINCT or not. | yii\db\Query |
each() | Starts a batch query and retrieves data row by row. | yii\db\Query |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
exists() | Returns a value indicating whether the query result contains any row of data. | yii\db\Query |
facets() | Sets FACET part of the query. | yii\sphinx\Query |
filterWhere() | Sets the WHERE part of the query but ignores empty operands. | yii\db\QueryTrait |
from() | Sets the FROM part of the query. | yii\db\Query |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getConnection() | yii\sphinx\Query | |
groupBy() | Sets the GROUP BY part of the query. | yii\db\Query |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
having() | Sets the HAVING part of the query. | yii\db\Query |
indexBy() | Sets the indexBy() property. | yii\db\QueryTrait |
init() | Initializes the object. | yii\base\Object |
innerJoin() | Appends an INNER JOIN part to the query. | yii\sphinx\Query |
join() | Appends a JOIN part to the query. | yii\sphinx\Query |
leftJoin() | Appends a LEFT OUTER JOIN part to the query. | yii\sphinx\Query |
limit() | Sets the LIMIT part of the query. | yii\db\QueryTrait |
match() | Sets the fulltext query text. This text will be composed into MATCH operator inside the WHERE clause. | yii\sphinx\Query |
max() | Returns the maximum of the specified column values. | yii\db\Query |
min() | Returns the minimum of the specified column values. | yii\db\Query |
off() | Detaches an existing event handler from this component. | yii\base\Component |
offset() | Sets the OFFSET part of the query. | yii\db\QueryTrait |
on() | Attaches an event handler to an event. | yii\base\Component |
one() | Executes the query and returns a single row of result. | yii\sphinx\Query |
options() | Sets the query options. | yii\sphinx\Query |
orFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | yii\db\QueryTrait |
orHaving() | Adds an additional HAVING condition to the existing one. | yii\db\Query |
orWhere() | Adds an additional WHERE condition to the existing one. | yii\db\QueryTrait |
orderBy() | Sets the ORDER BY part of the query. | yii\db\QueryTrait |
params() | Sets the parameters to be bound to the query. | yii\db\Query |
populate() | Converts the raw query results into the format as specified by this query. | yii\sphinx\Query |
prepare() | Prepares for building SQL. | yii\db\Query |
rightJoin() | Appends a RIGHT OUTER JOIN part to the query. | yii\sphinx\Query |
scalar() | Returns the query result as a scalar value. | yii\db\Query |
search() | Executes the query and returns the complete search result including e.g. hits, facets. | yii\sphinx\Query |
select() | Sets the SELECT part of the query. | yii\db\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 |
sum() | Returns the sum of the specified column values. | yii\db\Query |
trigger() | Triggers an event. | yii\base\Component |
union() | Appends a SQL statement using UNION operator. | yii\db\Query |
where() | Sets the WHERE part of the query. | yii\db\QueryTrait |
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 |
filterCondition() | Removes empty operands from the given query condition. | yii\db\QueryTrait |
isEmpty() | Returns a value indicating whether the give value is "empty". | yii\db\QueryTrait |
normalizeOrderBy() | Normalizes format of ORDER BY data | yii\db\QueryTrait |
queryScalar() | Queries a scalar value by setting select() first. | yii\sphinx\Query |
Property Details
Sphinx connection instance
Facet search specifications. For example:
[
'group_id',
'brand_id' => [
'order' => ['COUNT(*)' => SORT_ASC],
],
'price' => [
'select' => 'INTERVAL(price,200,400,600,800) AS price_seg',
'order' => ['FACET()' => SORT_ASC],
],
]
You need to use search() method in order to fetch facet results.
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 orderBy() one.
Method Details
Adds additional FACET part of the query.
static addFacets( $facets ) | ||
$facets | array | Facet specifications. |
return | static | The query object itself |
---|
Adds additional query options.
See also options().
static addOptions( $options ) | ||
$options | array | Query options in format: optionName => optionValue |
return | static | The query object itself |
---|
Adds additional WITHIN GROUP ORDER BY columns to the query.
See also within().
static 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 | static | The query object itself |
---|
Builds a snippets from provided source data.
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.
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.
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.
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. |
---|
yii\sphinx\Connection defaultConnection( ) | ||
return | yii\sphinx\Connection | Default connection value. |
---|
Sets FACET part of the query.
static facets( $facets ) | ||
$facets | array | Facet specifications. |
return | static | The query object itself |
---|
Fills the query result rows with the snippets built from source determined by snippetCallback() result.
array|yii\sphinx\ActiveRecord[] fillUpSnippets( $rows ) | ||
$rows | array | Raw query result rows. |
return | array|yii\sphinx\ActiveRecord[] | Query result rows with filled up snippets. |
---|
yii\sphinx\Connection getConnection( ) | ||
return | yii\sphinx\Connection | Sphinx connection instance |
---|
Appends an INNER JOIN part to the query.
yii\db\Query innerJoin( $table, $on = '', $params = [] ) | ||
$table | string|array | The table to be joined. Use string to represent the name of the table to be joined. Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use array to represent joining with a sub-query. The array must contain only one element. The value must be a Query object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string|array | The join condition that should appear in the ON part. Please refer to where() on how to specify this parameter. |
$params | array | The parameters (name => value) to be bound to the query. |
return | yii\db\Query | The query object itself |
---|
Appends a JOIN part to the query.
The first parameter specifies what type of join it is.
yii\db\Query join( $type, $table, $on = '', $params = [] ) | ||
$type | string | The type of join, such as INNER JOIN, LEFT JOIN. |
$table | string|array | The table to be joined. Use string to represent the name of the table to be joined. Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use array to represent joining with a sub-query. The array must contain only one element. The value must be a Query object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string|array | The join condition that should appear in the ON part. Please refer to where() on how to specify this parameter. |
$params | array | The parameters (name => value) to be bound to the query. |
return | yii\db\Query | The query object itself |
---|
Appends a LEFT OUTER JOIN part to the query.
yii\db\Query leftJoin( $table, $on = '', $params = [] ) | ||
$table | string|array | The table to be joined. Use string to represent the name of the table to be joined. Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use array to represent joining with a sub-query. The array must contain only one element. The value must be a Query object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string|array | The join condition that should appear in the ON part. Please refer to where() on how to specify this parameter. |
$params | array | The parameters (name => value) to be bound to the query |
return | yii\db\Query | The query object itself |
---|
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();
static match( $query ) | ||
$query | string | Fulltext query text. |
return | static | The query object itself |
---|
Executes the query and returns a single row of result.
array|boolean one( $db = null ) | ||
$db | yii\db\Connection | The database connection used to generate the SQL statement.
If this parameter is not given, the |
return | array|boolean | The first row (in terms of an array) of the query result. False is returned if the query results in nothing. |
---|
Sets the query options.
See also addOptions().
static options( $options ) | ||
$options | array | Query options in format: optionName => optionValue |
return | static | The query object itself |
---|
Converts the raw query results into the format as specified by this query.
This method is internally used to convert the data fetched from database into the format as required by this query.
array populate( $rows ) | ||
$rows | array | The raw query result from database |
return | array | The converted query result |
---|
Queries a scalar value by setting select() first.
Restores the value of select to make this query reusable.
bool|string queryScalar( $selectExpression, $db ) | ||
$selectExpression | string|yii\db\Expression | |
$db | yii\db\Connection|null |
Appends a RIGHT OUTER JOIN part to the query.
yii\db\Query rightJoin( $table, $on = '', $params = [] ) | ||
$table | string|array | The table to be joined. Use string to represent the name of the table to be joined. Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use array to represent joining with a sub-query. The array must contain only one element. The value must be a Query object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string|array | The join condition that should appear in the ON part. Please refer to where() on how to specify this parameter. |
$params | array | The parameters (name => value) to be bound to the query |
return | yii\db\Query | The query object itself |
---|
Executes the query and returns the complete search result including e.g. hits, facets.
array search( $db = null ) | ||
$db | yii\sphinx\Connection | The Sphinx connection used to generate the SQL statement. |
return | array | The query results. |
---|
static setConnection( $connection ) | ||
$connection | yii\sphinx\Connection | Sphinx connection instance |
return | static | The query object itself |
---|
Sets whether to automatically perform 'SHOW META' for the search query.
See also showMeta().
static showMeta( $showMeta ) | ||
$showMeta | boolean|string|yii\db\Expression | Whether to automatically perform 'SHOW META' |
return | static | The query object itself |
---|
Sets the PHP callback, which should be used to retrieve the source data for the snippets building.
See also snippetCallback().
static snippetCallback( $callback ) | ||
$callback | callable | PHP callback, which should be used to fetch source data for the snippets. |
return | static | The query object itself |
---|
Sets the call snippets query options.
See also snippetCallback().
static snippetOptions( $options ) | ||
$options | array | Call snippet options in format: option_name => option_value |
return | static | The query object itself |
---|
Sets the WITHIN GROUP ORDER BY part of the query.
See also addWithin().
static 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 | static | The query object itself |
---|