PHP code example of xervice / elasticsearch

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

    

xervice / elasticsearch example snippets


    use Xervice\Elasticsearch\ElasticsearchConfig;

    $config[ElasticsearchConfig::HOST] = '127.0.0.1';
    $config[ElasticsearchConfig::PORT] = 9200;

# Overwrite ElasticSearchDependencyProvider and add your IndexProvider 

    /**
     * @return \Xervice\Elasticsearch\Dependency\Plugin\IndexProviderInterface[]
     */
    protected function getIndexProvider(): array
    {
        return [
            new CustomIndexProvider
        ];
    }
    
    
### Your Index-Provider must implement IndexProviderInterface and define your index and types 
    /**
     * @param \Xervice\Elasticsearch\Business\Model\Index\IndexBuilderInterface $indexBuilder
     */
    public function createIndex(IndexBuilderInterface $indexBuilder, MappingConverterInterface $mappingConverter): void
    {
        $type = (new TypeDataProvider())
            ->setName('customtype')
            ->setMapping(
                $mappingConverter->convertToMapping(ElasticSearchTestDataProvider::class)
            );

        $index = (new IndexDataProvider())
            ->setName('customindex')
            ->setArguments(
                [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 1
                ]
            )
            ->setDelete(false)
            ->addType($type);

        $indexBuilder->createIndex($index);
    }


    $documentList = (new DocumentListDataProvider())
        ->setIndex('testindex')
        ->setType('testtype')
        ->addDocument(
            (new DocumentDataProvider())
                ->setIdent(1)
                ->setContent($customDataProvider->toArray())
        );

    $this->getFacade()->createDocuments($documentList);

    $textQuery = new QueryString('SearchKey');
    $boolQuery = new BoolQuery();
    $boolQuery->addMust($textQuery);

    $query = new Query($boolQuery);

    $result = $this->getFacade()->search('testindex', $query, [], []);

$result = $this->getFacade()->search(
    'testindex', 
    $query, 
    [
        new CustomSearchExtenderPlugin()
    ], 
    [
        new CustomResultFormatterPlugin()
    ]
);