PHP code example of earc / data-elasticsearch

1. Go to this page and download the library: Download earc/data-elasticsearch 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/ */

    

earc / data-elasticsearch example snippets


use eArc\Data\Initializer;

Initializer::init();

use eArc\DataElasticsearch\ParameterInterface;

$hosts = ['https://user:[email protected]:32775'];

di_set_param(ParameterInterface::CLIENT_HOSTS, $hosts);

use eArc\Data\ParameterInterface;
use eArc\DataElasticsearch\ElasticsearchDataBridge;

di_tag(ParameterInterface::TAG_ON_PERSIST, ElasticsearchDataBridge::class);
di_tag(ParameterInterface::TAG_ON_REMOVE, ElasticsearchDataBridge::class);
di_tag(ParameterInterface::TAG_ON_FIND, ElasticsearchDataBridge::class);

use eArc\DataElasticsearch\IndexService;

di_get(IndexService::class)->rebuildIndex([
    // list of your entity classes
]);

$primaryKeys = data_find(MyUserEntity::class, ['name' => ['Max', 'Moritz'], 'age' => 21]);
$userEntities = data_load_batch(MyUserEntity::class, $primaryKeys);

$userEntities = data_find_entities(MyUserEntity::class, ['name' => ['Max', 'Moritz'], 'age' => 21]);

$allUserPrimaryKeys = data_find(MyUserEntity::class, []);

data_find(MyUserEntity::class, ['age..range' => ['>' => 18]]);

data_find(MyUserEntity::class, ['lastLogin..range' => ['>=' => '2021-01-01', '<=' => '2021-01-31']]);

data_find(MyUserEntity::class, ['city..match' => 'Münster']);

data_find(Price::class, ['currency..text' => 'eur']);

data_find(Price::class, ['currency..exists' => null]);

data_find(Price::class, ['currency..exists_not' => null]);

data_find(MyUserEntity::class, ['_id' => ['1', '2', '392']]);

data_find(MyUserEntity::class, [
    'login.email' => '[email protected]',
    'login.password' => 'l9TFoW5549', 
]);

data_find(MyUserEntity::class, [
    'group._entityName' => Permission::class,
    'group._items' => [
        'name' => ['admin', 'moderator'],
        'active' => true,
    ], 
]);

$colorCategories = data_find(AttributeCategory::class, ['name..match' => ['colour', 'color']]);
$colors = data_find(Attribute::class, [
    'attributeCategory' => $colorCategories,
    'name..match' => ['blue', 'green', 'violet'],
]);

$colors = data_find(Attribute::class, ['name..match' => ['white']]);
$colorCategories = data_find(AttributeCategory::class, ['attributes.items' => $colors]);

data_find(Price::class, [        
    'offerStartDate.raw' => ['bool' => ['must' => [
        ['exists' => ['field' => 'offerStartDate']],
        ['range' => ['offerStartDate' => ['lte' => 'now']]],
    ]]],
]);

data_find(Price::class, [        
    '.raw_body' => [
        'query' => [
            'constant_score' => [
                'filter' => [
                    'bool' => [
                        'must' => [
                            ['exists' => ['field' => 'offerStartDate']],
                            ['range' => ['offerStartDate' => ['lte' => 'now']]],
                        ],
                    ],
                ],
            ],
        ],
    ],
]);

use eArc\DataElasticsearch\ParameterInterface;

di_set_param(ParameterInterface::INDEX_PREFIX, 'my-index-prefix');

use eArc\DataElasticsearch\ParameterInterface;
use eArc\DataElasticsearchTests\Entities\BlacklistedEntity;

di_set_param(ParameterInterface::WHITELIST, [
    // list of entity class names
    BlacklistedEntity::class => true,
    // ...
]);

use eArc\DataElasticsearch\ParameterInterface;

di_set_param(ParameterInterface::BLACKLIST, [
    // list of entity class names
    Attribute::class => true,
    // ...
]);

use eArc\DataElasticsearch\DocumentFactory;

di_decorate(DocumentFactory::class, MyDocumentFactory::class);