elastification/php-client-bundle

Symfony2 的 Elastification 客户端包

0.5.2 2016-01-20 11:34 UTC

This package is not auto-updated.

Last update: 2024-09-11 14:42:20 UTC


README

Build Status

配置

以下是使用此包的配置部分

Composer

composer.json 中需要

"elastification/php-client-bundle": "<1.0"

您已选择正确的传输包。如果您未安装 thrift,则应使用 guzzle。

"guzzlehttp/guzzle": "<7.0"

对于 Thrift 传输

"munkie/elasticsearch-thrift-php": "~1.4"

在完成您的 composer.json 后,不要忘记运行 composer update。

AppKernel

在您的 app/config/AppHernel.php 文件中,您应通过将其添加到数组中激活该包

$bundles[] = new Elastification\Bundle\ElastificationPhpClientBundle\ElastificationPhpClientBundle();

应用配置

在您的 app/config.yml 或基于环境的配置中,您可以添加参数(完整的配置示例)

elastification_php_client:
  host: 127.0.0.1
  port: 9200
  protocol: http # http/thrift
  elasticsearch_version: 1.4.1
  search_repository_serializer_dic_id: elastification_php_client.serializer.native #default: elastification_php_client.serializer.native
  index_repository_serializer_dic_id: elastification_php_client.serializer.native #default: elastification_php_client.serializer.native
  document_repository_serializer_dic_id: elastification_php_client.serializer.native #default: elastification_php_client.serializer.native
  replace_version_of_tagged_requests: true #default: false
  logging_enabled: true
  profiler_enabled: true
  jms_serializer_class_map:
      - {index: my-index, type: my-type, class: AppBundle\Entity\MyEntity}

DIC

客户端注册的 DIC 服务 ID 为 elastification_php_client

序列化器

本地序列化器服务 ID: elastification_php_client.serializer.native

Jms 序列化器搜索服务 ID: elastification_php_client.serializer.jms.search

Jms 序列化器文档服务 ID: elastification_php_client.serializer.jms.document

存储库

文档存储库服务 ID: elastification_php_client.repository.document

搜索存储库服务 ID: elastification_php_client.repository.search

索引存储库服务 ID: elastification_php_client.repository.index

Cat 存储库服务 ID: elastification_php_client.repository.cat

标记请求

如果您想注册请求服务,可以使用以下标记: elastification_php_client.request

版本

如何使用

示例

对于所有示例,您应该在您的 Elasticsearch 中创建一些样本数据。

搜索存储库示例

执行简单搜索。此代码是一个可以在控制器动作中执行的示例。

/** @var SearchRepositoryInterface $searchRepo */
$searchRepo = $this->get('elastification_php_client.repository.search');

$query = array(
    'query' => array(
        'term' => array(
            'country' => array(
                'value' => 'germany'
            )
        )
    )
);

$searchRepo->search('my-index', 'my-type', $query);
var_dump($response->getHits());

文档存储库示例

通过 ID 获取单个文档。此代码是一个可以在控制器动作中执行的示例。

/** @var DocumentRepositoryInterface $docRepo */
$docRepo = $this->get('elastification_php_client.repository.document');

var_dump($docRepo->get('my-index', 'my-type', 'yourDocumentId'));

索引存储库示例

检查索引是否存在。此代码是一个可以在控制器动作中执行的示例。

/** @var IndexRepositoryInterface $indexRepo */
$indexRepo = $this->get('elastification_php_client.repository.index');
var_dump($indexRepo->exists('my-index'));

创建索引。此代码是一个可以在控制器动作中执行的示例。

/** @var IndexRepositoryInterface $indexRepo */
$indexRepo = $this->get('elastification_php_client.repository.index');
var_dump($indexRepo->create('my-index'));

使用本地序列化器和无预配置请求的简单搜索查询示例

此代码是一个可以在控制器动作中执行的示例。

/** @var Client $client */
$client = $this->get('elastification_php_client');

$request = new SearchRequest('my-index', 'my-type', new NativeJsonSerializer());
$response = $client->send($request);
//get the raw deserialized data
var_dump($response->getData()->getGatewayValue());
//for grabbing into the result do: $response->getData()['hits']

标记请求服务并使用请求管理器的示例

以下是一个标记请求作为服务的示例。ID 参数是可选的。如果未设置此参数,则将使用请求服务 ID。如果配置参数 replace_version_of_tagged_requests 设置为 true,则所有注册的请求都将解析并设置为配置的版本。

request.getdocument:
    class: "Elastification\Client\Request\V090x\GetDocumentRequest"g
    arguments: ["my-index", "my-type", @elastification_php_client.serializer.native]
    public: false
    tags:
      - { name: elastification_php_client.request, id: get.service.text }

使用已注册的请求执行请求。此代码是一个可以在控制器动作中执行的示例。

$request = $client->getRequest('get.service.text');
$request->setId('yourDocumentId');
$response = $client->send($request);
var_dump($response->getData()->getGatewayValue());

待办事项

  • 为库创建版本(0.1.0)
  • 为包创建版本
  • [] 实现 thrift 配置
  • 创建 jms 序列化器服务
  • 创建文档存储库服务
  • 创建搜索存储库服务
  • 如果可用,创建 jms 序列化器服务
  • 客户端库:创建 JMS 文档实体
  • 创建文档 JMS 序列化服务
  • [] PHP 客户端启用/禁用(调试)输出?