https://github.com/miquido/elasticsearch-dbal
The project was made by Miquido. https://www.miquido.com/
https://github.com/miquido/elasticsearch-dbal
Last synced: about 1 year ago
JSON representation
The project was made by Miquido. https://www.miquido.com/
- Host: GitHub
- URL: https://github.com/miquido/elasticsearch-dbal
- Owner: miquido
- License: mit
- Created: 2018-08-13T14:16:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-05T09:58:24.000Z (almost 8 years ago)
- Last Synced: 2025-04-20T06:35:00.347Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/miquido/elasticsearch-dbal)
[](https://codeclimate.com/github/miquido/elasticsearch-dbal/maintainability)
[](https://codeclimate.com/github/miquido/elasticsearch-dbal/test_coverage)
[](https://opensource.org/licenses/mit-license.php)
# Elasticsearch DBAL
Wrapper for [https://github.com/ruflin/Elastica](https://github.com/ruflin/Elastica)
- [Installation guide](#installation)
- [Code Samples](#code-samples)
- [Contributing](#contributing)
## Installation
Use [Composer](https://getcomposer.org) to install the package:
```shell
composer require miquido/elasticsearch-dbal
```
## Code Samples
- [Initialize DBAL object](#initialize-dbal-object)
- [Count documents](#count-documents)
- [Search documents](#search-documents)
- [SearchResult and Document objects](#searchresult-and-document-objects)
- [Create new documents](#create-new-documents)
- [Update documents](#update-documents)
- [Delete documents](#delete-documents)
### Initialize *DBAL* object
*Miquido\Elasticsearch\DBAL* requires *Elastica\Type* object:
```php
getIndex('index_name')->getType('type_name');
$dbal = new DBAL($type);
```
### Count documents
```php
countAll(); // count all documents in the type
$dbal->count(new \Elastica\Query(new \Elastica\Query\Terms('field_name', [1, 2, 3]))); // count documents matching query
```
### Search documents
```php
search($query); // returns 10 documents (default ElasticSearch setting)
$dbal->searchAll($query); // returns all documents (uses scroll api)
$dbal->findOne($query);
$dbal->findByIds('id1', 'id2', 'id2');
```
### SearchResult and Document objects
*search()*, *searchAll()* and *findByIds()* methods return instance of [Miquido\Elasticsearch\Result\SearchResultInterface](src/Result/SearchResultInterface.php)
*findOne()* method returns instance of [Miquido\Elasticsearch\Document\DocumentInterface](src/Document/DocumentInterface.php)
Please also check [miquido/data-structure](https://github.com/miquido/data-structure) library for more details about classes used inside Documents objects.
```php
search(new \Elastica\Query(new \Elastica\Query\MatchAll()));
$result->count(); // returns number of documents in result
$result->getTotalHits(); // returns number of documents matching the query
$result->getTime(); // returns time of the query
$result->getDocuments()->getAll(); // returns instances of Documents
$result->getDocuments()->getData(); // returns instance of Miquido\DataStructure\Map\MapCollectionInterface
$document = $dbal->findOne(new \Elastica\Query());
$document->getId(); // string
$document->getData(); // returns instance of Miquido\DataStructure\Map\MapInterface
```
### Create new documents
```php
add(new Document(
null /* or string if you want to choose your own ID */,
new Map([
'name' => 'John',
'surname' => 'Smith',
'age' => 40,
]))
);
// you can also add many documents at once
$dbal->bulkAdd($document1, $document2, ...);
```
### Update documents
```php
updatePatch(new Document('documentId', new Map([
'age' => 41,
])));
// you can also add many documents at once
$dbal->bulkUpdatePatch($document1, $document2, ...);
```
### Delete documents
```php
deleteByIds('id1', 'id2', 'id3');
```
## Contributing
Pull requests, bug fixes and issue reports are welcome.
Before proposing a change, please discuss your change by raising an issue.