shufo / laravel-opensearch
在Laravel应用程序中使用官方PHP OpenSearch客户端的简单方法。
v0.2.2
2023-08-10 03:07 UTC
Requires
- php: ^7.3|^8.0
- ext-json: *
- guzzlehttp/psr7: ^1.7|^2.0
- illuminate/contracts: ^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
- psr/http-message: ^1.0
- shufo/opensearch-php: ^2.0.5
Requires (Dev)
- limedeck/phpunit-detailed-printer: ^6.0
- mockery/mockery: ^1.4.3
- orchestra/testbench: ^6.5|^7.0
- phpunit/phpunit: ^9.4
Suggests
- aws/aws-sdk-php: Required to connect to an OpenSearch host on AWS (^3.80)
This package is auto-updated.
Last update: 2024-09-15 03:49:59 UTC
README
⚠️ 此包仍处于alpha阶段。请勿在生产环境中使用。
在Laravel应用程序中简单使用 官方OpenSearch客户端。
这是 laravel-elasticsearch 的分支。
安装和配置
将仓库添加到您的composer.json
"repositories": [ { "type": "vcs", "url": "https://github.com/shufo/opensearch-php" } ],
然后
composer require shufo/laravel-opensearch
Laravel
包的service provider将自动注册其服务提供者。
发布配置文件
php artisan vendor:publish --provider="Shufo\LaravelOpenSearch\ServiceProvider"
要更改opensearch的主机,将OPENSEARCH_HOST环境变量设置为您的opensearch主机名。
要编辑配置,您可以编辑config/opensearch.php。
$ vim config/opensearch.php
用法
使用外观
// search >>> OpenSearch::search([ 'index' => 'example', 'body' => [ 'query' => [ 'match' => [ 'id' => '123' ] ] ] ]) => [ "took" => 1, "timed_out" => false, "_shards" => [ "total" => 1, "successful" => 1, "skipped" => 0, "failed" => 0, ], "hits" => [ "total" => [ "value" => 1, "relation" => "eq", ], "max_score" => 0.6931471, "hits" => [ [ "_index" => "example", "_type" => "_doc", "_id" => "1", "_score" => 0.6931471, "_source" => [ "id" => "123", "body" => "test", ], ], ], ], ] // create index OpenSearch::indices()->create([ 'index' => 'example', 'body' => [ 'mappings' => [ 'properties' => [ 'id' => [ 'type' => 'long', ], 'text' => [ 'type' => 'text', ] ] ] ], ]) // add document to index OpenSearch::index([ "id" => "123", "body" => [ "id" => "123", "text" => "foo", ], "index" => "example", ]); // delete index OpenSearch::indices()->delete(['index' => 'example']); // SQL (currently it's available only select operation) >>> OpenSearch::sql()->query(["query" => "select * from example", "fetch_size" => 1]) => [ "schema" => [ [ "name" => "body", "type" => "text", ], [ "name" => "id", "type" => "keyword", ], ], "cursor" => "d:eyJhIjp7fSwicyI6IkZHbHVZMngxWkdWZlkyOXVkR1Y0ZEY5MWRXbGtEWEYxWlhKNVFXNWtSbVYwWTJnQkZrTmZVamR0VEc1ZlUwSmxOM2h4U2w5bFRWQjRaMUVBQUFBQUFBQUFxaFpqUjFGckxVRm9YMUl6Vnpkc2NXaHlabkk1VFZGbiIsImMiOlt7Im5hbWUiOiJib2R5IiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJpZCIsInR5cGUiOiJrZXl3b3JkIn1dLCJmIjoxLCJpIjoiZXhhbXBsZSIsImwiOjF9", "total" => 2, "datarows" => [ [ "test", "123", ], ], "size" => 1, "status" => 200, ]
使用Opensearch客户端构建器
use OpenSearch\ClientBuilder; $data = [ 'body' => [ 'testField' => 'abc' ], 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', ]; $client = ClientBuilder::create()->build(); $return = $client->index($data);
贡献
- 分支它
- 创建您的功能分支(
git checkout -b my-new-feature) - 提交您的更改(
git commit -am '添加一些功能') - 推送到分支(
git push origin my-new-feature) - 创建新的拉取请求
许可证
MIT