PHP Class Cake\ElasticSearch\Query

Inheritance: implements IteratorAggregat\IteratorAggregate, use trait Cake\Datasource\QueryTrait
Datei anzeigen Open project: cakephp/elastic-search Class Usage Examples

Protected Properties

Property Type Description
$_dirty boolean Internal state to track whether or not the query has been modified.
$_elasticQuery
$_parts array The various query builder parts that will be transferred to the elastica query.
$_searchOptions array Additional options for Elastica\Type::search()

Public Methods

Method Description
__construct ( Type $repository ) Query constructor
aggregate ( array | AbstractAggregation $aggregation ) Add an aggregation to the elastic query object
applyOptions ( array $options ) Populates or adds parts to current query clauses using an array.
clause ( string $name ) : mixed Returns any data that was stored in the specified clause. This is useful for modifying any internal part of the query and it is used during compiling to transform the query accordingly before it is executed. The valid clauses that can be retrieved are: fields, preFilter, postFilter, query, order, limit and offset.
compileQuery ( ) : string Compile the Elasticsearch query.
find ( $type = 'all', $options = [] ) : Query {@inheritDoc}
highlight ( array $highlight ) Set the highlight options for the query.
limit ( integer $limit ) Sets the maximum number of results to return for this query.
offset ( integer $num ) Sets the number of records that should be skipped from the original result set This is commonly used for paginating large results. Accepts an integer.
order ( string | array $order, boolean $overwrite = false ) Sets the sorting options for the result set.
page ( integer $num, integer $limit = null ) Set the page of results you want.
postFilter ( array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite = false ) Sets the filter to use in the post_filter object. Filters added using this method will be stacked on a bool filter.
query ( array $matcher ) Method to set the query
searchOptions ( array $options = null ) Set or get the search options
select ( array $fields, boolean $overwrite = false ) Adds fields to be selected from _source.
where ( array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite = false ) Sets the filter to use in a FilteredQuery object. Filters added using this method will be stacked on a bool filter and applied to the filter part of a filtered query.

Protected Methods

Method Description
_buildFilter ( string $type, array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite ) Auxiliary function used to parse conditions into filters and store them in a _parts variable.
_execute ( ) : ResultSet Executes the query.

Method Details

__construct() public method

Query constructor
public __construct ( Type $repository )
$repository Type The type of document.

_buildFilter() protected method

Auxiliary function used to parse conditions into filters and store them in a _parts variable.
protected _buildFilter ( string $type, array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite )
$type string The name of the part in which the filters will be stored
$conditions array | callable | Elastica\Filter\AbstractFilter The list of conditions.
$overwrite boolean Whether or not to replace previous filters.

_execute() protected method

Executes the query.
protected _execute ( ) : ResultSet
return ResultSet The results of the query

aggregate() public method

Add an aggregation to the elastic query object
public aggregate ( array | AbstractAggregation $aggregation )
$aggregation array | Elastica\Aggregation\AbstractAggregation One or multiple facets

applyOptions() public method

This is handy for passing all query clauses at once. The option array accepts: - fields: Maps to the select method - conditions: Maps to the where method - order: Maps to the order method - limit: Maps to the limit method - offset: Maps to the offset method - page: Maps to the page method ### Example: $query->applyOptions([ 'fields' => ['id', 'name'], 'conditions' => [ 'created >=' => '2013-01-01' ], 'limit' => 10 ]); Is equivalent to: $query ->select(['id', 'name']) ->where(['created >=' => '2013-01-01']) ->limit(10)
public applyOptions ( array $options )
$options array list of query clauses to apply new parts to.

clause() public method

The return value for each of those parts may vary. Some clauses use QueryExpression to internally store their state, some use arrays and others may use booleans or integers. This is summary of the return types for each clause. - fields: array, will return empty array when no fields are set - preFilter: The filter to use in a FilteredQuery object, returns null when not set - postFilter: The filter to use in the post_filter object, returns null when not set - query: Raw query (Elastica\Query\AbstractQuery), return null when not set - order: OrderByExpression, returns null when not set - limit: integer, null when not set - offset: integer, null when not set
public clause ( string $name ) : mixed
$name string name of the clause to be returned
return mixed

