2017 © Pedro Peláez
 

library doctrine-elastic

Elasticsearch Doctrine Adaptation

image

andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  • Saturday, February 3, 2018
  • by andsalves
  • Repository
  • 6 Watchers
  • 13 Stars
  • 3,193 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 2 Open issues
  • 46 Versions
  • 11 % Grown

The README.md

Doctrine-Elastic

Custom Doctrine Library for Elasticsearch., (*1)

Build Status Coverage Status Scrutinizer Code Quality, (*2)

Last stable release: v1.3.1 (Elasticsearch 2.x or 5.x support), (*3)

Tests on Elasticsearch 2.4.1/5.1/5.5 - PHP 5.6/7.0, (*4)

This library is not actively maintained anymore, (*5)

Get Started

Creating a working ElasticEntityManager

Please see https://github.com/andsalves/doctrine-elastic/blob/master/docs/creating-an-elastic-entity-manager-instance.md, (*6)

Creating a working DoctrineElastic Entity

Just like Doctrine, we need to set some annotations in our entities, here's an example:, (*7)

<?php
namespace DoctrineElastic\Entity;

use Doctrine\ORM\Mapping as ORM;
use DoctrineElastic\Mapping as ElasticORM;

/**
 * @author Ands
 *
 * @ElasticORM\Type(name="foo_type", index="foo_index")
 * @ORM\Entity
 */
class FooType {

    /**
     * @var string
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ElasticORM\MetaField(name="_id")
     * @ORM\Column(name="_id", type="integer")
     */
    public $_id;

    /**
     * @var int
     *
     * @ElasticORM\Field(name="custom_numeric_field", type="integer")
     */
    private $customNumericField;

    /**
     * @var string
     * 
     * Below: Use 'string' for elasticsearch 2.x and 'keyword' for elasticsearch 5.x+
     * @ElasticORM\Field(name="custom_field", type="string")
     */
    private $customField;

    /**
     * @var array
     *
     * @ElasticORM\Field(name="custom_nested_field", type="nested")
     */
    private $customNestedField = [];

    /**
     * @return int
     */
    public function getCustomNumericField() {
        return $this->customNumericField;
    }

    /**
     * @param int $customNumericField
     * @return FooType
     */
    public function setCustomNumericField($customNumericField) {
        $this->customNumericField = $customNumericField;
        return $this;
    }

    /**
     * @return string
     */
    public function getCustomField() {
        return $this->customField;
    }

    /**
     * @param string $customField
     * @return FooType
     */
    public function setCustomField($customField) {
        $this->customField = $customField;
        return $this;
    }

    /**
     * @return array
     */
    public function getCustomNestedField() {
        return $this->customNestedField;
    }

    /**
     * @param array $customNestedField
     * @return FooType
     */
    public function setCustomNestedField($customNestedField) {
        $this->customNestedField = $customNestedField;
        return $this;
    }
}

This entity represents, in Elasticsearch, a type named 'foo_type', that belongs to an index named 'foo_index'. Note the class annotation @ElasticORM\Type with these definitions. The property annotation @ElasticORM\Field represents a _source field of a document inside the 'foo_type' type. The @ElasticORM\MetaField annotation represents a metafield, like _id. @ElasticORM\MetaField _id is required for an entity, and must be a public property., (*8)

Only properties with @ElasticORM\Field annotation will be considered document fields. In elasticsearch, the document column name will be the 'name' property from @ElasticORM\Field annotation from the class property, just like the 'type' annotation property., (*9)

Inserting Documents

With this library, making CRUD operations through ElasticEntityManager is really simple. Assuming you have an ElasticEntityManager instance in a variable called $elasticEntityManager:, (*10)

$newFoo = new DoctrineElastic\Entity\FooType(); // Or wherever be your entity
$newFoo->setCustomNumericField(1234);
$newFoo->setCustomField('Test Value');
$newFoo->setCustomNestedField(['some_value' => 'Some Value', 'whatever' => 'Whatever']);

$elasticEntityManager->persist($newFoo); // Persisting entity...
$elasticEntityManager->flush(); // And flushing... Oh God, just like Doctrine!
Note 1:

Index and type will be created automatically, as well as their mappings, if don't exist yet., (*11)

Note 2:

By default, mappings for analyzable fields will be not_analyzed (index='not_analyzed'). DoctrineElastic was made to work this way. However, you can change it with 'index' @ElasticORM\Field annotation property, if you prefer default analized fields. e.g. @ElasticORM\Field(name='mad_field', type='string', index='analyzed'). Attention: Search documents with ElasticEntityManager is not guaranteed when you do that, once it isn't always possible to match exact values., (*12)

Note 3:

DoctrineElastic does not accept TRANSACTIONS (yet). You will find an available 'beginTransaction' method in ElasticEntityManager, but it does nothing. It is there because ElasticEntityManager implements EntityManagerInterface from Doctrine. That happens with some few other methods., (*13)

Note 4:

Just like in Doctrine, after flushing, the entity will have the _id field filled. If you persist an entity with _id field non null, DoctrineElastic will search a doc for update, if it doesn't exist, it's created with the provided _id., (*14)

Finding Documents

If you know Doctrine, this is very easy and intuitive:, (*15)

$myFoo = $elasticEntityManager->getRepository(DoctrineElastic\Entity\FooType::class)->findOneBy(['customNumericField' => 1234]);

if (!is_null($myFoo)) {
    print 'Yes, I found it!';
} else {
    print 'Nothing here';
}
Note 1:

