xervice/elasticsearch

1.2.2 2019-04-15 21:13 UTC

This package is auto-updated.

Last update: 2024-04-16 08:46:50 UTC


README

Scrutinizer Code Quality Code Coverage

Installation

composer require xervice/elasticsearch:^1.0.0

Configuration

Your config

    use Xervice\Elasticsearch\ElasticsearchConfig;

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

Define Index

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

##Using Create documents

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

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

Search documents

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

    $query = new Query($boolQuery);

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

Extended Search

You can define your own QueryExtender and ResultFormatter and add them to your Search:

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