PHP code example of glue-agency / craft-elasticsearch

1. Go to this page and download the library: Download glue-agency/craft-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/ */

    

glue-agency / craft-elasticsearch example snippets




return [
    /**
     * Connection settings
     */
    'endpoint'  => 'https://your-endpoint.cloud.es.io',
    'port'      => '443',

    /** 
     * Authentication
     * 
     * Supported auth keys: "basic", "cloudId", "apiKey"
     * 
     * Each auth key ge indexing and linked indexes.
     * At least sections or entryTypes must be set.
     * 
     * Available settings per index:
     *   sections   - Limit to specific section handles (optional)
     *   entryTypes - Limit to specific entry type handles (optional)
     *   statuses   - Limit to specific entry statuses (optional, defaults to ['live', 'pending'])
     *   fields     - Limit which fields are indexed, supports wildcards and nested relations (optional, defaults to all fields)
     */
    'indexes' => [
        [
            'name'       => 'index-name',
            'element'    => \craft\elements\Entry::class,
            'site'       => 'site-handle',
            'settings' => [
                'sections'   => ['section-handle-1', 'section-handle-2'],
                'entryTypes' => ['entry-type-handle-1', 'entry-type-handle-2'],
                'statuses'   => ['live', 'pending'], // default, can be omitted
            ],
        ],
        [
            'name'       => 'index-name',
            'element'    => \craft\elements\Entry::class,
            'site'       => 'site-handle',
            'settings' => [
                'sections'   => ['section-handle-3'],
                'entryTypes' => ['entry-type-handle-3'],
                'statuses'   => ['live'], // only index live entries for this index
            ],
        ],
    ],
];

use craft\elements\Category;
use GlueAgency\Elasticsearch\factories\ElementIndexCriteriaFactory;
use GlueAgency\Elasticsearch\factories\ElementMappingFactory;
use GlueAgency\Elasticsearch\factories\ElementQueryFilterFactory;
use GlueAgency\Elasticsearch\factories\ElementSchemaFactory;
use yii\base\Event;

// Register Criteria
Event::on(
    ElementIndexCriteriaFactory::class, 
    ElementIndexCriteriaFactory::EVENT_REGISTER_CRITERIA_BUILDERS, 
    function($event) {
        $event->builders[Category::class] = MyCategoryCriteria::class;
    }
);

// Register Query Filter
Event::on(
    ElementQueryFilterFactory::class, 
    ElementQueryFilterFactory::EVENT_REGISTER_FILTERS, 
    function($event) {
        $event->filters[Category::class] = MyCategoryFilter::class;
    }
);

// Register Schema
Event::on(
    ElementSchemaFactory::class, 
    ElementSchemaFactory::EVENT_REGISTER_SCHEMAS, 
    function($event) {
        $event->schemas[Category::class] = MyCategorySchema::class;
    }
);

// Register Mapper
Event::on(
    ElementMappingFactory::class, 
    ElementMappingFactory::EVENT_REGISTER_ELEMENT_MAPPERS, 
    function($event) {
        $event->mappers[Category::class] = MyCategoryMapper::class;
    }
);

use GlueAgency\Elasticsearch\factories\FieldMappingFactory;
use GlueAgency\Elasticsearch\factories\PropertyMappingFactory;
use yii\base\Event;

// Register the Schema Property Mapper
Event::on(
    PropertyMappingFactory::class, 
    PropertyMappingFactory::EVENT_REGISTER_MAPPERS, 
    function($event) {
        $event->mappings[MyCustomField::class] = MyCustomPropertyMapper::class;
    }
);

// Register the Document Field Mapper
Event::on(
    FieldMappingFactory::class, 
    FieldMappingFactory::EVENT_REGISTER_MAPPERS, 
    function($event) {
        $event->mappings[MyCustomField::class] = MyCustomFieldMapper::class;
    }
);