Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2023-11-30 (10 months ago) | Not enough user ratings | Total: 62 | All time: 10,453 This week: 63 |
Version | License | PHP version | Categories | |||
simple-elasticsearch 7.0.0 | Custom (specified... | 7.1 | Searching, Web services, PHP 7 |
Description | Author | |
This package can index and search documents using Elastic Search. |
<?php |
PHP library to connect to and use Elasticsearch in a simple way.
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
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
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
Files (17) |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
contrib (4 files) | ||||
sample (1 file) | ||||
src (2 files) | ||||
tests (2 files) | ||||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpmd.xml | Data | Auxiliary data | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (17) | / | contrib |
File | Role | Description |
---|---|---|
coverage-checker.php | Example | Example script |
Dockerfile | Data | Auxiliary data |
pre-commit | Data | Auxiliary data |
setup.sh | Data | Auxiliary data |
Files (17) | / | src |
File | Role | Description |
---|---|---|
BaseElasticsearch.php | Class | Class source |
SimpleElasticsearch.php | Class | Class source |
Files (17) | / | tests |
File | Role | Description |
---|---|---|
BaseElasticsearchTest.php | Class | Class source |
SimpleElasticsearchTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
simple-elasticsearch-2023-11-30.zip 39KB | |
simple-elasticsearch-2023-11-30.tar.gz 35KB | |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.