Elasticsearch offers a rich set of query capabilities, including geospatial queries and regex-based searches, which go beyond the traditional query types found in SQL databases. The Laravel-Elasticsearch integration provides a seamless interface to leverage these powerful features directly within your Laravel models.
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.
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.
This will only return the documents where the description field contains the phrase ‘loves es…’. Ex: ‘loves espresso’, ‘loves essays’ and ‘loves eskimos’ etc
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.
The whereFuzzy method allows you to query for documents based on a fuzzy search within a field. This is useful when you need to search for approximate matches to a given value.
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.
The whereGeoBox 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.
Method Signature
/**
* Filter results based on a geo-point field within a defined box on a map.
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.
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.
$bodyParams= [
'query'=> [
'match'=> [
'color'=>'silver',
],
],
];
Product::rawSearch($bodyParams, $optionsParams= []); //Will search within the products index
Product::rawDsl($dsl); //Will use the given $dsl as is and return the raw response from Elasticsearch
The DSL example above uses the match query to search for products with the color ‘silver’
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.
$body= [
'aggs'=> [
'price_ranges'=> [
'range'=> [
'field'=>'price',
'ranges'=> [
['to'=>100],
['from'=>100, 'to'=>500],
['from'=>500, 'to'=>1000],
['from'=>1000],
],
],
'aggs'=> [
'sales_over_time'=> [
'date_histogram'=> [
'field'=>'datetime',
'fixed_interval'=>'1d',
],
],
],
],
],
];
returnProduct::rawAggregation($body);
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.
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.