Skip to content

Commit

Permalink
Merge pull request #8 from Spameri/version-one
Browse files Browse the repository at this point in the history
Version one
  • Loading branch information
Spamercz authored Feb 24, 2022
2 parents 039c4f0 + f8e9fa0 commit ab176b0
Show file tree
Hide file tree
Showing 329 changed files with 8,241 additions and 8,182 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Package CI

on:
pull_request:

jobs:
checks:
name: Checks
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 7.4, 8.0, 8.1 ]
version: [ lowest, standard ]
elastic: [ 7.12.1 ]

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Start Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: ${{ matrix.elastic }}

- if: matrix.version == 'lowest'
name: Composer lowest
run: make composer-lowest

- if: matrix.version == 'standard'
name: Composer standard
run: make composer

- if: matrix.php == '7.4'
name: Coding standard
run: make cs

- if: matrix.php == '7.4' && matrix.version == 'lowest'
name: PHPStan lowest
run: make phpstan-lowest

- if: matrix.php == '7.4' && matrix.version == 'standard'
name: PHPStan standard
run: make phpstan

- name: Tests
run: make tests

46 changes: 0 additions & 46 deletions .travis.yml

This file was deleted.

41 changes: 22 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,28 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.4",
"ext-json": "*",
"nette/di": "^2.4.10",
"nette/security": "^2.3.0",
"nette/utils": "^2.4.10",
"spameri/elastic-query": "^0.6.0",
"elasticsearch/elasticsearch": "^6.0",
"kdyby/console": "^2.7.1",
"kdyby/datetime-provider": "v1.0.0",
"kdyby/monolog": "^1.3.2",
"tracy/tracy": "^2.4.18"
"ext-curl": "*",
"spameri/elastic-query": "^v1.0.0",
"elasticsearch/elasticsearch": "^7.12.0",
"nette/di": "^3.0.5",
"nette/utils": "^3.2.5",
"symfony/console": "^4.4.26",
"tracy/tracy": "^2.6.7",
"monolog/monolog": "^2.0.2",
"ezimuel/ringphp": "^1.2"
},
"require-dev": {
"spameri/coding-standard": "dev-master",
"spameri/dependency-mocker": "^1.3",
"nette/tester": "^2.2.0",
"phpstan/phpstan-shim": "^0.11.5",
"nette/tester": "^2.4",
"phpstan/phpstan": "0.12.84",
"php-coveralls/php-coveralls": "^2.1",
"nette/bootstrap": "2.4",
"nette/http": "^2.4.7",
"mockery/mockery": "^1.2"
"nette/bootstrap": "^3.0",
"nette/http": "^3.0.7",
"kdyby/console": "^4.0.0",
"mockery/mockery": "^1.5"
},
"autoload": {
"psr-4": {
Expand All @@ -54,9 +55,11 @@
"autoload-dev": {
"psr-4": {
"SpameriTests\\": "tests/SpameriTests"
},
"classmap": [
"tests/SpameriTests/Elastic/Migration/migrations"
]
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
67 changes: 36 additions & 31 deletions doc/00_quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ In your configuration neon file you need to add these lines to `extension:` sect
```yaml
extensions:
spameriElasticSearch: \Spameri\Elastic\DI\SpameriElasticSearchExtension
console: Kdyby\Console\DI\ConsoleExtension
monolog: Kdyby\Monolog\DI\MonologExtension
```

Optionaly you need some Symfony Console implementation ie:

`````yaml
extensions:
console: Kdyby\Console\DI\ConsoleExtension
`````

### II. Configure

Now you need to tell library where is ElasticSearch running. Default values are **localhost**
Expand All @@ -38,37 +43,29 @@ spameriElasticSearch:

### III. Configure Entity

Next step is to configure your first entity. This entity is for e-shop product.
Next step is to configure your first entity. In this example, entity is for e-shop product.

In neon configuration you need just index name. In this example it is just under parameters.elasticsSearch key, but can be anywhere.

```yaml
1.| spameriElasticSearch:
2.| entities:
3.| SimpleProduct:
4.| index: spameri_simple_product
5.| dynamic: strict
6.| config: @simpleProductConfig
7.| properties:
parameters:
elasticSearch:
SimpleProductIndex: spameri_simple_product
```
- First line is extensionName
- Second line is entities config array
- Third line is EntityName
- Fourth line is index name for this entity
- Fifth line is for specifying whether index should accept new not specified fields
- Sixth line is reference to where is object with entity configuration
- Seventh line is where you can configure your entity within this neon

---

## 3. Create entity class

Mainly you need entity class, which implements \Spameri\Elastic\Entity\ElasticEntityInterface interface.
```php
<?php declare(strict_types = 1);

class SimpleProduct implements \Spameri\Elastic\Entity\IElasticEntity
class SimpleProduct implements \Spameri\Elastic\Entity\ElasticEntityInterface
{

public function __construct(
\Spameri\Elastic\Entity\Property\IElasticId $id,
\Spameri\Elastic\Entity\Property\ElasticIdInterface $id,
int $databaseId,
string $name,
?string $content,
Expand All @@ -86,8 +83,10 @@ class SimpleProduct implements \Spameri\Elastic\Entity\IElasticEntity
}
```

And then implement two methods. Method id is quite clear :) and entityVariables method returns all the properties you want inserted to Elastic.

```php
public function id(): \Spameri\Elastic\Entity\Property\IElasticId
public function id(): \Spameri\Elastic\Entity\Property\ElasticIdInterface
{
return $this->id;
}
Expand All @@ -99,9 +98,11 @@ public function entityVariables(): array
}
```

To build entity from Elasticsearch and not use black magic🪄 you have to explicitly make factory class for that entity. And from hit object which represents result from Elasticsearch you have to extract all parameters. No magic, typed 🎖️

### Factory
````php
class SimpleProductFactory implements \Spameri\Elastic\Factory\IEntityFactory
class SimpleProductFactory implements \Spameri\Elastic\Factory\EntityFactoryInterface
{

public function create(\Spameri\ElasticQuery\Response\Result\Hit $hit) : \Generator
Expand All @@ -123,23 +124,27 @@ class SimpleProductFactory implements \Spameri\Elastic\Factory\IEntityFactory
}
````

In collection factory you need to specify our entity.

### CollectionFactory
````php
class SimpleProductCollectionFactory implements \Spameri\Elastic\Factory\ICollectionFactory
class SimpleProductCollectionFactory implements \Spameri\Elastic\Factory\CollectionFactoryInterface
{

public function create(
\Spameri\Elastic\Model\IService $service
, array $elasticIds = []
, \Spameri\Elastic\Entity\IElasticEntity ... $entityCollection
) : \Spameri\Elastic\Entity\IElasticEntityCollection
\Spameri\Elastic\Model\ServiceInterface $service,
array $elasticIds = [],
\Spameri\Elastic\Entity\ElasticEntityInterface ... $entityCollection
) : \Spameri\Elastic\Entity\ElasticEntityCollectionInterface
{
return new \App\ProductModule\Entity\ProductCollection($service, $elasticIds, ... $entityCollection);
}

}
````

---

## 4. Index Configuring
````php
class SimpleProductConfig implements \Spameri\Elastic\Settings\IndexConfigInterface
Expand Down Expand Up @@ -306,7 +311,7 @@ $options = new \Spameri\Elastic\Import\Run\Options(600);
// Clear index
try {
$this->delete->execute($this->simpleProductConfig->provide()->indexName());
} catch (\Spameri\Elastic\Exception\ElasticSearchException $exception) {}
} catch (\Spameri\Elastic\Exception\AbstractElasticSearchException $exception) {}

