pion/laravel-lelastico

v0.2.4 2022-03-17 09:19 UTC

This package is auto-updated.

Last update: 2024-09-18 15:43:03 UTC


README

Total Downloads Latest Stable Version Latest Unstable Version License

介绍

本库的重点是简化弹性索引(带映射/设置)的管理,以及创建可重复查询构建(手动或从请求)。

  • 添加了通过包装类管理 Elasticsearch 索引的能力,这将帮助您创建/更新索引、写入文档(使用批量模式)等。
  • 添加了为每个索引创建带查询过滤器的查询构建器的功能。
  • 添加了从请求数据构建查询构建器的功能(可重用组件)。

要求

  • Composer
  • PHP 7.4+

安装

1. 将自定义仓库添加到 composer.json

"repositories": {
    {
        "type": "git",
        "url": "https://github.com/pionl/elasticsearch-query-builder.git"
    }
}

2. 通过 composer 安装

composer require pion/laravel-lelastico

3. 添加服务提供者(Laravel 5.4 及以下 - 支持自动发现)

\Lelastico\LelasticoServiceProvider::class,

依赖

用法

设置弹性主机

对于开发,您可以使用配置中的默认值而无需密码:localhost:9200

使用 ELASTICSEARCH_HOSTS 环境变量设置 Elasticsearch 主机。 格式

解析 Elasticsearch 客户端

$client = resolve(\Elasticsearch\Client::class);
$client = $container->make(\Elasticsearch\Client::class);

映射类型常量

使用常量如

  • MappingTypes::KEYWORD
  • MappingTypes::TEXT
  • MappingTypes::TEXT_WITH_KEYWORD
  • MappingTypes::SHORT
  • MappingTypes::SHORT_WITH_KEYWORD
  • MappingTypes::LONG
  • MappingTypes::LONG_WITH_KEYWORD
  • MappingTypes::INTEGER
  • MappingTypes::INTEGER_WITH_KEYWORD
  • MappingTypes::DATE
  • MappingTypes::BOOLEAN
  • MappingTypes::FLOAT
  • MappingTypes::textWithAnalyzer(string $analyzer, string $searchAnalyzer), builds

添加索引

  1. 通过扩展 AbstractElasticIndex 并实现 createIndexName 为弹性索引创建您的索引

    • 实现 propertyMappings 以进行自定义映射。
    protected function propertyMappings(): array
    {
      return [
          'id' => MappingTypes::KEYWORD,
          'name' => MappingTypes::TEXT_WITH_KEYWORD,
          'is_verified' => MappingTypes::BOOLEAN,
          'email' => MappingTypes::textWithAnalyzer('fulltext'),
          'created_at' => MappingTypes::DATE,
          'updated_at' => MappingTypes::DATE,
          'deleted_at' => MappingTypes::DATE,
      ];
    }   
    • 实现 settings 以进行自定义索引设置
    protected function settings(): array
    {
      // Add support for partial text search
      return [
          'index' => [
              'analysis' => [
                  'filter' => [
                      'fulltext_filter' => [
                          // Always from start of beginning of each token
                          'type' => 'edge_ngram',
                          'min_gram' => 3,
                          'max_gram' => 20,
                      ],
                  ],
                  'analyzer' => [
                      'fulltext' => [
                          'type' => 'custom',
                          'tokenizer' => 'standard',
                          'filter' => ['lowercase', 'fulltext_filter'],
                      ],
                  ],
              ],
          ],
      ];
    }
  2. 使用索引类创建或更新 lelastico.php 配置

    return [
        'indices' => [
            \App\ElasticSearch\Indices\UsersIndex::class,
        ],
    ];
  3. 使用 php artisan elastic:indices 更新或创建弹性索引(存储设置/映射)

    Updates the elastic indices
            --only="only", handle only given index
            --f, will delete the index and data. Will new index with mappings
            --d, will delete the index and data
            --skip-settings-update, when upadting, the index is closed / opened due the settings update. You can skip it
            by provided this option.
    

排序

默认情况下,我们按照 _id 排序,以确保分页正确。

您可以使用 $builder->setSortById(false); 关闭此功能。

要启用可排序行为,请向您的 AbstractBuilder 实例添加 HasSorting 特性并实现 allowedSortFields 方法。

/**
 * Allowed fields for sorting.
 *
 * Key is the name of the field in the query.
 * Value is the name of the field in the index.
 *
 * @return array
 */
public function allowedSortFields(): array
{
    return [
        'goals' => 'goals_count',
        'minutes' => 'played_minutes',
    ];
}

启用排序后,您可以使用 sort 请求查询参数对结果进行排序。此参数接受以 {field_name}:{sort_direction} 格式排序的字段列表。

排序方向可用为 ascdesc,如果未指定,则默认排序方向为 asc

示例

sort[]=goals

sort[]=goals:asc&sort[]=minutes:desc

配置

  • log_measurement 记录每个查询到日志(默认为 false)。您可以使用 ELASTICSEARCH_LOG_MEASUREMENT 环境变量。
  • log_debug 在本地环境中,将每个查询数据调试日志记录到日志中(默认为 true)。您可以使用 ELASTICSEARCH_LOG_DEBUG 环境变量。
  • service 允许更改可用索引(实现 IndicesServiceContract 或扩展 IndicesService)
  • prefix 用于索引名称的前缀 - 使用 APP_NAME 并将 '-' 替换为 '_',将名称转换为缩略词版本。
  • hosts 您的 Elasticsearch 的 IP 列表 - https://elastic.ac.cn/guide/en/elasticsearch/client/php-api/current/configuration.html。使用 ; 分隔符。默认 localhost:9200。

待办事项

  • 改进文档
  • 添加 make 控制台。

更新日志

可在 发布 中找到。

贡献或扩展

有关如何贡献更改的说明,请参阅 CONTRIBUTING.md。所有贡献都受欢迎。

赞助商

本库的创建和改进得益于客户项目。

版权和许可

laravel-elasticoMartin Kluska 编写,并发布在 MIT 许可证 下。

版权(c)2020 Martin Kluska