CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C Elastic Search 2.x API

  • Project:
    • Elastic Search
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 2.x
      • 4.x
      • 3.x
      • 2.x

Namespaces

  • Cake\ElasticSearch
    • Association
    • Datasource
    • Rule
    • TestSuite
    • View

Class QueryBuilder

Namespace: Cake\ElasticSearch

Method Summary

  • __call() public

    Helps calling the and() and or() methods transparently.

  • _parseQuery() protected

    Parses a field name containing an operator into a Filter object.

  • and_() public

    Combines all the passed arguments in a single bool query using the "must" clause.

  • between() public

    Returns a Range query object setup to query documents having the field between a from and a to value

  • bool() public

    Returns a bool query that can be chained with the addMust(), addShould(), addFilter and addMustNot() methods.

  • exists() public

    Returns an Exists query object setup to query documents having a property present or not set to null.

  • geoBoundingBox() public

    Returns a GeoBoundingBox query object setup to query documents having a property bound by two coordinates.

  • geoDistance() public

    Returns an GeoDistance query object setup to query documents having a property in the radius distance of a coordinate.

  • geoPolygon() public

    Returns an GeoPolygon query object setup to query documents having a property enclosed in the polygon induced by the passed geo points.

  • geoShape() public

    Returns an GeoShapeProvided query object setup to query documents having a property enclosed in the specified geometrical shape type.

  • geoShapeIndex() public

    Returns an GeoShapePreIndex query object setup to query documents having a property enclosed in the specified geometrical shape type.

  • gt() public

    Returns a Range query object setup to query documents having the field greater than the provided value.

  • gte() public

    Returns a Range query object setup to query documents having the field greater than or equal the provided value.

  • hasChild() public

    Accepts a query and the child type to run against, and results in parent documents that have child docs matching the query.

  • hasParent() public

    Query by child documents having parent documents matching the query

  • ids() public

    Query documents that only have the provided ids.

  • limit() public

    Limits the number of documents (per shard) to execute on.

  • lt() public

    Returns a Range query object setup to query documents having the field smaller than the provided value.

  • lte() public

    Returns a Range query object setup to query documents having the field smaller or equals than the provided value.

  • match() public

    Returns a Match query object that query documents that have fields containing a match.

  • matchAll() public

    A query that returns all documents.

  • nested() public

    Returns a Nested query object setup to query sub documents by a path.

  • not() public

    Returns a BoolQuery query with must_not field that is typically ussed to negate another query expression

  • or_() public

    Combines all the passed arguments in a single BoolQuery query using should clause.

  • parse() public

    Converts an array into a single array of query objects

  • prefix() public

    Returns a Prefix query to query documents that have fields containing terms with a specified prefix

  • range() public

    Returns a Range query object setup to query documents having the field greater than the provided values.

  • regexp() public

    Returns a Regexp query to query documents based on a regular expression.

  • script() public

    Returns a Script query object that allows to query based on the return value of a script.

  • simpleQueryString() public

    Returns a SimpleQueryString object that allows to query based on a search string.

  • term() public

    Returns a Term query object that query documents that have fields containing a term.

  • terms() public

    Returns a Terms query object that query documents that have fields containing some terms.

  • type() public

    Returns a Type query object that query documents matching the provided document/mapping type.

Method Detail

__call() ¶ public

__call(string $method, array $args): Elastica\Query\AbstractQuery

Helps calling the and() and or() methods transparently.

Parameters
string $method

The method name.

array $args

The argumemts to pass to the method.

Returns
Elastica\Query\AbstractQuery

_parseQuery() ¶ protected

_parseQuery(string $field, mixed $value): Elastica\Query\AbstractQuery

Parses a field name containing an operator into a Filter object.

Parameters
string $field

The filed name containing the operator

mixed $value

The value to pass to the query

Returns
Elastica\Query\AbstractQuery

and_() ¶ public

and_(): Elastica\Query\BoolQuery

Combines all the passed arguments in a single bool query using the "must" clause.

Example:

{{{ $bool = $builder->and( $builder->terms('tags', ['cool', 'stuff']), $builder->exists('comments') ); }}}

Returns
Elastica\Query\BoolQuery

between() ¶ public

