<commit_before><?php

namespace FOS\ElasticaBundle\Persister;

use Elastica\Exception\NotFoundException;
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
use Elastica\Type;
use Elastica\Document;

/**
 * Inserts, replaces and deletes single documents in an elastica type
 * Accepts domain model objects and converts them to elastica documents
 *
 * @author <NAME> <<EMAIL>>
 */
class ObjectPersister implements ObjectPersisterInterface
{
    protected $type;
    protected $transformer;
    protected $objectClass;
    protected $fields;

    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
    {
        $this->type            = $type;
        $this->transformer     = $transformer;
        $this->objectClass     = $objectClass;
        $this->fields          = $fields;
    }

    /**
     * Insert one object into the type
     * The object will be transformed to an elastica document
     *
     * @param object $object
     */
    public function insertOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        $this->type->addDocument($document);
    }

    /**
     * Replaces one object in the type
     *
     * @param object $object
     * @return null
     **/
    public function replaceOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        try {
            $this->type->deleteById($document->getId());
        } catch (NotFoundException $e) {}
        $this->type->addDocument($document);
    }

    /**
     * Deletes one object in the type
     *
     * @param object $object
     * @return null
     **/
    public function deleteOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        try {
            $this->type->deleteById($document->getId());
        } catch (NotFoundException $e) {}
    }

    /**
     * Deletes one object in the type by id
     *
     * @param mixed $id
     *
     * @return null
     **/
    public function deleteById($id)
    {
        try {
            $this->type->deleteById($id);
        } catch (NotFoundException $e) {}
    }

    /**
     * Bulk insert an array of objects in the type for the given method
     *
     * @param array $objects array of domain model objects
     * @param string Method to call
     */
    public function insertMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $documents[] = $this->transformToElasticaDocument($object);
        }
        $this->type->addDocuments($documents);
    }

    /**
     * Bulk updates an array of objects in the type
     *
     * @param array $objects array of domain model objects
     */
    public function replaceMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $document = $this->transformToElasticaDocument($object);

            try {
                $this->type->getDocument($document->getId());
            } catch (NotFoundException $e) {
                $this->type->addDocument($document);
            }

            $documents[] = $document;
        }
        $this->type->updateDocuments($documents);
    }

    /**
     * Bulk deletes an array of objects in the type
     *
     * @param array $objects array of domain model objects
     */
    public function deleteMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $documents[] = $this->transformToElasticaDocument($object);
        }<commit_msg>Bulk upsert<commit_after><?php

namespace FOS\ElasticaBundle\Persister;

use Elastica\Exception\NotFoundException;
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
use Elastica\Type;
use Elastica\Document;

/**
 * Inserts, replaces and deletes single documents in an elastica type
 * Accepts domain model objects and converts them to elastica documents
 *
 * @author <NAME> <<EMAIL>>
 */
class ObjectPersister implements ObjectPersisterInterface
{
    protected $type;
    protected $transformer;
    protected $objectClass;
    protected $fields;

    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
    {
        $this->type            = $type;
        $this->transformer     = $transformer;
        $this->objectClass     = $objectClass;
        $this->fields          = $fields;
    }

    /**
     * Insert one object into the type
     * The object will be transformed to an elastica document
     *
     * @param object $object
     */
    public function insertOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        $this->type->addDocument($document);
    }

    /**
     * Replaces one object in the type
     *
     * @param object $object
     * @return null
     **/
    public function replaceOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        try {
            $this->type->deleteById($document->getId());
        } catch (NotFoundException $e) {}
        $this->type->addDocument($document);
    }

    /**
     * Deletes one object in the type
     *
     * @param object $object
     * @return null
     **/
    public function deleteOne($object)
    {
        $document = $this->transformToElasticaDocument($object);
        try {
            $this->type->deleteById($document->getId());
        } catch (NotFoundException $e) {}
    }

    /**
     * Deletes one object in the type by id
     *
     * @param mixed $id
     *
     * @return null
     **/
    public function deleteById($id)
    {
        try {
            $this->type->deleteById($id);
        } catch (NotFoundException $e) {}
    }

    /**
     * Bulk insert an array of objects in the type for the given method
     *
     * @param array $objects array of domain model objects
     * @param string Method to call
     */
    public function insertMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $documents[] = $this->transformToElasticaDocument($object);
        }
        $this->type->addDocuments($documents);
    }

    /**
     * Bulk update an array of objects in the type.  Create document if it does not already exist.
     *
     * @param array $objects array of domain model objects
     */
    public function replaceMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $document = $this->transformToElasticaDocument($object);
            $document->setDocAsUpsert(true);
            $documents[] = $document;
        }
        $this->type->updateDocuments($documents);
    }

    /**
     * Bulk deletes an array of objects in the type
     *
     * @param array $objects array of domain model objects
     */
    public function deleteMany(array $objects)
    {
        $documents = array();
        foreach ($objects as $object) {
            $documents[] = $this->transformToElasticaDocument($object);
        }
{'fraction_non_alphanumeric': 0.11371942648299128, 'fraction_numerical': 0.0, 'mean_word_length': 2.26076993583868, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2108002', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1924, 'n_words': 518}