PHP code example of divante-ltd / pimcore-elasticsearch-plugin

1. Go to this page and download the library: Download divante-ltd/pimcore-elasticsearch-plugin 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/ */

    

divante-ltd / pimcore-elasticsearch-plugin example snippets


$container = \Pimcore::getDiContainer();
/** @var \DivanteLtd\PimcoreElasticsearchPlugin\Service\ConfigService $configService */
$configService = $container->get('DivanteLtd\PimcoreElasticsearchPlugin\Service\ConfigService');

$configService->setHosts(["127.0.0.1:9200"]);

$indices = [
    "exampleIndex" => [
        "mappings" => [
            "newsType" => [
                "properties" => [
                    "title" => [
                        "type" => "string",
                        "analyzer" => "standard"
                    ]
                ]
            ]
        ],
        //...
    ]
];
foreach ($indices as $indexName => $body) {
    $configService->addIndex($indexName, $body);
}


php pimcore/cli/console.php divante-ltd:elasticsearch:create-index exampleIndex

php pimcore/cli/console.php divante-ltd:elasticsearch:remove-index exampleIndex


use DivanteLtd\PimcoreElasticsearchPlugin\Indexer\AbstractIndexer;

class ExampleIndexer extends AbstractIndexer
{

    /**
     * @param AbstractElement $element
     *
     * @return bool
     */
    public function isIndexable(AbstractElement $element): bool
    {
        return $element instanceof News;
    }

    /**
     * @param AbstractElement|News $element
     *
     * @return array
     */
    public function buildDocument(AbstractElement $element): array
    {
        return [
            'title' => $element->getTitle(),
        ];
    }

    /**
     * @return string
     */
    public function getIndexName(): string
    {
        return 'exampleIndex';
    }

    /**
     * @return string
     */
    public function getType(): string
    {
        return 'newsType';
    }
}

/** @var \DivanteLtd\PimcoreElasticsearchPlugin\Indexer\Service\IndexerRegisterService $indexerRegister */
$indexerRegister = \Pimcore::getDiContainer()->get(
    'DivanteLtd\PimcoreElasticsearchPlugin\Indexer\Service\IndexerRegisterService'
);
$indexerRegister->add(new ExampleIndexer());

php pimcore/cli/console.php divante-ltd:elasticsearch:reindex-all

php pimcore/cli/console.php divante-ltd:elasticsearch:reindex-all --objects