Skip to content

Commit

Permalink
Merge pull request #12 from pionl/v3
Browse files Browse the repository at this point in the history
WIP: V3 - refactoring #7
  • Loading branch information
erichard authored Apr 6, 2022
2 parents 9b7398b + 6da5cae commit 96627da
Show file tree
Hide file tree
Showing 78 changed files with 2,083 additions and 829 deletions.
47 changes: 38 additions & 9 deletions README.md
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`

19 changes: 18 additions & 1 deletion UPGRADE-3.0.md
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)

28 changes: 24 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,44 @@
}
],
"autoload": {
"psr-4": {
"psr-4": {
"Erichard\\ElasticQueryBuilder\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"psr-4": {
"Tests\\Erichard\\ElasticQueryBuilder\\": "tests/"
}
},
"require": {
"php": ">=7.0"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpstan/phpstan": "^1.4.10",
"phpstan/phpstan-deprecation-rules": "^1.0.0",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpunit/phpunit": "^9.5.19",
"rector/rector": "^0.12.17",
"symplify/easy-coding-standard": "^10.1"
},
"scripts": {
"lint:fix": "./vendor/bin/ecs --fix",
"lint:stan": "./vendor/bin/phpstan",
"lint:upgrade": "vendor/bin/rector process",
"lint": "composer lint:fix && composer lint:upgrade && composer lint:stan",
"test": "./vendor/bin/phpunit"
},
"extra": {
"branch-alias": {
"dev-main": "3.0-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"symfony/thanks": false
}
}
}
20 changes: 20 additions & 0 deletions ecs.php
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']);
};
20 changes: 20 additions & 0 deletions phpstan.neon
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

43 changes: 16 additions & 27 deletions phpunit.xml.dist
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>
38 changes: 38 additions & 0 deletions rector.php
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,
]);

};
53 changes: 53 additions & 0 deletions src/Aggregation/AbstractAggregation.php
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;
}
Loading

0 comments on commit 96627da

Please sign in to comment.