PHP code example of headoo / elasticsearch-bundle

1. Go to this page and download the library: Download headoo/elasticsearch-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

headoo / elasticsearch-bundle example snippets




namespace Name\NameBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class YourEntityClassName
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * Set id
     *
     * @param integer $id
     * @return Id
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }


    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;



    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return City
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}


    use Elastica\Query;
    use Elastica\Search;

    $elasticClient           = $this->container->get('headoo.elasticsearch.helper')->getClient('localhost');
    $search                  = new Search($elasticClient);
    //We will search in our index defined in mapping
    $search->addIndex('indexname');
    $query                   = new Query();
    $boolQuery               = new Query\BoolQuery();
    $queryMatch              = new Query\Match();
    $queryMatch->setFieldQuery('name', 'whatever');

    $boolQuery->addMust($queryMatch);
    $query->setQuery($boolQuery);
    $resultSet = $search->search($query);



namespace Name\NameBundle\EventListener;

use Headoo\ElasticSearchBundle\Event\ElasticSearchEvent;

class ElasticSearchListener
{
    /**
     * @param ElasticSearchEvent $event
     */
    public function onElasticEntityAction(ElasticSearchEvent $event)
    {
        //Action can be persist, update and delete
        $action = $event->getAction();
        //Your Doctrine Entity
        $entity = $event->getEntity();
    }
}
 php
public function registerBundles()
{
    return array(
        // ...
        new Headoo\ElasticSearchBundle\HeadooElasticSearchBundle(),
        // ...
    );
}
 php


/**********************************************************************
 ************************************CONNECTIONS***********************
 **********************************************************************
 */

$connections =
    array('localhost'=>
        array(
            'host'    => 'localhost',
            'port'    => '9200',
        )
);


$container->setParameter('elastica_connections', $connections);

/**********************************************************************
 ************************************INDEX*****************************
 **********************************************************************
 */
$elasticaIndex = array(
    'number_of_shards' => 1,
    'number_of_replicas' => 1,
    'analysis' => array(
        'analyzer' => array(
            'default' => array(
                'type' => 'custom',
                'tokenizer' => 'standard',
                'filter' => array('lowercase'')
            ),
            'default_search' => array(
                'type' => 'custom',
                'tokenizer' => 'standard',
                'filter' => array('standard', 'lowercase')
            ),
        ),
    )
);


$mapping['YourEntityClassName']['mapping'] = array(
    'id'                => array('type' => 'integer', '
 php

namespace Name\NameBundle\Entity\Transformer\EntityTransformer;
use Elastica\Document;
use Name\NameBundle\Entity\YourEntityClassName;

class YourEntityClassName
{
    /**
     * @param YourEntityClassName $yourEntityClassName
     * @return Document
     */
    public function transform(YourEntityClassName $yourEntityClassName)
    {
        $identifier             = $yourEntityClassName->getId();
        $values = array(
            'name'              => $yourEntityClassName->getName(),
            'date'              => (new \DateTime('now'))->format('c'),
        );

        //Create a document to index
        $document = new Document($identifier, $values);
        return $document;
    }
}