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(),
// ...
);
}