compileQuery() public method

Compile the Elasticsearch query.
public compileQuery ( ) : string
return string The Elasticsearch query.

find() public method

{@inheritDoc}
public find ( $type = 'all', $options = [] ) : Query
return Query

highlight() public method

Set the highlight options for the query.
public highlight ( array $highlight )
$highlight array The highlight options to use.

limit() public method

This sets the size option for the Elastic Search query.
public limit ( integer $limit )
$limit integer The number of documents to return.

offset() public method

Sets the number of records that should be skipped from the original result set This is commonly used for paginating large results. Accepts an integer.
public offset ( integer $num )
$num integer The number of records to be skipped

order() public method

The accepted format for the $order parameter is: - [['name' => ['order'=> 'asc', ...]], ['price' => ['order'=> 'asc', ...]]] - ['name' => 'asc', 'price' => 'desc'] - 'field1' (defaults to order => 'desc')
public order ( string | array $order, boolean $overwrite = false )
$order string | array The sorting order to use.
$overwrite boolean Whether or not to replace previous sorting.

page() public method

This method provides an easier to use interface to set the limit + offset in the record set you want as results. If empty the limit will default to the existing limit clause, and if that too is empty, then 25 will be used. Pages should start at 1.
public page ( integer $num, integer $limit = null )
$num integer The page number you want.
$limit integer The number of rows you want in the page. If null the current limit clause will be used.

postFilter() public method

This method can be used in the same way the where() method is used. Please refer to its documentation for more details.
See also: Cake\ElasticSearch\Query::where()
public postFilter ( array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite = false )
$conditions array | callable | Elastica\Filter\AbstractFilter The list of conditions.
$overwrite boolean Whether or not to replace previous filters.

query() public method

Method to set the query
public query ( array $matcher )
$matcher array Set the query parts

searchOptions() public method

Set or get the search options
public searchOptions ( array $options = null )
$options array An array of additional search options

select() public method

Calling this function multiple times will append more fields to the list of fields to be selected from _source. If true is passed in the second argument, any previous selections will be overwritten with the list passed in the first argument.
public select ( array $fields, boolean $overwrite = false )
$fields array The list of fields to select from _source.
$overwrite boolean Whether or not to replace previous selections.

where() public method

There are several way in which you can use this method. The easiest one is by passing a simple array of conditions: {{{ Generates a {"term": {"name": "jose"}} json filter $query->where(['name' => 'jose']); }}} You can have as many conditions in the array as you'd like, Operators are also allowe in the field side of the array: {{{ $query->where(['name' => 'jose', 'age >' => 30, 'interests in' => ['php', 'cake']); }}} You can read about the available operators and how they translate to Elastic Search filters in the Cake\ElasticSearch\FilterBuilder::parse() method documentation. Additionally, it is possible to use a closure as first argument. The closure will receive a FilterBuilder instance, that you can use for creating arbitrary filter combinations: {{{ $query->where(function ($builder) { return $builder->and($builder->between('age', 10, 20), $builder->missing('name')); }); }}} Finally, you can pass any already built filters as first argument: {{{ $query->where(new \Elastica\Filter\Term('name.first', 'jose')); }}{
See also: Cake\ElasticSearch\FilterBuilder
public where ( array | callable | Elastica\Filter\AbstractFilter $conditions, boolean $overwrite = false )
$conditions array | callable | Elastica\Filter\AbstractFilter The list of conditions.
$overwrite boolean Whether or not to replace previous filters.

Property Details

$_dirty protected_oe property

Internal state to track whether or not the query has been modified.
protected bool $_dirty
return boolean

$_elasticQuery protected_oe property

protected $_elasticQuery

$_parts protected_oe property

The various query builder parts that will be transferred to the elastica query.
protected array $_parts
return array

$_searchOptions protected_oe property

Additional options for Elastica\Type::search()
protected array $_searchOptions
return array