Elasticsearch Specific Queries
This content is for v3.9. Switch to the latest version for up-to-date documentation.
GeoBox
The filterGeoBox
method allows you to retrieve documents based on geospatial data, specifically targeting documents where a geo-point field falls within a defined “box” on a map. This is particularly useful for applications requiring location-based filtering, such as finding all events within a specific geographical area.
GeoPoint
The filterGeoPoint
method filters results based on their proximity to a given point, specified by latitude and longitude, within a certain radius.
The $distance
parameter is a string combining a numeric value and a distance unit (e.g., km for kilometers, mi for miles). Refer to the Elasticsearch documentation on distance units for more information.
Where Exact
This method allows you to query for exact case-sensitive matches within a field. This is useful when you need to find a specific value. Keep in mind that the field will also need to have a keyword mapping.
Under the hood, this method uses the term
query from Elasticsearch’s Query DSL.
This will only return the documents where the name field is exactly ‘John Smith’. ‘john smith’ or ‘John’ will not be returned.
Where Phrase
This method allows you to query for exact phrases within a field. This is useful when you need to search for a specific sequence of words within a text field.
Under the hood, this method uses the match_phrase
query from Elasticsearch’s Query DSL.
This will only return the documents where the description field contains the exact phrase ‘loves espressos’. Individual tokens like ‘loves’ or ‘espressos’ will not be returned in isolation.
Where Phrase Prefix
Similar to WherePhrase
, this method allows you to query for exact phrases where the last word starts with a particular prefix.
Under the hood, this method uses the match_phrase_prefix
query from Elasticsearch’s Query DSL.
This will only return the documents where the description field contains the the phrase ‘loves es…’. Ex: ‘loves espresso’, ‘loves essays’ and ‘loves eskimos’ etc
Where Timestamp
This method allows you to query for timestamps on a known field and will sanitize the input to ensure it is a valid timestamp for both seconds and milliseconds.
This will only return the documents where the last_viewed field is less than or equal to the timestamp 1713911889521 ms.
Where Regex
The WhereRegex
method allows you to query for documents based on a regular expression pattern within a field.
The first example will return documents where the color field matches the pattern ‘bl(ue)?(ack)?’, which means it can be ‘blue’ or ‘black’. The second example will return documents where the color field matches the pattern ‘bl…*’, which means it starts with ‘bl’ and has at least three more characters. Both should return Blue or Black from the colors field.
RAW DSL Queries
For scenarios where you need the utmost flexibility and control over your Elasticsearch queries, the Laravel-Elasticsearch integration provides the capability to directly use Elasticsearch’s Query DSL (Domain Specific Language). The results will still be returned as collections of Eloquent models.
The DSL example above uses the match
query to search for products with the color ‘silver’
RAW Aggregation Queries
Similar to raw search queries, you can also execute raw aggregation queries using Elasticsearch’s Aggregation DSL. This allows you to perform complex aggregations on your data and retrieve the results in a structured format.
The aggregation example above uses the range
aggregation to group products into price ranges and the date_histogram
aggregation to group sales over time within each price range.
To DSL
This method returns the parsed DSL query from the query builder. This can be useful when you need to inspect the raw query being generated by the query builder.
This will return the raw DSL query generated by the query builder instance.