kiwfy / simple-elasticsearch-php
PHP library to connect to and use Elasticsearch
Installs: 10 287
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- mockery/mockery: ^1.6
- phpmd/phpmd: ^2.14
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
README
PHP library to connect to and use Elasticsearch in a simple way.
Installation
Release 7.0.0 Requires PHP 8.3
Release 6.0.0 Requires PHP 8.2
Release 5.0.0 Requires PHP 8.1
Release 4.0.0 Requires PHP 7.4
Release 3.0.0 Requires PHP 7.3
Release 2.0.0 Requires PHP 7.2
Release 1.0.0 Requires PHP 7.1
The recommended way to install is through Composer.
composer require not-empty/simple-elasticsearch-php-lib
Usage
Setting up connection
use SimpleElasticsearch\SimpleElasticsearch; $host = 'http://localhost:9200/'; $elastic = new SimpleElasticsearch($host); $elastic->setConnectionOptions([ 'connect_timeout' => 5, 'timeout' => 5, ]);
Checking if connection is available
... $isConnected = $elastic->isConnected(); var_dump($isConnected);
Putting an index
... $indexName = 'test'; $index = $elastic->putIndex( $indexName ); var_dump($index);
Putting a mapping
... $indexName = 'test'; $mapping = [ 'properties' => [ 'name' => [ 'type' => 'keyword', ], 'email' => [ 'type' => 'keyword', ], 'gender' => [ 'type' => 'byte', ] ] ]; $newMapping = $elastic->putMapping( $indexName, $mapping ); var_dump($newMapping);
Putting a template
... $documentName = 'document'; $template = [ 'index_patterns' => [ 'document*' ], 'mappings' => [ '_source' => [ 'enabled' => true, ], 'properties' => [ 'name' => [ 'type' => 'keyword', ], 'created' => [ 'type'=> 'date', 'format' => 'yyyy-MM-dd HH:mm:ss', ], ] ] ]; $newTemplate = $elastic->putTemplate( $documentName, $template ); var_dump($newTemplate);
Getting an index
... $indexName = 'test'; $getIndex = $elastic->getIndex( $indexName ); var_dump($getIndex);
Getting a mapping
... $indexName = 'test'; $getMapping = $elastic->getMapping( $indexName ); var_dump($getMapping);
Getting a template
... $documentName = 'document'; $getTemplate = $elastic->getTemplate( $documentName ); var_dump($getTemplate);
Posting a document with template
... $documentName = 'document'; $dataTemplate = [ 'name' => 'document1', 'created' => date('Y-m-d H:i:s'), ]; $postDocumentTemplate = $elastic->postDocument( $documentName, $dataTemplate ); var_dump($postDocumentTemplate);
Posting a document passing the id
... $documentName = 'document'; $indexName = 'test'; $data = [ 'name' => 'user', 'email' => 'test@test.com', 'gender' => 0, ]; $id = '01HDRQRB0VPDDB9HWHX3MGY6XG'; $postDocument = $elastic->postDocument( $indexName, $data, $id ); var_dump($postDocument);
Getting a document by his id
... $indexName = 'test'; $id = '01HDRQRB0VPDDB9HWHX3MGY6XG'; $getDocument = $elastic->getDocument( $indexName, $id ); var_dump($getDocument);
Deleting a document by his id
... $indexName = 'test'; $id = '01HDRQRB0VPDDB9HWHX3MGY6XG'; $deleteDocument = $elastic->deleteDocument( $indexName, $id ); var_dump($deleteDocument);
Searching documents
... $indexName = 'test'; $dslQuery = [ 'term' => [ 'email' => [ 'value' => 'test@test.com', 'boost' => 1, ], ], ]; $searchDocuments = $elastic->searchDocuments( $indexName, $dslQuery ); var_dump($searchDocuments);
Listing documents
... $indexName = 'test'; $listDocuments = $elastic->listDocuments( $indexName ); var_dump($listDocuments);
Listing documents paginated
... $indexName = 'test'; $page = 2; $listDocumentsPaginated = $elastic->listDocuments( $indexName, $page ); var_dump($listDocumentsPaginated);
Executing 'SQL' querys
... $query = "SELECT * FROM test WHERE email LIKE '%test@test.com' ORDER BY email DESC"; $sqlResponse = $elastic->sql( $query ); var_dump($sqlResponse);
Executing 'SQL' querys with cursor to paginate
... // var $sql has data returned from previous query with the cursor $sqlCursorResponse = $elastic->sqlCursor( $sql['cursor'] ); var_dump($sqlCursorResponse);
Translating 'SQL' query to 'DSL' query
... $query = "SELECT * FROM test WHERE email LIKE '%test@test.com' ORDER BY email DESC"; $translate = $elastic->translate( $query ); var_dump($translate);
Deleting template
... $documentName = 'document'; $deleteTemplate = $elastic->deleteTemplate( $documentName ); var_dump($deleteTemplate);
Deleting index
... $indexName = 'test'; $deleteIndex = $elastic->deleteIndex( $indexName ); var_dump($deleteIndex);
Aggregating documents
... $indexName = 'test'; $dslAgregate = [ 'genders' => [ 'terms' => [ 'field' => 'gender', ] ] ]; $dslQueryAggregate = [ 'wildcard' => [ 'email' => [ 'wildcard' => '*1-test@test.com', 'boost' => 1, ], ], ]; $aggregateDocuments = $elastic->aggregateDocuments( $indexName, $dslAgregate, $dslQueryAggregate ); var_dump($aggregateDocuments);
if you want an environment to run or test it, you can build and install dependences like this
docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/simple-elasticsearch-php-lib:php83 -f contrib/Dockerfile .
Access the container
docker run -v ${PWD}/:/var/www/html -it not-empty/simple-elasticsearch-php-lib:php83 bash
Verify if all dependencies is installed
composer install --no-dev --prefer-dist
and run
php sample/elastic-sample.php
Development
Want to contribute? Great!
The project using a simple code. Make a change in your file and be careful with your updates! Any new code will only be accepted with all validations.
To ensure that the entire project is fine:
First you need to building a correct environment to install all dependences
docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/simple-elasticsearch-php-lib:php83 -f contrib/Dockerfile .
Access the container
docker run -v ${PWD}/:/var/www/html -it not-empty/simple-elasticsearch-php-lib:php83 bash
Install all dependences
composer install --dev --prefer-dist
Run all validations
composer check
Not Empty Foundation - Free codes, full minds