between(string $field, mixed $from, mixed $to): Elastica\Query\Range

Returns a Range query object setup to query documents having the field between a from and a to value

Parameters
string $field

The field to query by.

mixed $from

The lower bound value.

mixed $to

The upper bound value.

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

bool() ¶ public

bool(): Elastica\Query\BoolQuery

Returns a bool query that can be chained with the addMust(), addShould(), addFilter and addMustNot() methods.

Returns
Elastica\Query\BoolQuery
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

exists() ¶ public

exists(string $field): Elastica\Query\Exists

Returns an Exists query object setup to query documents having a property present or not set to null.

Parameters
string $field

The field to check for existance.

Returns
Elastica\Query\Exists
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html

geoBoundingBox() ¶ public

geoBoundingBox(string $field, array|string $topLeft, array|string $bottomRight): Elastica\Query\GeoBoundingBox

Returns a GeoBoundingBox query object setup to query documents having a property bound by two coordinates.

Example:

{{{ $query = $builder->geoBoundingBox('location', [40.73, -74.1], [40.01, -71.12]);

$query = $builder->geoBoundingBox( 'location', ['lat => 40.73, 'lon' => -74.1], ['lat => 40.01, 'lon' => -71.12] );

$query = $builder->geoBoundingBox('location', 'dr5r9ydj2y73', 'drj7teegpus6'); }}}

Parameters
string $field

The field to compare.

array|string $topLeft

The top left coordinate.

array|string $bottomRight

The bottom right coordinate.

Returns
Elastica\Query\GeoBoundingBox
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html

geoDistance() ¶ public

geoDistance(string $field, array|string $location, string $distance): Elastica\Query\GeoDistance

Returns an GeoDistance query object setup to query documents having a property in the radius distance of a coordinate.

Example:

{{{ $query = $builder->geoDistance('location', ['lat' => 40.73, 'lon' => -74.1], '10km');

$query = $builder->geoBoundingBox('location', 'dr5r9ydj2y73', '5km'); }}}

Parameters
string $field

The field to compare.

array|string $location

The coordinate from which to compare.

string $distance

The distance radius.

Returns
Elastica\Query\GeoDistance
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html

geoPolygon() ¶ public

geoPolygon(string $field, array $geoPoints): Elastica\Query\GeoPolygon

Returns an GeoPolygon query object setup to query documents having a property enclosed in the polygon induced by the passed geo points.

Example:

{{{ $query= $builder->geoPolygon('location', [ ['lat' => 40, 'lon' => -70], ['lat' => 30, 'lon' => -80], ['lat' => 20, 'lon' => -90], ]);

$query = $builder->geoPolygon('location', [ 'drn5x1g8cu2y', ['lat' => 30, 'lon' => -80], '20, -90', ]); }}}

Parameters
string $field

The field to compare.

array $geoPoints

List of geo points that form the polygon

Returns
Elastica\Query\GeoPolygon
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html

geoShape() ¶ public

geoShape(string $field, array $geoPoints, string $type = Elastica\Query\GeoShapeProvided::TYPE_ENVELOPE): Elastica\Query\GeoShapeProvided

Returns an GeoShapeProvided query object setup to query documents having a property enclosed in the specified geometrical shape type.

Example:

{{{ $query = $builder->geoShape('location', [[13.0, 53.0], [14.0, 52.0]], 'envelope');

$query = $builder->geoShape('location', [ [[-77.03653, 38.897676], [-77.009051, 38.889939]], 'linestring' ]); }}}

You can read about the supported shapes and how they are created here: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html

Parameters
string $field

The field to compare.

array $geoPoints

List of geo points that form the shape.

string $type optional

The shape type to use (envelope, linestring, polygon, multipolygon...)

Returns
Elastica\Query\GeoShapeProvided
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html

geoShapeIndex() ¶ public

geoShapeIndex(string $field, string $id, string $type, string $index = 'shapes', string $path = 'shape'): Elastica\Query\GeoShapePreIndexed

Returns an GeoShapePreIndex query object setup to query documents having a property enclosed in the specified geometrical shape type.

Example:

{{{ $query = $builder->geoShapeIndex('location', 'DEU', 'countries', 'shapes', 'location'); }}}

Parameters
string $field

The field to compare.

string $id

