Laravel 包

Elastic Bridge: Eloquent-Style Queries for Elasticsearch and OpenSearch

发布
Elastic Bridge: Eloquent-Style Queries for Elasticsearch and OpenSearch image

Elastic Bridge is a Laravel package by Agyenim Boateng for querying Elasticsearch and OpenSearch with an Eloquent-style API. You define a class for an index, then chain PHP methods for full-text search and filters without writing JSON DSL by hand.

If you have used a fluent PHP Elasticsearch query builder , the query syntax will look familiar. Elastic Bridge also provides document casting, accessors and mutators, bulk indexing, and queries across multiple indexes.

Define a bridge for your index

The package calls its model classes "bridges". Generate one with Artisan:

php 工匠 make:bridge HotelRoom

Each bridge extends the package's base class. You can set its index explicitly:

命名空间 App\Bridges ;
使用 Lacasera\ElasticBridge\ElasticBridge ;
班级 HotelRoom 延伸 ElasticBridge
{
受保护 索引 = 'hotel-rooms' ;
}

Bridges support a $casts property for converting document attributes, including dates and enums. Accessors and mutators let you change values when reading or assigning attributes, using Laravel's Attribute 班级。

Combine full-text search and filters

A room search can combine a city match with a currency filter and a price limit:

使用 App\Bridges\HotelRoom ;
$rooms = HotelRoom :: asBoolean ()
-> mustMatch ( '城市' , 'accra' )
-> filterByTerm ( '代码' , 美元 )
-> filterByRange ( '价格' , 500 , 'lte' )
-> orderBy ( '价格' , 'ASC' )
-> 光标分页 ( 15 )
-> 得到 ([ '姓名' , '价格' , '代码' ]);

The distinction between mustMatch() 和 filterByTerm() follows the search engine's query structure: the former builds a match clause, while the latter adds an exact term filter. Field mappings still determine how your documents are searched.

For a search across fields, use multiMatch() :

$rooms = HotelRoom :: multiMatch (
场地 :[ 'advertiser' , 'service_type' ],
询问 : 'hotel' ,
) -> 得到 ();

这 full-text search documentation explains the supported query types. In v2, multiMatch() 和 matchPhrase() automatically nest inside a boolean query. A standalone match() 用途 asRaw() 或者 asMatch() ; inside a boolean query, use mustMatch() 或者 shouldMatch() 。

Cursor pagination uses search_after and requires a deterministic sort. The returned collection provides next and previous sort values through links() , which you pass to a subsequent cursorPaginate() 称呼。

Retrieve documents and aggregations together

You can call scalar aggregate methods such as avg() , sum() , 和 count() , or attach an aggregation to a document query:

$rooms = HotelRoom :: asBoolean ()
-> mustMatch ( '城市' , 'accra' )
-> withAggregate ( 'avg' , '价格' )
-> 得到 ();
$averagePrice = $rooms -> priceAvg ();

In v2, aggregation results belong to the returned collection instance. Separate result sets retain their own aggregation values, including in long-lived workers. The package also returns a Stats object for stats() and a collection of bucket objects for histogram() 。

Test without a search cluster

The package's fake() method supplies a search response for either backend. You can inspect the generated query with toQuery() :

民众 功能 test_builds_currency_filter () : 空白
{
HotelRoom :: 伪造的 ([
“热门歌曲” => [
'全部的' => [ '价值' => 0 , '关系' => 'eq' ],
“热门歌曲” => [],
],
]);
$查询 = HotelRoom :: asBoolean ()
-> filterByTerm ( '代码' , 美元 )
-> toQuery ();
$this -> 断言相同 ([
'询问' => [
'bool' => [
'filter' => [
[ 'term' => [ '代码' => 美元 ]],
],
],
],
], $query);
}

This checks the query your application builds without requiring a running Elasticsearch or OpenSearch instance. The testing documentation covers the fake connection API.

Installation and backend configuration

The documented requirements are PHP 8.2 or 8.3, Laravel 10, 11, or 12, and Elasticsearch 8.x or OpenSearch 2.x.

作曲家 要求 lacasera/elastic-bridge
php 工匠 供应商:发布 --标签= "elastic-bridge-config"

放 SEARCH_DRIVER 到 elasticsearch 或者 opensearch and configure the connection in config/elasticbridge.php . Both drivers use the same fluent query API. Authentication options include basic auth and API keys, plus AWS SigV4 for OpenSearch with the optional AWS SDK dependency.

The package also ships guidelines and a development skill for Laravel Boost's skills support 。 跑步 php artisan boost:install in a project using Boost to make the package guidance available to your coding agent.

You can find the setup instructions and API reference in the Elastic Bridge documentation and the MIT-licensed package on GitHub 。

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

Filed in

赞助

masteringlaravel logo
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Laravel AI SDK and Laravel MCP Security Fixes: Update Now image

Laravel AI SDK and Laravel MCP Security Fixes: Update Now

阅读文章
Mailbox for Laravel: Preview and Test Rendered Email image

Mailbox for Laravel: Preview and Test Rendered Email

阅读文章
Count Worker Crashes as Job Exceptions in Laravel 13.34 image

Count Worker Crashes as Job Exceptions in Laravel 13.34

阅读文章
Laravel Release Cycle: Versions, Support Policy, and Dates image

Laravel Release Cycle: Versions, Support Policy, and Dates

阅读文章
Unlearn.dev goes free for a weekend, October 10 and 11 image

Unlearn.dev goes free for a weekend, October 10 and 11

阅读文章
Tashil: Laravel Subscription Plans and Feature Usage Limits image

Tashil: Laravel Subscription Plans and Feature Usage Limits

阅读文章