<commit_before><?php

namespace Elastica\Test;

use Elastica\Client;
use Elastica\Connection;
use Elastica\Document;
use Elastica\Exception\ClientException;
use Elastica\Script;
use Elastica\Index;
use Elastica\Request;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;

class ClientTest extends BaseTest
{

    public function testConstruct()
    {
        $client = $this->_getClient();
        $this->assertCount(1, $client->getConnections());
    }

    public function testConnectionsArray()
    {
        // Creates a new index 'xodoa' and a type 'user' inside this index
        $client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200))));
        $index = $client->getIndex('elastica_test1');
        $index->create(array(), true);

        $type = $index->getType('user');

        // Adds 1 document to the index
        $doc1 = new Document(1,
            array('username' => 'hans', 'test' => array('2', '3', '5'))
        );
        $type->addDocument($doc1);

        // Adds a list of documents with _bulk upload to the index
        $docs = array();
        $docs[] = new Document(2,
            array('username' => 'john', 'test' => array('1', '3', '6'))
        );
        $docs[] = new Document(3,
            array('username' => 'rolf', 'test' => array('2', '3', '7'))
        );
        $type->addDocuments($docs);

        // Refresh index
        $index->refresh();

        $resultSet = $type->search('rolf');
    }

    public function testTwoServersSame()
    {
        // Creates a new index 'xodoa' and a type 'user' inside this index
        $client = new Client(array('connections' => array(
            array('host' => 'localhost', 'port' => 9200),
            array('host' => 'localhost', 'port' => 9200),
        )));
        $index = $client->getIndex('elastica_test1');
        $index->create(array(), true);

        $type = $index->getType('user');

        // Adds 1 document to the index
        $doc1 = new Document(1,
        array('username' => 'hans', 'test' => array('2', '3', '5'))
        );
        $type->addDocument($doc1);

        // Adds a list of documents with _bulk upload to the index
        $docs = array();
        $docs[] = new Document(2,
        array('username' => 'john', 'test' => array('1', '3', '6'))
        );
        $docs[] = new Document(3,
        array('username' => 'rolf', 'test' => array('2', '3', '7'))
        );
        $type->addDocuments($docs);

        // Refresh index
        $index->refresh();

        $resultSet = $type->search('rolf');
    }

    public function testConnectionParamsArePreparedForConnectionsOption()
    {
        $client = new Client(array('connections' => array(array('url' => 'https://localhost:9200'))));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testConnectionParamsArePreparedForServersOption()
    {
        $client = new Client(array('servers' => array(array('url' => 'https://localhost:9200'))));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testConnectionParamsArePreparedForDefaultOptions()
    {
        $client = new Client(array('url' => 'https://localhost:9200'));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testBulk()
    {
        $client = $this->_getClient();

        $params = array(
            array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')),
            array('user' => array('name' => 'hans')),
            array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '2')),
            array('user' => array('name' => 'peter')),
        );

        $client->bulk($params);
    }

    public function testOptimizeAll()
    {
        $client = $this->_getClient();
        $response = $client->optimizeAll();

        $this->assertFalse($response->hasError());
    }

    /**
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testAddDocumentsEmpty()
    {
        $client = $this->_getClient();
        $client->addDocuments(array());
    }

    public function testUpdateDocuments() {
        $indexName = 'test';
        $typeName = 'people';

        $client = $this->_getClient();
        $type = $client->getIndex($indexName)->getType($typeName);

        $initialValue = 28;
        $modifiedValue = 27;

        $doc1 = new Document(
            1,
            array('username' => 'hans', 'age' => $initialValue),
            $typeName,
            $indexName
        );
        $doc2 = new Document(
            2,
            array('username' => 'anna', 'age' => $initialValue),
            $typeName,
            $indexName
        );
        $data = array($doc1, $doc2);
        $client->addDocuments($data);

        foreach ($data as $i => $doc) {<commit_msg>tests added for bulk update operation<commit_after><?php

namespace Elastica\Test;

use Elastica\Client;
use Elastica\Connection;
use Elastica\Document;
use Elastica\Exception\ClientException;
use Elastica\Script;
use Elastica\Index;
use Elastica\Request;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;

class ClientTest extends BaseTest
{

    public function testConstruct()
    {
        $client = $this->_getClient();
        $this->assertCount(1, $client->getConnections());
    }

    public function testConnectionsArray()
    {
        // Creates a new index 'xodoa' and a type 'user' inside this index
        $client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200))));
        $index = $client->getIndex('elastica_test1');
        $index->create(array(), true);

        $type = $index->getType('user');

        // Adds 1 document to the index
        $doc1 = new Document(1,
            array('username' => 'hans', 'test' => array('2', '3', '5'))
        );
        $type->addDocument($doc1);

        // Adds a list of documents with _bulk upload to the index
        $docs = array();
        $docs[] = new Document(2,
            array('username' => 'john', 'test' => array('1', '3', '6'))
        );
        $docs[] = new Document(3,
            array('username' => 'rolf', 'test' => array('2', '3', '7'))
        );
        $type->addDocuments($docs);

        // Refresh index
        $index->refresh();

        $resultSet = $type->search('rolf');
    }

    public function testTwoServersSame()
    {
        // Creates a new index 'xodoa' and a type 'user' inside this index
        $client = new Client(array('connections' => array(
            array('host' => 'localhost', 'port' => 9200),
            array('host' => 'localhost', 'port' => 9200),
        )));
        $index = $client->getIndex('elastica_test1');
        $index->create(array(), true);

        $type = $index->getType('user');

        // Adds 1 document to the index
        $doc1 = new Document(1,
        array('username' => 'hans', 'test' => array('2', '3', '5'))
        );
        $type->addDocument($doc1);

        // Adds a list of documents with _bulk upload to the index
        $docs = array();
        $docs[] = new Document(2,
        array('username' => 'john', 'test' => array('1', '3', '6'))
        );
        $docs[] = new Document(3,
        array('username' => 'rolf', 'test' => array('2', '3', '7'))
        );
        $type->addDocuments($docs);

        // Refresh index
        $index->refresh();

        $resultSet = $type->search('rolf');
    }

    public function testConnectionParamsArePreparedForConnectionsOption()
    {
        $client = new Client(array('connections' => array(array('url' => 'https://localhost:9200'))));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testConnectionParamsArePreparedForServersOption()
    {
        $client = new Client(array('servers' => array(array('url' => 'https://localhost:9200'))));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testConnectionParamsArePreparedForDefaultOptions()
    {
        $client = new Client(array('url' => 'https://localhost:9200'));
        $connection = $client->getConnection();

        $this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
    }

    public function testBulk()
    {
        $client = $this->_getClient();

        $params = array(
            array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')),
            array('user' => array('name' => 'hans')),
            array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '2')),
            array('user' => array('name' => 'peter')),
        );

        $client->bulk($params);
    }

    public function testOptimizeAll()
    {
        $client = $this->_getClient();
        $response = $client->optimizeAll();

        $this->assertFalse($response->hasError());
    }

    /**
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testAddDocumentsEmpty()
    {
        $client = $this->_getClient();
        $client->addDocuments(array());
    }

    public function testUpdateDocuments() {
        $indexName = 'test';
        $typeName = 'people';

        $client = $this->_getClient();
        $type = $client->getIndex($indexName)->getType($typeName);

        $initialValue = 28;
        $modifiedValue = 27;

        $doc1 = new Document(
            1,
            array('name' => 'hans', 'age' => $initialValue),
            $typeName,
            $indexName
        );
        $doc2 = new Document(
            2,
            array('name' => 'anna', 'age' => $initialValue),
            $typeName,
            $indexName
        );
        $data = array($doc1, $doc2);
        $client->addDocuments($data);

        foreach ($data as $i => $doc) {
{'fraction_non_alphanumeric': 0.17858546168958742, 'fraction_numerical': 0.015913555992141455, 'mean_word_length': 2.4095780308104486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 163, 'https://': 12, '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': '5884685', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2959, 'n_words': 653}