The ID of the document containing the pre-indexed shape.

string $type

Index type where the pre-indexed shape is.

string $index optional

Name of the index where the pre-indexed shape is.

string $path optional

The field specified as path containing the pre-indexed shape.

Returns
Elastica\Query\GeoShapePreIndexed
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html

gt() ¶ public

gt(string $field, mixed $value): Elastica\Query\Range

Returns a Range query object setup to query documents having the field greater than the provided value.

Parameters
string $field

The field to query by.

mixed $value

The value to compare with.

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

gte() ¶ public

gte(string $field, mixed $value): Elastica\Query\Range

Returns a Range query object setup to query documents having the field greater than or equal the provided value.

Parameters
string $field

The field to query by.

mixed $value

The value to compare with.

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

hasChild() ¶ public

hasChild(string|Elastica\Query|Elastica\Query\AbstractQuery $query, string $type): Elastica\Query\HasChild

Accepts a query and the child type to run against, and results in parent documents that have child docs matching the query.

Parameters
string|Elastica\Query|Elastica\Query\AbstractQuery $query

The query.

string $type

The child type to query against.

Returns
Elastica\Query\HasChild
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html

hasParent() ¶ public

hasParent(string|Elastica\Query|Elastica\Query\AbstractQuery $query, string $type): Elastica\Query\HasParent

Query by child documents having parent documents matching the query

Parameters
string|Elastica\Query|Elastica\Query\AbstractQuery $query

The query.

string $type

The parent type to query against.

Returns
Elastica\Query\HasParent
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html

ids() ¶ public

ids(array $ids = []): Elastica\Query\Ids

Query documents that only have the provided ids.

Parameters
array $ids optional

The list of ids to query by.

Returns
Elastica\Query\Ids
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html

limit() ¶ public

limit(int $limit): Elastica\Query\Limit

Limits the number of documents (per shard) to execute on.

Parameters
int $limit

The maximum number of documents to query.

Returns
Elastica\Query\Limit
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-limit-query.html

lt() ¶ public

lt(string $field, mixed $value): Elastica\Query\Range

Returns a Range query object setup to query documents having the field smaller than the provided value.

Parameters
string $field

The field to query by.

mixed $value

The value to compare with.

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

lte() ¶ public

lte(string $field, mixed $value): Elastica\Query\Range

Returns a Range query object setup to query documents having the field smaller or equals than the provided value.

Parameters
string $field

The field to query by.

mixed $value

The value to compare with.

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

match() ¶ public

match(string $field, string $value): Elastica\Query\Match

Returns a Match query object that query documents that have fields containing a match.

Example:

{{{ $builder->match('user.name', 'jose'); }}}

Parameters
string $field

The field to query by.

string $value

The match to find in field.

Returns
Elastica\Query\Match
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

matchAll() ¶ public

matchAll(): Elastica\Query\MatchAll

A query that returns all documents.

Returns
Elastica\Query\MatchAll
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html

nested() ¶ public

nested(string $path, Elastica\Query\AbstractQuery $query): Elastica\Query\Nested

Returns a Nested query object setup to query sub documents by a path.

Example:

{{{ $builder->nested('comments', $builder->term('author', 'mark')); }}}

Parameters
string $path

A dot separated string denoting the path to the property to query.

Elastica\Query\AbstractQuery $query

The query conditions.

Returns
Elastica\Query\Nested
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

not() ¶ public

not(Elastica\Query\AbstractQuery|array $query): Elastica\Query\BoolQuery

Returns a BoolQuery query with must_not field that is typically ussed to negate another query expression

Parameters
Elastica\Query\AbstractQuery|array $query

The query to negate

Returns
Elastica\Query\BoolQuery
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

or_() ¶ public

or_(): Elastica\Query\BoolQuery

Combines all the passed arguments in a single BoolQuery query using should clause.

Example:

{{{ $bool = $builder->or( $builder->not($builder->exists('tags')), $builder->exists('comments') ); }}}

Returns
Elastica\Query\BoolQuery

parse() ¶ public

parse(array|Elastica\Query\AbstractQuery $conditions): array

Converts an array into a single array of query objects

Parsing a single array:

