msdev/solarium-bundle

与 solarium solr 客户端集成。

dev-master / 5.x-dev 2021-08-23 12:02 UTC

This package is auto-updated.

Last update: 2024-09-04 11:58:08 UTC


README

Latest Version Total Downloads

关于:针对 PHP-8 和 Symfony 5.3 进行升级

NelmioSolariumBundle 提供了与 solarium solr 客户端的集成。

安装

在 composer.json 中需要 msdev/solarium-bundle:dev-master 包并更新依赖。

$ composer require msdev/solarium-bundle:dev-master

将 NelmioSolariumBundle 添加到 AppKernel.php

public function registerBundles()
{
    $bundles = array(
        ...
        new Nelmio\SolariumBundle\NelmioSolariumBundle(),
        ...
    );
    ...
}

基本配置

快速开始配置

nelmio_solarium: ~

提供了一个具有默认选项的 Solarium_Client 服务(https://:8983/solr

    $client = $this->get('solarium.client');

在 config.yml 中配置您的端点

nelmio_solarium:
    endpoints:
        default:
            scheme: http
            host: localhost
            port: 8983
            path: /solr
            core: active
    clients:
        default:
            endpoints: [default]

如果您只有一个端点,则不需要 client 部分

用法

$client = $this->get('solarium.client');
$select = $client->createSelect();
$select->setQuery('foo');
$results = $client->select($select);

更多信息请参阅 Solarium 文档

多个客户端和端点

nelmio_solarium:
    endpoints:
        default:
            host: 192.168.1.2
        another:
            host: 192.168.1.3
    clients:
        default:
            endpoints: [default]
        another:
            endpoints: [another]
    $defaultClient = $this->get('solarium.client');
    $anotherClient = $this->get('solarium.client.another');

您也可以使用自己的名称更改 default 名称,但不要忘记更改 default_client 选项,如果您想访问 solarium.client 服务

nelmio_solarium:
    default_client: firstOne
    endpoints:
        firstOne:
            host: 192.168.1.2
        anotherOne:
            host: 192.168.1.3
    clients:
        firstOne:
            endpoints: [firstOne]
        anotherOne:
            endpoints: [anotherOne]
    $firstOneClient = $this->get('solarium.client');
    //or
    $firstOneClient = $this->get('solarium.client.firstOne');

    $anotherOneClient = $this->get('solarium.client.anotherOne');

从 Solarium 3.x 开始,您也可以在同一个客户端内有多个端点

nelmio_solarium:
    endpoints:
        default:
            host: 192.168.1.2
        another:
            host: 192.168.1.3
    # if you are using all the endpoints, the clients section is not necessary
    clients:
        default:
            endpoints: [default, another]

您还可以设置默认端点

nelmio_solarium:
    endpoints:
        default:
            host: 192.168.1.2
        another:
            host: 192.168.1.3
    clients:
        default:
            endpoints: [default, another]
            default_endpoint: another

您还可以组合多个客户端和端点

nelmio_solarium:
    endpoints:
        one:
            host: 192.168.1.2
        two:
            host: 192.168.1.3
        three:
            host: 192.168.1.4
    clients:
        firstOne:
            endpoints: [one, two]
            default_endpoint: two
        secondOne:
            endpoints: [two, three]
            default_endpoint: three

客户端注册表

您还可以使用服务 solarium.client_registry 通过配置中使用的名称访问您已配置的客户端(例如上述示例)

$registry = $this->get('solarium.client_registry');
$firstOne = $registry->getClient('firstOne');
$secondOne = $registry->getClient('secondOne');

或者如果您已配置默认客户端

$registry = $this->get('solarium.client_registry');
$default = $registry->getClient();

插件

Solarium 与插件一起工作。如果您想使用自己的插件,您可以在包配置中注册插件,使用服务 ID 或插件类

nelmio_solarium:
    clients:
        default:
            plugins:
                test_plugin_service:
                    plugin_service: plugin _service_id
                test_plugin_classname:
                    plugin_class: Some\Plugin\TestPlugin

重写客户端类

要更改客户端类,您可以设置客户端_class 选项

nelmio_solarium:
    clients:
        default:
            client_class: Solarium\Core\Client

自定义客户端使用的 HTTP 适配器

如果您需要自定义客户端执行对 Solr 的 HTTP 请求时使用的适配器,则可以使用 adapter_service 选项指定要作为适配器使用的 symfony 服务的 ID

nelmio_solarium:
    clients:
        default:
            adapter_service: 'my.custom.adapter.service'

HTTP 请求超时

如果您使用的是默认适配器(Curl)且未自定义 adapter_service,则可以使用 adapter_timeout 选项自定义超时。Solarium 默认使用 5 秒的超时。

nelmio_solarium:
    clients:
        default:
            adapter_timeout: 10

许可证

在 MIT 许可证下发布,请参阅 LICENSE。