-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: V3 - refactoring #7 #12
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the Symfony ruleset please 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I thought has this setup for Symfony related stuff but I will check it 👍 Thanks for your time! I will try this week to split it as requested :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to leave the 3 rules?
I've switched to symfony and it made change in
yoda_style
.Is this what you want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's perfect. I work with yoda condition for so long I can't go back !
But in your exemple I can't figure why you don't use a simple
if (!empty($this->innerHits))
. The empty function only return bool so you don't have to be strict.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, in our team we are do like to always use a strict comparison even for bools. We find it more readable than finding
!
symbol at the start