// Create index
$this->create->execute(
Expand All @@ -330,7 +335,7 @@ class SimpleProductListPresenter extends \App\Presenter\BasePresenter
try {
$products = $this->productService->getAllBy($query);

} catch (\Spameri\Elastic\Exception\ElasticSearchException $exception) {
} catch (\Spameri\Elastic\Exception\AbstractElasticSearchException $exception) {
$products = [];
}

Expand Down Expand Up @@ -396,7 +401,7 @@ public function buildQuery(?string $queryString): \Spameri\ElasticQuery\ElasticQ
{
$query = new \Spameri\ElasticQuery\ElasticQuery();
$query->addShouldQuery(
new \Spameri\ElasticQuery\Query\Match(
new \Spameri\ElasticQuery\Query\ElasticMatch(
'name',
$queryString
)
Expand All @@ -418,7 +423,7 @@ $subQuery = new \Spameri\ElasticQuery\Query\QueryCollection();

```php
$subQuery->addShouldQuery(
new \Spameri\ElasticQuery\Query\Match(
new \Spameri\ElasticQuery\Query\ElasticMatch(
'name',
$queryString,
3,
Expand All @@ -441,7 +446,7 @@ $subQuery->addShouldQuery(
)
);
$subQuery->addShouldQuery(
new \Spameri\ElasticQuery\Query\Match(
new \Spameri\ElasticQuery\Query\ElasticMatch(
'content',
$queryString,
1,
Expand Down
17 changes: 9 additions & 8 deletions doc/01_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ Use composer `composer require spameri/elastic`
### 1. Config ElasticSearch

In your config neon, enable extensions. Kdyby/Console is there because we need it to do some command line commands.
Monolog is required by elasticsearch/elasticsearch and we can use existing extension in Kdyby/Monolog.

```neon
```yaml
extensions:
spameriElasticSearch: \Spameri\Elastic\DI\SpameriElasticSearchExtension
console: Kdyby\Console\DI\ConsoleExtension
monolog: Kdyby\Monolog\DI\MonologExtension
```

Optionaly you need some Symfony Console implementation ie:

`````yaml
extensions:
console: Kdyby\Console\DI\ConsoleExtension
`````

Then configure where is your ElasticSearch.
```neon
spameriElasticSearch:
Expand All @@ -37,8 +41,7 @@ $result = $this->clientProvider->client()->search(
$index,
new \Spameri\ElasticQuery\Document\Body\Plain(
$elasticQuery->toArray()
),
$index
)
)
)->toArray()
);
Expand All @@ -52,8 +55,6 @@ what methods and arrays are supported.

### 2. First entity

#### [Neon file configuration](02_neon_configuration.md)

#### [Create entity class](03_entity_class.md)

#### [Create entity service](12_entity_service.md)
Expand Down
Loading

0 comments on commit ab176b0

Please sign in to comment.