You can use findBy and findAll methods too., (*16)

Note 2:

It doesn't matter if index and type exist or not in your Elasticsearch. If not exist, no documents are returned, also no exception is thrown., (*17)

Note 3:

To search by _id, use $elasticEntityManager::getReference or $elasticEntityManager::find (they are equivalent in ElasticEntityManager)., (*18)

Removing Documents

$myFoo = $elasticEntityManager->getRepository(DoctrineElastic\Entity\FooType::class)->findOneBy(['customNumericField' => 1234]);

if (!is_null($myFoo)) {
    // Let's delete this one
    $elasticEntityManager->remove($myFoo);
    $elasticEntityManager->flush(); // Deleted :)
} else {
    print 'Nothing to remove';
}

Using Query Builder

Please see the tests for this feature as a good example: https://github.com/andsalves/doctrine-elastic/blob/master/tests/DoctrineElastic/Tests/ElasticEntityManagerTest.php, (*19)

Parent-Child Relationship

Please see the tests for this feature as a good example: https://github.com/andsalves/doctrine-elastic/blob/master/tests/DoctrineElastic/Tests/ParentChildTest.php, (*20)

Application-side Relationships with DoctrineElastic

You can simulate relational databases relationships, with loss of performance, obviously. DoctrineElastic has this feature as an internal feature development, but it is not recommended to be used - if you need complex relationships, you should use a relational database. If you'd really like to use relationships like that with this library, contact me for help., (*21)

# #, (*22)

For questions, please contact me at ands.alves.nunes@gmail.com.
Please feel free to open issues or make pull requests.

# # # #, (*23)

The Versions

03/02 2018

dev-master

9999999-dev https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

03/02 2018

v1.3.4

1.3.4.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

09/11 2017

v1.3.3

1.3.3.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

09/11 2017

v1.3.2

1.3.2.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

07/11 2017

v1.3.1

1.3.1.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

20/10 2017

v1.3.0

1.3.0.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

19/10 2017

v1.2.6

1.2.6.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

11/09 2017

v1.2.5

1.2.5.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

07/08 2017

v1.2.3

1.2.3.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

24/06 2017

v1.2.2

1.2.2.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

24/06 2017

dev-iuribrindeiro-patch-6

dev-iuribrindeiro-patch-6 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

21/06 2017

dev-ref/verifing-query-key

dev-ref/verifing-query-key https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

17/06 2017

dev-iuribrindeiro-patch-5

dev-iuribrindeiro-patch-5 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

11/06 2017

v1.2.1beta

1.2.1.0-beta https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

06/06 2017

1.2-beta3

1.2.0.0-beta3 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

31/05 2017

v0.7.x-dev

0.7.9999999.9999999-dev https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

31/05 2017

v0.7.2

0.7.2.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

29/05 2017

1.2-beta2

1.2.0.0-beta2 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

29/05 2017

1.2.x-dev

1.2.9999999.9999999-dev https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

29/05 2017

1.2-beta

1.2.0.0-beta https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

29/05 2017

1.0.x-dev

1.0.9999999.9999999-dev https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

20/04 2017

dev-cleaning-index-string

dev-cleaning-index-string https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Doctrine Adaptation

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database library search elasticsearch doctrine entity engine elastic best adaptation

13/04 2017

v0.7.1

0.7.1.0 https://packagist.org/packages/andsalves/doctrine-elastic

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

MIT

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

11/04 2017

v0.7

0.7.0.0 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

11/04 2017

dev-dev-documentations

dev-dev-documentations http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

10/04 2017

dev-parent_child

dev-parent_child http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

03/04 2017

v0.6-beta4

0.6.0.0-beta4 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

03/04 2017

v0.6-beta3

0.6.0.0-beta3 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

14/02 2017

dev-iuribrindeiro-patch-4

dev-iuribrindeiro-patch-4 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

13/02 2017

v0.6-beta2

0.6.0.0-beta2 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

10/02 2017

dev-iuribrindeiro-patch-3

dev-iuribrindeiro-patch-3 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

10/02 2017

v0.6-beta1

0.6.0.0-beta1 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

10/02 2017

dev-andsalves-patch-1

dev-andsalves-patch-1 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

07/02 2017

dev-iuribrindeiro-patch-2

dev-iuribrindeiro-patch-2 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

01/02 2017

v0.5-beta5

0.5.0.0-beta5 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

01/02 2017

dev-iuribrindeiro-patch-1

dev-iuribrindeiro-patch-1 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

28/01 2017

v0.5-beta4

0.5.0.0-beta4 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

27/01 2017

v0.5-beta3

0.5.0.0-beta3 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

11/01 2017

v0.5-beta2

0.5.0.0-beta2 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

16/12 2016

dev-develop

dev-develop http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

16/12 2016

v0.5beta

0.5.0.0-beta http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

14/12 2016

dev-relationshipQuery

dev-relationshipQuery http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

08/12 2016

dev-innerJoin

dev-innerJoin http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

02/12 2016

v0.1-beta3

0.1.0.0-beta3 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

04/11 2016

v0.1-beta2

0.1.0.0-beta2 http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic

04/11 2016

v0.1-beta

0.1.0.0-beta http://www.doctrine-project.org

Elasticsearch Adaptation for Doctrine Object-Relational-Mapper for PHP

  Sources   Download

The Requires

 

by Anderson Alves Nunes de Freitas

orm database search elasticsearch doctrine elastic