Giter VIP home page Giter VIP logo

awscloudsearchbundle's Introduction

AWSCloudSearchBundle

Note: This bundle is in alpha development stage and is as such not for production environments. Our aim is to complete development and testing in the next two weeks. All contributions welcome.

This bundle is designed to make it easier to integrate Amazon Cloud Search with Symfony2 projects to index entities regardless of database implementation.

Full AWS Cloud Search API Doco available at: http://docs.aws.amazon.com/cloudsearch/latest/developerguide/SvcIntro.html

  1. Installation

Install PHP Curl module:

    yum install php-curl

Add to composer.json:

    "redeyeapps/awscloudsearchbundle" : "dev-master"
  1. Configuration

In your Symfony2 projects config.yml you needs to configure you AWS indexes. doc_endpoint and search_endpoint can be copied directly for AWS Cloud Search console, remember to change protocol to https for ssl encryption.

Example Configuration:

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

Also remember to setup your AWS Cloud Search access rules to allow indexing and searching from appropriate ip's.

  1. Indexer Usage

To index documents you need to create a JSON array of documents that match the AWS Cloud Search fields format you configured in the AWS console and post it to Cloud Search.

To index changes to entities (adds/updates/removes) it is recommended you us an event subscriber to doctrine persist events to index entity changes on the fly.

To create a subscriber see: http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

For full example subscriber see Resources/doc/ExampleSubscriber.php

To do initial indexing of entities it is recommended you use a Symfony2 command. For full example indexing command see Resources/doc/ExampleIndexCommand.php

Notes on Converting Entity to Json

There are a couple of approaches, one is to use this bundle: http://jmsyst.com/bundles/JMSSerializerBundle/master/installation

This bundle is pretty complex to setup and adds a few extra depenacies. In our case we have created a simple function on the entities we need to index called getSearchFields() which manually converts the entity to an object that that matches the fields configured for our indexes. This is then json encoded and passed to the indexer service.

Example:

    //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;
    }

This matches an index with the fields:

    id : uint
    number : uint
    title : text
    groups : uint
  1. Search Usage

Searching using the CloudSearchClient service is pretty straight forward (the only required setting is 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');      

Format of results is documented here: http://docs.aws.amazon.com/cloudsearch/latest/developerguide/Search.Response.html

awscloudsearchbundle's People

Contributors

haguiry avatar

Watchers

shuogawa avatar  avatar

Forkers

thangnvdigdinos

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.