-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from pionl/v3
WIP: V3 - refactoring #7
- Loading branch information
Showing
78 changed files
with
2,083 additions
and
829 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,74 @@ | ||
ElasticSearch Query Builder | ||
=========================== | ||
|
||
![img](https://img.shields.io/badge/phpstan-8-green) | ||
![php](https://img.shields.io/badge/php-8.0-brightgreen) | ||
|
||
|
||
This is a PHP library which helps you build query for an ElasticSearch client by using a fluent interface. | ||
|
||
WARNING: This branch contains the next 3.x release. Check the [corresponding issue](https://github.com/erichard/elasticsearch-query-builder/issues/7) for the roadmap. | ||
|
||
Installation | ||
------------ | ||
|
||
``` | ||
$ composer require erichard/elasticsearch-query-builder | ||
```bash | ||
composer require erichard/elasticsearch-query-builder | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
``` | ||
```php | ||
|
||
use Erichard\ElasticQueryBuilder\QueryBuilder; | ||
use Erichard\ElasticQueryBuilder\Aggregation\Aggregation; | ||
use Erichard\ElasticQueryBuilder\Filter\Filter; | ||
|
||
$qb = new QueryBuilder(); | ||
$query = new QueryBuilder(); | ||
|
||
$qb | ||
$query | ||
->setType('my_type') | ||
->setIndex('app') | ||
->setSize(10) | ||
; | ||
|
||
// Add an aggregation | ||
$qb->addAggregation(Aggregation::terms('agg_name')->setField('my_field')); | ||
$query->addAggregation(Aggregation::terms('agg_name', 'my_field')); | ||
$query->addAggregation(Aggregation::terms('agg_name_same_as_field')); | ||
|
||
// Add a filter | ||
$boolFilter = Filter::bool(); | ||
$boolFilter->addFilter(Filter::terms()->setField('field')->setValue($value)); | ||
$boolFilter->addFilter(Filter::terms('field', 'value')); | ||
|
||
|
||
$qb->addFilter($boolFilter); | ||
$query->addFilter($boolFilter); | ||
|
||
// I am using a client from elasticsearch/elasticsearch here | ||
$results = $client->search($qb->getQuery()); | ||
$results = $client->search($query->build()); | ||
``` | ||
|
||
with PHP 8.1 you can do this: | ||
|
||
```php | ||
new BoolQuery(should: [ | ||
new RangeQuery( | ||
field: PriceTermsIndex::PREFIX_OPTION . OptionIds::PERSONS_MAX, | ||
gte: $this->roomCount | ||
), | ||
new RangeQuery( | ||
field: PriceTermsIndex::PREFIX_OPTION . OptionIds::PERSONS_MAX, | ||
gte: $this->roomCount | ||
), | ||
]) | ||
] | ||
``` | ||
|
||
Contribution | ||
------------ | ||
|
||
- Use PHPCS fixer and PHPStan | ||
- `composer lint` | ||
- Update tests (PHPUnit) | ||
- `composer test` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
# Upgrade guide for 3.0 | ||
|
||
|
||
- If you are type-hinting build aggregation use `AbstractAggregation` instead of `Aggregation` | ||
- If you are implementing own `Aggregation` - extend `AbstractAggregation` | ||
- If you are implementing own `Query` (filter) - implement `QueryInterface` | ||
|
||
## Rewrite filters to Query | ||
|
||
- All files were renamed from `Filter` suffix to `Query` | ||
- namespace has been moved to `Query` | ||
- Move values from `setField` (and other "required" properties) to a constructor of the filter | ||
- Find in your IDE all usages of this text `use Erichard\ElasticQueryBuilder\Filter\` | ||
- Then find all `Filter` definitions and rewrite them to Query | ||
- Remove any lines with `use Erichard\ElasticQueryBuilder\Filter\` | ||
- Use PHPStan to find bugs after refactoring. | ||
|
||
## New terminology | ||
|
||
All xxxFilter classes have been renamed to xxxQuery to be more consistent with Elastic terms. | ||
- All xxxFilter classes have been renamed to xxxQuery to be more consistent with Elastic terms. | ||
- We are using strict types - ensure that values you are sending are valid. | ||
- Required properties must be defined in constructor. Optional can be in constructor too (for PHP 8.1 - named arguments) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symplify\EasyCodingStandard\ValueObject\Option; | ||
use Symplify\EasyCodingStandard\ValueObject\Set\SetList; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->import(SetList::PSR_12); | ||
$containerConfigurator->import(SetList::SYMPLIFY); | ||
$containerConfigurator->import(SetList::COMMON); | ||
$containerConfigurator->import(SetList::CLEAN_CODE); | ||
|
||
$parameters = $containerConfigurator->parameters(); | ||
|
||
$parameters->set(Option::PARALLEL, true); | ||
|
||
$parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-phpunit/extension.neon | ||
- vendor/phpstan/phpstan-phpunit/rules.neon | ||
- vendor/phpstan/phpstan-deprecation-rules/rules.neon | ||
|
||
parameters: | ||
|
||
parallel: | ||
processTimeout: 600.0 | ||
|
||
paths: | ||
- src | ||
- tests | ||
|
||
# The level 8 is the highest level | ||
level: 8 | ||
|
||
# it is impossible to map build() | ||
checkMissingIterableValueType: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
bootstrap="vendor/autoload.php"> | ||
|
||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage> | ||
<include> | ||
<directory>./</directory> | ||
</include> | ||
<exclude> | ||
<directory>./tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Core\Configuration\Option; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
use Rector\Set\ValueObject\LevelSetList; | ||
use Rector\Set\ValueObject\SetList; | ||
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector; | ||
use Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
|
||
// get parameters | ||
$parameters = $containerConfigurator->parameters(); | ||
$parameters->set(Option::PATHS, [ | ||
__DIR__ . '/src', | ||
__DIR__ . '/tests', | ||
]); | ||
|
||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80); | ||
|
||
// Define what rule sets will be applied | ||
$containerConfigurator->import(LevelSetList::UP_TO_PHP_80); | ||
|
||
$containerConfigurator->import(SetList::CODE_QUALITY); | ||
$containerConfigurator->import(SetList::CODING_STYLE); | ||
|
||
$services = $containerConfigurator->services(); | ||
$services->set(AddVoidReturnTypeWhereNoReturnRector::class); | ||
$services->set(BooleanInBooleanNotRuleFixerRector::class)->configure([ | ||
BooleanInTernaryOperatorRuleFixerRector::TREAT_AS_NON_EMPTY => false, | ||
]); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Erichard\ElasticQueryBuilder\Aggregation; | ||
|
||
use Erichard\ElasticQueryBuilder\Contracts\BuildsArray; | ||
use Erichard\ElasticQueryBuilder\Features\HasAggregations; | ||
|
||
abstract class AbstractAggregation implements BuildsArray | ||
{ | ||
use HasAggregations; | ||
|
||
/** | ||
* @param array<AbstractAggregation> $aggregations | ||
*/ | ||
public function __construct( | ||
private string $name, | ||
array $aggregations = [] | ||
) { | ||
$this->aggregations = $aggregations; | ||
} | ||
|
||
/** | ||
* @return array<string, array>|array<string, string>|array<string, int> | ||
*/ | ||
public function build(): array | ||
{ | ||
$data = [ | ||
$this->getType() => $this->buildAggregation(), | ||
]; | ||
|
||
$this->buildAggregationsTo($data); | ||
|
||
return $data; | ||
} | ||
|
||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
abstract protected function getType(): string; | ||
|
||
abstract protected function buildAggregation(): array; | ||
} |
Oops, something went wrong.