{{{ $query = $builder->parse([ 'name' => 'mark', 'age <=' => 35 ]);

// Equivalent to: $query = [ $builder->term('name', 'mark'), $builder->lte('age', 35) ]; }}}

Creating "or" conditions:

{{{ $query = $builder->parse([ 'or' => [ 'name' => 'mark', 'age <=' => 35 ] ]);

// Equivalent to: $query = [$builder->or( $builder->term('name', 'mark'), $builder->lte('age', 35) )]; }}}

Negating conditions:

{{{ $query = $builder->parse([ 'not' => [ 'name' => 'mark', 'age <=' => 35 ] ]);

// Equivalent to: $query = [$builder->not( $builder->and( $builder->term('name', 'mark'), $builder->lte('age', 35) ) )]; }}}

Checking for field existance

{{{ $query = $builder->parse([ 'name is' => null, 'age is not' => null ]);

// Equivalent to: $query = [ $builder->not($builder->exists('name')), $builder->exists('age') ]; }}}

Checking if a value is in a list of terms

{{{ $query = $builder->parse([ 'name in' => ['jose', 'mark'] ]);

// Equivalent to: $query = [$builder->terms('name', ['jose', 'mark'])] }}}

The list of supported operators is:

<, >, <=, >=, in, not in, is, is not, !=

Parameters
array|Elastica\Query\AbstractQuery $conditions

The list of conditions to parse.

Returns
array

prefix() ¶ public

prefix(string $field, string $prefix, float $boost = 1.0): Elastica\Query\Prefix

Returns a Prefix query to query documents that have fields containing terms with a specified prefix

Parameters
string $field

The field to query by.

string $prefix

The prefix to check for.

float $boost optional

The optional boost

Returns
Elastica\Query\Prefix
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html

range() ¶ public

range(string $field, array $args): Elastica\Query\Range

Returns a Range query object setup to query documents having the field greater than the provided values.

The $args array accepts the following keys:

  • gte: greater than or equal
  • gt: greater than
  • lte: less than or equal
  • lt: less than
Parameters
string $field

The field to query by.

array $args

An array describing the search range

Returns
Elastica\Query\Range
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

regexp() ¶ public

regexp(string $field, string $regexp, float $boost = 1.0): Elastica\Query\Regexp

Returns a Regexp query to query documents based on a regular expression.

Example:

{{{ $builder->regexp('name.first', 'ma.*'); }}}

Parameters
string $field

The field to query by.

string $regexp

The regular expression.

float $boost optional

Boost

Returns
Elastica\Query\Regexp
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html

script() ¶ public

script(array|string|Elastica\Script\AbstractScript $script): Elastica\Query\Script

Returns a Script query object that allows to query based on the return value of a script.

Example:

{{{ $builder->script("doc['price'].value > 1"); }}}

Parameters
array|string|Elastica\Script\AbstractScript $script

The script.

Returns
Elastica\Query\Script
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html

simpleQueryString() ¶ public

simpleQueryString(array|string $fields, string $string): Elastica\Query\SimpleQueryString

Returns a SimpleQueryString object that allows to query based on a search string.

Example:

{{{ $builder->simpleQueryString(['body'], '"fried eggs" +(eggplant | potato) -frittata'); }}}

Parameters
array|string $fields

The fields to search within

string $string

The pattern to find in the fields

Returns
Elastica\Query\SimpleQueryString
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

term() ¶ public

term(string $field, string $value): Elastica\Query\Term

Returns a Term query object that query documents that have fields containing a term.

Example:

{{{ $builder->term('user.name', 'jose'); }}}

Parameters
string $field

The field to query by.

string $value

The term to find in field.

Returns
Elastica\Query\Term
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

terms() ¶ public

terms(string $field, array $values): Elastica\Query\Terms

Returns a Terms query object that query documents that have fields containing some terms.

Example:

{{{ $builder->terms('user.name', ['jose', 'mark']); }}}

Parameters
string $field

The field to query by.

array $values

The list of terms to find in field.

Returns
Elastica\Query\Terms
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

type() ¶ public

type(string $type): Elastica\Query\Type

Returns a Type query object that query documents matching the provided document/mapping type.

Example:

{{{ $builder->type('products'); }}}

Parameters
string $type

The type name

Returns
Elastica\Query\Type
See Also
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs