haguiry/awscloudsearchbundle

Symfony 2 的 AWS Cloud Search 包

安装: 8

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

类型:symfony-bundle

dev-master 2014-06-30 17:24 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:32:19 UTC


README

注意:此包处于alpha开发阶段,因此不适用于生产环境。我们的目标是完成开发并测试,时间在接下来的两周内。欢迎所有贡献。

此包旨在使将Amazon Cloud Search与Symfony2项目集成变得更加容易,以便无论数据库实现如何,都可以索引实体。

完整的AWS Cloud Search API文档可在以下地址获取:http://docs.aws.amazon.com/cloudsearch/latest/developerguide/SvcIntro.html

  1. 安装

安装PHP Curl模块

    yum install php-curl

添加到composer.json

    "redeyeapps/awscloudsearchbundle" : "dev-master"
  1. 配置

在您的Symfony2项目config.yml中,您需要配置AWS索引。doc_endpoint和search_endpoint可以直接复制用于AWS Cloud Search控制台,记得将协议更改为https以进行ssl加密。

示例配置

aws_cloud_search: 
indexes: 
    index1 :
        doc_endpoint: https://doc-index1.us-west-1.cloudsearch.amazonaws.com
        search_endpoint: https://search-index1.cloudsearch.amazonaws.com
        lang: en
    index2 :
        doc_endpoint: https://doc-index2.us-west-1.cloudsearch.amazonaws.com
        search_endpoint: https://search-index2-test.cloudsearch.amazonaws.com
        lang: en

还要记得设置您的AWS Cloud Search访问规则,以允许从适当的ip进行索引和搜索。

  1. 索引器使用

要索引文档,您需要创建一个JSON数组,该数组匹配您在AWS控制台中配置的AWS Cloud Search字段格式,并将其发布到Cloud Search。

要索引实体的更改(添加/更新/删除),建议您使用事件订阅者,以对doctrine持久化事件进行索引,从而实时索引实体更改。

要创建订阅者,请参阅:https://symfony.com.cn/doc/current/cookbook/doctrine/event_listeners_subscribers.html

完整的示例订阅者请参阅 Resources/doc/ExampleSubscriber.php

要执行实体的初始索引,建议您使用Symfony2命令。完整的示例索引命令请参阅 Resources/doc/ExampleIndexCommand.php

关于将实体转换为JSON的注意事项

有两种方法,一种方法是使用此包:http://jmsyst.com/bundles/JMSSerializerBundle/master/installation

此包的设置相当复杂,并添加了一些额外的依赖项。在我们的情况下,我们在需要索引的实体上创建了一个简单的函数getSearchFields(),该函数将实体手动转换为与索引配置相匹配的对象。然后将其进行json编码,并传递给索引器服务。

示例

    //src/MyOrg/MyBundle/Entity/MyEntity.php
    public function getSearchFields() {
        $obj = new \StdClass;
        $obj->id = $this->getId();
        $number = $this->getNumber();

        //Be careful with null fields
        if($code == null) {
            $number = '';
        }
        $obj->number = $number;

        $title = $this->getTitle();
        if($title == null) {
            $title = '';
        }
        $obj->title = $title;

        //Groups Array
        $groups = array();
        foreach($this->getGroups() as $group) {
            $groups[] = $group->getId();
        }

        if(count($groups) > 0){
            $obj->groups = $groups;
        }

        return $obj;
    }

这匹配具有以下字段的索引

    id : uint
    number : uint
    title : text
    groups : uint
  1. 搜索使用

使用CloudSearchClient服务进行搜索非常简单(唯一的必需设置是setIndex)

    // Create search client
    $cloudsearcher = $this->get('cloudsearchclient');

    //Specify index to search. Must match indexname in config.yml
    $cloudsearcher->setIndex('redeyevms');

    //Set text fields to search with search term
    $cloudsearcher->addSearchField('title');
    $cloudsearcher->addSearchField('desc');

    //Set fields to recieve in results
    $cloudsearcher->addReturnField('title');

    //Set a sort field
    $cloudsearcher->addSort('title', 'ASC');

    //Set offsets and result limit, useful for paging.
    $cloudsearcher->setOffset($offset);
    $cloudsearcher->setLimit($resultlength);

    // Match mode, one of: normal, exact, startswith, endswith, any. Defaults to normal.
    $cloudsearcher->setMatchMode('startswith');

    //Add a filter to search
    $cloudsearcher->addFilter('genre', 'or', array('horror', 'sci-fi')); //Genre can be either horror or sci-fi.

    //Do search with string as search term
    $results = $cloudsearcher->search('star wars');      

结果的格式在此处有文档:http://docs.aws.amazon.com/cloudsearch/latest/developerguide/Search.Response.html