This content is for v3.9. Switch to the latest version for up-to-date documentation.
Elasticsearch To Eloquent Mapping
Glossary of common Elasticsearch queries and their Eloquent equivalents, providing a quick reference for developers coming from Elasticsearch to Laravel’s Eloquent.
Full Text Queries
Section titled “Full Text Queries”Match query
Section titled “Match query”$products = Product::where('manufacturer.country', 'England')->take(10)->get();
{ "index": "products", "body": { "query": { "match": { "manufacturer.country": "England" } }, "_source": [ "*" ] }, "size": 10}
Match phrase query
Section titled “Match phrase query”return Person::wherePhrase('description', 'loves espressos')->get();
{ "index": "people", "body": { "query": { "match_phrase": { "description": "loves espressos" } }, "_source": [ "*" ] }}
Match phrase prefix query
Section titled “Match phrase prefix query”return Person::wherePhrasePrefix('description', 'loves es')->get();
{ "index": "people", "body": { "query": { "match_phrase_prefix": { "description": "loves es" } }, "_source": [ "*" ] }}
Term level queries
Section titled “Term level queries”Term query
Section titled “Term query”return Person::whereExact('name', 'John Smith')->get();
This will only return the documents where the name field is exactly ‘John Smith’. ‘john smith’ or ‘John’ will not be returned.
{ "index": "people", "body": { "query": { "term": { "name.keyword": "John Smith" } }, "_source": [ "*" ] }}
Terms query
Section titled “Terms query”$products = Product::whereIn('status', [1,5,11])->get();
{ "index": "products", "body": { "query": { "terms": { "status": [ 1, 5, 11 ] } }, "_source": [ "*" ] }}
Range query
Section titled “Range query”$products = Product::whereBetween('in_stock', [10, 100])->get();
Find all products with an in_stock value between 10 and 100 (including 10 and 100)
{ "index": "products", "body": { "query": { "range": { "in_stock": { "gte": 10, "lte": 100 } } }, "_source": [ "*" ] }}
$products = Product::where('status','>=', 3)->take(10)->get();
{ "index": "products", "body": { "query": { "range": { "status": { "gte": 3 } } }, "_source": [ "*" ] }, "size": 10}
Exists query
Section titled “Exists query”$products = Product::whereNotIn('color', ['red','green'])->whereNotNull('color')->get();
{ "index": "products", "body": { "query": { "bool": { "must": [ { "bool": { "must_not": { "terms": { "color": [ "red", "green" ] } } } }, { "exists": { "field": "color" } } ] } }, "_source": [ "*" ] }}
$products = Product::whereNull('color')->get();
{ "index": "products", "body": { "query": { "bool": { "must_not": [ { "exists": { "field": "color" } } ] } }, "_source": [ "*" ] }}
Fuzzy query
Section titled “Fuzzy query”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Prefix query
Section titled “Prefix query”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Wildcard query
Section titled “Wildcard query”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Multi-match query
Section titled “Multi-match query”Most Field
Section titled “Most Field”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Cross Fields
Section titled “Cross Fields”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Best Fields
Section titled “Best Fields”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Phrase
Section titled “Phrase”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Phrase Prefix
Section titled “Phrase Prefix”- Not directly supported in v3.9/3.8, but can be implemented using search queries
Geo Queries
Section titled “Geo Queries”Geo-bounding box query
Section titled “Geo-bounding box query”// Define the top-left and bottom-right coordinates of the box$topLeft = [-10, 10]; // [latitude, longitude]$bottomRight = [10, -10]; // [latitude, longitude]
// Retrieve UserLogs where 'agent.geo' falls within the defined box$logs = UserLog::where('status', 7)->filterGeoBox('agent.geo', $topLeft, $bottomRight)->get();
{ "index": "user_logs", "body": { "query": { "bool": { "must": [ { "match": { "status": 7 } } ], "filter": { "geo_bounding_box": { "agent.geo": { "top_left": [ -10, 10 ], "bottom_right": [ 10, -10 ] } } } } }, "_source": [ "*" ] }}
Geo-distance query
Section titled “Geo-distance query”// Specify the central point and radius$point = [0, 0]; // [latitude, longitude]$distance = '20km';
// Retrieve UserLogs where 'agent.geo' is within 20km of the specified point$logs = UserLog::where('status', 7)->filterGeoPoint('agent.geo', $distance, $point)->get();
{ "index": "user_logs", "body": { "query": { "bool": { "must": [ { "match": { "status": 7 } } ], "filter": { "geo_distance": { "distance": "20km", "agent.geo": { "lat": 0, "lon": 0 } } } } }, "_source": [ "*" ] }}