olekjs/elasticsearch

v1.12.0 2024-04-19 17:27 UTC

This package is auto-updated.

Last update: 2024-09-19 18:32:47 UTC


README

这是一个简单的包,用于将 Elasticsearch API 集成到 Laravel 项目中。

此包仍在开发中。

安装

使用 composer 安装

  composer require olekjs/elasticsearch

.env 文件中设置 Elasticsearch 基础 URL

ELASTICSEARCH_URL=https://:9200

自定义索引

自定义索引与 Laravel 模型类似。首先,您需要定义自己的类,该类扩展了 AbstractIndex

<?php

namespace App\Indices;

use Olekjs\Elasticsearch\Contracts\AbstractIndex;

class LogIndex extends AbstractIndex
{
    protected string $index = 'logs';
}

然后,您可以使用自定义索引执行搜索操作,无需指定索引名称

use App\Indices\LogIndex;

...

LogIndex::query()->where('type', 'error')->get();

通用用法

有三种请求选项

  1. 使用构建器
use Olekjs\Elasticsearch\Builder\Builder;

...

$results = Builder::query()
    ->index('shops')
    ->where('slug', 'test-slug')
    ->where('email', 'test@test.com')
    ->whereLike('name', 'test')
    ->whereIn('_id', [123, 321])
    ->get(); // Resturns SearchResponseDto
  1. 使用自定义索引 如何使用自定义索引?请参阅:自定义索引
use App\Indices\MyCustomIndex;

...

$results = MyCustomIndex::query()
    ->where('slug', 'test-slug')
    ->where('email', 'test@test.com')
    ->whereLike('name', 'test')
    ->whereIn('_id', [123, 321])
    ->get(); // Resturns SearchResponseDto
  1. 直接使用客户端类
use Olekjs\Elasticsearch\Client;

...

$client = new Client();
$client->search('logs', [
  'query' => [
    'bool' => ['filter' => ['term' => ['email' => 'test@test.com']]]
  ]
]);

示例

  1. 索引新文档
use Olekjs\Elasticsearch\Client;

...

$client = new Client();
$client->create(
  index: 'logs',
  id: 1,
  data: ['level' => 'error', 'message' => 'Error...', ...]
);
  1. 查找文档
use Olekjs\Elasticsearch\Client;

...

$client = new Client();

// If document doesn't exists null will be returned
$client->find(
  index: 'logs',
  id: 1
);

// If document doesn't exists NotFoundResponseException will be thrown
$client->findOrFail(
  index: 'logs',
  id: 2
);

客户端类参考

    public function search(string $index, array $data): SearchResponseDto;

    public function find(string $index, string|int $id): ?FindResponseDto;

    public function findOrFail(string $index, string|int $id): FindResponseDto;

    public function create(string $index, string|int $id, array $data): IndexResponseDto;

    public function update(string $index, string|int $id, array $data): IndexResponseDto;

    public function delete(string $index, string|int $id): IndexResponseDto;

    public function searchWhereIn(string $index, string $field, array $values): SearchResponseDto;

    public function searchWhereKeyword(string $index, string $field, string $value): SearchResponseDto;

    public function searchWhereLike(string $index, string $field, string|int|float $value): SearchResponseDto;

    public function increment(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;

    public function decrement(string $index, string|int $id, string $field, int $value = 1): IndexResponseDto;

运行测试

要运行测试,请运行以下命令

  php vendor/bin/testbench package:test