Git Product home page Git Product logo

pimcore-elasticsearch-plugin's Introduction

PimcoreElasticsearchPlugin

Requirements

  • PHP 7 or newer
  • Pimcore 4
  • Elasticsearch 5
  • Composer

Installation

First step

via Composer

composer require divante-ltd/pimcore-elasticsearch-plugin

manually

  • Download this repository into your plugins directory.
  • Download manually dependencies (see composer.json).
  • Follow next steps in this instruction.

Second step

Open Extension tab in admin panel and install plugin. After this, installation is finished.

Usage

How to use config service?

$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);
}

INFO: remember config service must be configured before you call elasticsearch service, e.g.: in plugin init method.

Creating index

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

It will create index in elasticsearch.

INFO: it allows only to create indices which where added to config service.

Removing index

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

It will remove index from elasticsearch.

Indexers


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';
    }
}

You need also to register this indexer, so it can work with reindex all command and event listeners like object.postAdd, etc.

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

From now everytime you add, edit or delete object which is instance of News class, it will be added, saved or deleted from elastic.

How to reindex everything?

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

If you want to reindex only e.g. objects:

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

How to search?

use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\FullText\MatchQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TypeQuery;
use ONGR\ElasticsearchDSL\Search;
use DivanteLtd\PimcoreElasticsearchPlugin\Service\ElasticSearchService;


/** @var ElasticSearchService $elasticSearchService */
$elasticSearchService = \Pimcore::getDiContainer()->get(
    'DivanteLtd\PimcoreElasticsearchPlugin\Service\ElasticSearchService'
);
$boolQuery = new BoolQuery();
$boolQuery->addParameter("minimum_should_match", 1);
$boolQuery->addParameter("boost", 1);
$boolQuery->add(new TypeQuery('newsType'), BoolQuery::MUST);
$boolQuery->add(new MatchQuery('title', $query), BoolQuery::SHOULD);
$search = new Search();
$search
    ->addQuery($boolQuery)
    ->setFrom(0)
    ->setSize(10);
//reults should be array of News objects
$results = $elasticSearchService->find('newsType', $search);

For more examples take a look here: http://docs.ongr.io/ElasticsearchDSL

About Authors

Divante-logo

We are a Software House from Europe, headquartered in Poland and employing about 150 people. Our core competencies are built around Magento, Pimcore and bespoke software projects (we love Symfony3, Node.js, Angular, React, Vue.js). We specialize in sophisticated integration projects trying to connect hardcore IT with good product design and UX.

Visit our website Divante.co for more information.

pimcore-elasticsearch-plugin's People

Contributors

kkarkus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

buonvnxk

pimcore-elasticsearch-plugin's Issues

Pimcore 5

Are there currently plans on making a Bundle for Pimcore 5?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.