Skip to content

Query Builder Reference

Below are commonly used methods available on the bridge builder. Chain them fluently from your bridge class.

Term selection

  • asBoolean() — sets a bool query context
  • asRaw() — allows passing a raw body via raw([...])
  • asMatch(), asFuzzy(), asIds(), asPrefix(), asRange(), asRegex(), asTerm(), asTerms(), asTermSet(), asWildCard()

Full-text helpers

  • match(field, query, options = []) — top-level; use with asRaw()/asMatch()
  • orMatch(field, query)
  • matchPhrase(field, query, options = []) — nests itself as a bool must clause
  • multiMatch(fields, query) — nests itself as a bool must clause

Bool helpers

  • shouldMatchAll(boost = 1.0)
  • matchAll(boost = 1.0)
  • mustMatch(field, value)
  • must(query, field, value)
  • mustNot(query, field, payload)
  • mustExist(field) / shouldExist(field)

v2 behavior

Repeating any bool clause (mustMatch, shouldMatch, mustNot, …) appends to an array of clause objects, so chaining multiple clauses of the same type produces valid DSL. multiMatch and matchPhrase no longer require asRaw() — they switch to a bool context automatically.

Pagination and sorting

  • orderBy(field, direction = 'ASC') — throws InvalidQuery on a direction other than asc/desc
  • take(size) / limit(size)
  • skip(from) / offset(from)
  • simplePaginate(size = 15, from = 0)
  • cursorPaginate(size = 15, sort = [])

Execution

  • all(perPage = 15, columns = ['*']) — bounded first page via match_all
  • get(columns = ['*'])
  • count()
  • toQuery(asJson = false) — returns the built body as array or JSON

Aggregations

  • avg(field), min(field), max(field), sum(field)
  • stats(field) returns a Stats object
  • histogram(field, interval) returns buckets
  • withAggregate(type, field, options = []) to attach to a query (results are read from the returned collection, e.g. $rooms->priceAvg())

Filters

  • filterByTerm(field, value)
  • filterByRange(field, value, operator) or chain range(field, operator, value) — operators: gt, gte, lt, lte
  • Geo: filterByGeoShape, filterByGeoDistance, filterByGeoPolygon, filterByGeoDistanceRange, filterByGeoBoundingBox

v2 behavior

Filters are valid inside a bool query. If you add filters without an explicit asBoolean(), the builder now promotes the query into a bool and attaches the filters, instead of silently dropping them.

Indexing and updates

  • create(attributes) — returns created _id (string)
  • save() — updates existing or creates if missing (boolean)
  • increment(field, counter = 1) / decrement(field, counter = 1)
  • bulk(rows, chunkSize = null) — bulk index; returns a BulkResult
  • upsert(rows, chunkSize = null) — bulk update-or-insert (each row needs an id); returns a BulkResult

Index targeting

  • from(index|array) — override the search index for this query (wildcard/comma allowed)
  • into(index) — override the concrete write index for this call

Utilities

  • find(id|array) — returns a single bridge or a collection
  • withValues(values, field = null, options = []) — for queries that accept values

Released under the MIT License.