diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9f6fe0..6d6e6df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,8 +9,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ 7.4 ] - elastic: [ 7.12.1, 6.8.15, 5.6.16 ] + php: [ 7.4, 8.1 ] + elastic: [ 7.17.0 ] steps: - uses: actions/checkout@v2 @@ -33,14 +33,14 @@ jobs: - name: Composer run: make composer - - if: matrix.php == '7.4' && matrix.elastic == '7.12.1' + - if: matrix.php == '8.1' && matrix.elastic == '7.17.0' name: Coding standard run: make cs - - if: matrix.php == '7.4' && matrix.elastic == '7.12.1' + - if: matrix.php == '8.1' && matrix.elastic == '7.17.0' name: PHPStan run: make phpstan - - if: matrix.php == '7.4' + - if: matrix.php == '8.1' name: Tests run: make tests diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 47b207b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,46 +0,0 @@ -language: php - -os: linux - -cache: - directories: - - $HOME/.composer/cache - - $HOME/phpcs-cache - -php: - - "7.1" - - "7.2" - - "7.3" - -jdk: - - oraclejdk9 - -env: - matrix: -# - RUN_TESTS=1 ES_VERSION="2.4.6" - - RUN_TESTS=1 ES_VERSION="5.6.15" - - RUN_TESTS=1 ES_VERSION="6.7.1" - - RUN_TESTS=1 ES_VERSION="7.0.0-linux-x86_64" - -matrix: - include: - - php: 7.3 - env: CODING_STANDARD=1 PHPSTAN=1 - allow_failures: - - env: RUN_TESTS=1 ES_VERSION="7.0.0-linux-x86_64" - -before_install: - - travis_retry composer self-update - - sudo apt-get update && sudo apt-get install oracle-java8-installer - - java -version - - sudo update-alternatives --set java /usr/lib/jvm/java-8-oracle/jre/bin/java - - java -version - - if [ "$ES_VERSION" != "" ]; then ./travis-elastic.sh; fi - -install: - - travis_retry composer update --no-interaction --no-suggest --no-progress --prefer-dist --prefer-stable - -script: - - if [ "$PHPSTAN" = "1" ]; then vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests; fi - - if [ "$CODING_STANDARD" = "1" ]; then vendor/bin/phpcs --standard=ruleset.xml --cache=$HOME/phpcs-cache/.phpcs-cache src tests; fi - - if [ "$RUN_TESTS" = "1" ]; then vendor/bin/tester $COVERAGE -s -c ./tests/php.ini ./tests; fi diff --git a/Makefile b/Makefile index 5bd9f8e..59e8aaf 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ .PHONY: composer .PHONY: phpstan .PHONY: cs +.PHONY: cs-local +.PHONY: cbf .PHONY: tests .PHONY: coverage + composer: composer update --no-interaction --no-suggest --no-progress --prefer-dist --prefer-stable @@ -11,10 +14,16 @@ phpstan: vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests cs: - vendor/bin/phpcs --standard=ruleset.xml --cache=$HOME/phpcs-cache/.phpcs-cache src tests + vendor/bin/phpcs --standard=ruleset.xml --cache=.phpcs-cache src tests/SpameriTests + +cs-local: + vendor/bin/phpcs --standard=ruleset.xml src tests/SpameriTests + +cbf: + vendor/bin/phpcbf --standard=ruleset.xml src tests/SpameriTests tests: - vendor/bin/tester -s -p php --colors 1 -C tests + vendor/bin/tester -s -p php --colors 1 -C -j 1 tests coverage: vendor/bin/tester -s -p php --colors 1 -C --coverage ./coverage.html --coverage-src ./src tests diff --git a/README.md b/README.md index caf0c6e..eb61922 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # ElasticQuery Elastic documentation converted to php objects for easy using. -[![Build Status](https://travis-ci.org/Spameri/ElasticQuery.svg?branch=master)](https://travis-ci.org/Spameri/ElasticQuery) - Installation ------------ diff --git a/composer.json b/composer.json index aaecc37..c426a97 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,12 @@ "issues": "https://github.com/Spameri/ElasticQuery/issues" }, "require": { - "php": ">=7.1", + "php": ">=7.4", "ext-json": "*" }, "require-dev": { - "phpstan/phpstan": "v0.12.37", + "ext-curl": "*", + "phpstan/phpstan": "^1.4.6", "nette/tester": "v2.3.1", "elasticsearch/elasticsearch": "^7", "guzzlehttp/guzzle": "^6.3", diff --git a/doc/01-usage.md b/doc/01-usage.md index 56f2d26..1e4501d 100644 --- a/doc/01-usage.md +++ b/doc/01-usage.md @@ -1,9 +1,10 @@ # Using with [ElasticSearch/ElasticSearch](https://github.com/elastic/elasticsearch-php) First we need to prepare query for what we want to search. + ```php $query = new \Spameri\ElasticQuery\ElasticQuery(); $query->query()->must()->add( - new \Spameri\ElasticQuery\Query\Match( + new \Spameri\ElasticQuery\Query\ElasticMatch( 'name', 'Avengers' ) diff --git a/phpstan.neon b/phpstan.neon index d37576b..945d242 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,6 @@ parameters: ignoreErrors: - - "#Call to function array_key_exists\\(\\) with string and array will always evaluate to false#" + - "#Call to function array_key_exists\\(\\) with string and array will always evaluate to false#" + + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false diff --git a/ruleset.xml b/ruleset.xml index 675fdad..5abf5a9 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -6,6 +6,7 @@ + diff --git a/src/Aggregation/AggregationCollection.php b/src/Aggregation/AggregationCollection.php index 8166ea4..fa39a51 100644 --- a/src/Aggregation/AggregationCollection.php +++ b/src/Aggregation/AggregationCollection.php @@ -31,13 +31,13 @@ public function __construct( } - public function key() : string + public function key(): string { return 'top-aggs-collection'; } - public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection + public function filter(): \Spameri\ElasticQuery\Filter\FilterCollection { return $this->filter; } @@ -45,13 +45,13 @@ public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection public function add( LeafAggregationCollection $leafAggregation - ) : void + ): void { $this->aggregations[$leafAggregation->key()] = $leafAggregation; } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->aggregations)); } @@ -59,19 +59,19 @@ public function keys() : array public function isKey( string $key - ) : bool + ): bool { return \array_key_exists($key, \array_map('\strval', \array_keys($this->aggregations))); } - public function count() : int + public function count(): int { return \count($this->aggregations); } - public function toArray() : array + public function toArray(): array { $array = []; diff --git a/src/Aggregation/Avg.php b/src/Aggregation/Avg.php new file mode 100644 index 0000000..9292872 --- /dev/null +++ b/src/Aggregation/Avg.php @@ -0,0 +1,37 @@ +field = $field; + } + + + public function key(): string + { + return 'avg_' . $this->field; + } + + + public function toArray(): array + { + return [ + 'avg' => [ + 'field' => $this->field, + ], + ]; + } + +} diff --git a/src/Aggregation/Filter.php b/src/Aggregation/Filter.php new file mode 100644 index 0000000..5fb9c1b --- /dev/null +++ b/src/Aggregation/Filter.php @@ -0,0 +1,27 @@ + [ + 'bool' => $array, + ], + ]; + } + +} diff --git a/src/Aggregation/Histogram.php b/src/Aggregation/Histogram.php index 5aded05..8a1b053 100644 --- a/src/Aggregation/Histogram.php +++ b/src/Aggregation/Histogram.php @@ -9,15 +9,9 @@ class Histogram implements LeafAggregationInterface { - /** - * @var string - */ - private $field; + private string $field; - /** - * @var int - */ - private $interval; + private int $interval; public function __construct( @@ -30,13 +24,13 @@ public function __construct( } - public function key() : string + public function key(): string { return $this->field; } - public function toArray() : array + public function toArray(): array { return [ 'histogram' => [ diff --git a/src/Aggregation/LeafAggregationCollection.php b/src/Aggregation/LeafAggregationCollection.php index 4b51428..0822bd7 100644 --- a/src/Aggregation/LeafAggregationCollection.php +++ b/src/Aggregation/LeafAggregationCollection.php @@ -38,28 +38,34 @@ public function __construct( } - public function key() : string + public function addAggregation(LeafAggregationInterface $aggregation): void + { + $this->aggregations[$aggregation->key()] = $aggregation; + } + + + public function key(): string { return $this->name; } - public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection + public function filter(): \Spameri\ElasticQuery\Filter\FilterCollection { return $this->filter; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->aggregations); } - public function toArray() : array + public function toArray(): array { $array = []; - $hasFilter = $this->filter && \count($this->filter->toArray()); + $hasFilter = \count($this->filter->toArray()); foreach ($this->aggregations as $aggregation) { if ($aggregation instanceof \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection) { diff --git a/src/Aggregation/Max.php b/src/Aggregation/Max.php new file mode 100644 index 0000000..2cf758d --- /dev/null +++ b/src/Aggregation/Max.php @@ -0,0 +1,37 @@ +field = $field; + } + + + public function key(): string + { + return 'max_' . $this->field; + } + + + /** + * @return array> + */ + public function toArray(): array + { + return [ + 'max' => [ + 'field' => $this->field, + ], + ]; + } + +} diff --git a/src/Aggregation/Min.php b/src/Aggregation/Min.php new file mode 100644 index 0000000..fc9248c --- /dev/null +++ b/src/Aggregation/Min.php @@ -0,0 +1,36 @@ +field = $field; + } + + + public function key(): string + { + return 'min_' . $this->field; + } + + + /** + * @return array + */ + public function toArray(): array + { + return [ + 'min' => [ + 'field' => $this->field, + ], + ]; + } + +} diff --git a/src/Aggregation/Nested.php b/src/Aggregation/Nested.php new file mode 100644 index 0000000..801265e --- /dev/null +++ b/src/Aggregation/Nested.php @@ -0,0 +1,33 @@ +path = $path; + } + + + public function key(): string + { + return 'nested_' . $this->path; + } + + + public function toArray(): array + { + return [ + 'nested' => [ + 'path' => $this->path, + ], + ]; + } + +} diff --git a/src/Aggregation/Range.php b/src/Aggregation/Range.php index 731f1a0..e4e9eb1 100644 --- a/src/Aggregation/Range.php +++ b/src/Aggregation/Range.php @@ -37,13 +37,13 @@ public function __construct( } - public function key() : string + public function key(): string { return $this->field; } - public function toArray() : array + public function toArray(): array { $array = [ 'field' => $this->field, @@ -63,7 +63,7 @@ public function toArray() : array } - public function ranges() : \Spameri\ElasticQuery\Aggregation\RangeValueCollection + public function ranges(): \Spameri\ElasticQuery\Aggregation\RangeValueCollection { return $this->ranges; } diff --git a/src/Aggregation/RangeValue.php b/src/Aggregation/RangeValue.php index 4e44d3c..b9c831b 100644 --- a/src/Aggregation/RangeValue.php +++ b/src/Aggregation/RangeValue.php @@ -6,46 +6,64 @@ class RangeValue implements \Spameri\ElasticQuery\Entity\EntityInterface { - /** - * @var string - */ - private $key; + private string $key; /** - * @var int + * @var int|float|string|\DateTimeInterface|null */ private $from; /** - * @var int + * @var int|float|string|\DateTimeInterface|null */ private $to; + private bool $fromEqual; + private bool $toEqual; + + /** + * @param int|float|string|\DateTimeInterface|null $from + * @param int|float|string|\DateTimeInterface|null $to + */ public function __construct( - string $key - , int $from - , int $to + string $key, + $from, + $to, + bool $fromEqual = TRUE, + bool $toEqual = TRUE ) { $this->key = $key; $this->from = $from; $this->to = $to; + $this->fromEqual = $fromEqual; + $this->toEqual = $toEqual; } - public function key() : string + public function key(): string { return $this->key; } - public function toArray() : array + public function toArray(): array { + $from = $this->from; + $to = $this->to; + + if ( ! $this->fromEqual && \is_int($from)) { + $from++; + } + if ($this->toEqual && \is_int($to)) { + $to++; + } + return [ 'key' => $this->key, - 'from' => $this->from, - 'to' => $this->to, + 'from' => $from, + 'to' => $to, ]; } diff --git a/src/Aggregation/RangeValueCollection.php b/src/Aggregation/RangeValueCollection.php index b6e19ce..63fc538 100644 --- a/src/Aggregation/RangeValueCollection.php +++ b/src/Aggregation/RangeValueCollection.php @@ -20,7 +20,7 @@ public function __construct( } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } @@ -28,7 +28,7 @@ public function getIterator() : \ArrayIterator public function add( \Spameri\ElasticQuery\Aggregation\RangeValue $rangeValue - ) : void + ): void { $this->collection[] = $rangeValue; } diff --git a/src/Aggregation/Term.php b/src/Aggregation/Term.php index 20040b3..6d42ed8 100644 --- a/src/Aggregation/Term.php +++ b/src/Aggregation/Term.php @@ -9,58 +9,73 @@ class Term implements LeafAggregationInterface { - /** - * @var string - */ - private $field; - - /** - * @var int - */ - private $size; - - /** - * @var ?int - */ - private $missing; - - /** - * @var ?string - */ - private $key; + private string $field; + + private int $size; + + private ?int $missing; + + private ?string $key; + + private \Spameri\ElasticQuery\Aggregation\Terms\OrderCollection $order; + + private ?string $include; + + private ?string $exclude; public function __construct( - string $field - , int $size = 5 - , int $missing = NULL - , string $key = NULL + string $field, + int $size = 0, + int $missing = NULL, + ?\Spameri\ElasticQuery\Aggregation\Terms\OrderCollection $order = NULL, + ?string $include = NULL, + ?string $exclude = NULL, + ?string $key = NULL ) { $this->field = $field; $this->size = $size; $this->missing = $missing; $this->key = $key; + $this->order = $order ?? new \Spameri\ElasticQuery\Aggregation\Terms\OrderCollection(); + $this->include = $include; + $this->exclude = $exclude; } - public function key() : string + public function key(): string { return $this->key ?? $this->field; } - public function toArray() : array + public function toArray(): array { $array = [ 'field' => $this->field, - 'size' => $this->size, ]; + if ($this->size > 0) { + $array['size'] = $this->size; + } + if ($this->missing !== NULL) { $array['missing'] = $this->missing; } + if (\count($this->order)) { + $array['order'] = $this->order->toArray(); + } + + if ($this->include !== NULL) { + $array['include'] = $this->include; + } + + if ($this->exclude !== NULL) { + $array['exclude'] = $this->exclude; + } + return [ 'terms' => $array, ]; diff --git a/src/Aggregation/Terms/Order.php b/src/Aggregation/Terms/Order.php new file mode 100644 index 0000000..9da1f43 --- /dev/null +++ b/src/Aggregation/Terms/Order.php @@ -0,0 +1,35 @@ +field = $field; + $this->type = $type; + } + + + public function key(): string + { + return 'order_' . $this->field . '_' . $this->type; + } + + + public function toArray(): array + { + return [ + $this->field => $this->type, + ]; + } + +} diff --git a/src/Aggregation/Terms/OrderCollection.php b/src/Aggregation/Terms/OrderCollection.php new file mode 100644 index 0000000..0a492a4 --- /dev/null +++ b/src/Aggregation/Terms/OrderCollection.php @@ -0,0 +1,97 @@ + + */ + private array $collection; + + + public function __construct( + \Spameri\ElasticQuery\Aggregation\Terms\Order ... $collection + ) { + $this->collection = []; + foreach ($collection as $item) { + $this->add($item); + } + } + + + public function remove(string $key): bool + { + if (isset($this->collection[$key])) { + unset($this->collection[$key]); + + return TRUE; + } + + return FALSE; + } + + + public function get(string $key): ?\Spameri\ElasticQuery\Aggregation\Terms\Order + { + return $this->collection[$key] ?? NULL; + } + + + public function isValue(string $key): bool + { + return isset($this->collection[$key]); + } + + + public function keys(): array + { + return \array_keys($this->collection); + } + + + public function clear(): void + { + $this->collection = []; + } + + + /** + * @param \Spameri\ElasticQuery\Aggregation\Terms\Order $item + */ + public function add($item): void + { + $this->collection[$item->key()] = $item; + } + + + public function getIterator(): \ArrayIterator + { + return new \ArrayIterator($this->collection); + } + + + public function count(): int + { + return \count($this->collection); + } + + + public function toArray(): array + { + if (\count($this->collection) === 1) { + return \reset($this->collection)->toArray(); + } + + $array = []; + foreach ($this->collection as $order) { + $array[] = $order->toArray(); + } + + return $array; + } + +} diff --git a/src/Aggregation/TopHits.php b/src/Aggregation/TopHits.php new file mode 100644 index 0000000..e446163 --- /dev/null +++ b/src/Aggregation/TopHits.php @@ -0,0 +1,33 @@ +size = $size; + } + + + public function key(): string + { + return 'top_hits_' . $this->size; + } + + + public function toArray(): array + { + return [ + 'top_hits' => [ + 'size' => $this->size, + ], + ]; + } + +} diff --git a/src/Collection/AbstractCollection.php b/src/Collection/AbstractCollection.php index e428604..c0736fc 100644 --- a/src/Collection/AbstractCollection.php +++ b/src/Collection/AbstractCollection.php @@ -25,7 +25,7 @@ public function __construct( public function add( \Spameri\ElasticQuery\Entity\EntityInterface $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -33,7 +33,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -47,19 +47,15 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Entity\EntityInterface + ): ?\Spameri\ElasticQuery\Entity\EntityInterface { - if (isset($this->collection[$key])) { - return $this->collection[$key]; - } - - return NULL; + return $this->collection[$key] ?? NULL; } public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -69,25 +65,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Collection/CollectionInterface.php b/src/Collection/CollectionInterface.php index 5d5eb9e..d6e0065 100644 --- a/src/Collection/CollectionInterface.php +++ b/src/Collection/CollectionInterface.php @@ -8,30 +8,30 @@ interface CollectionInterface extends \IteratorAggregate public function add( \Spameri\ElasticQuery\Entity\EntityInterface $item - ) : void; + ): void; public function remove( string $key - ) : bool; + ): bool; public function get( string $key - ) : ?\Spameri\ElasticQuery\Entity\EntityInterface; + ): ?\Spameri\ElasticQuery\Entity\EntityInterface; public function isValue( string $key - ) : bool; + ): bool; - public function count() : int; + public function count(): int; - public function keys() : array; + public function keys(): array; - public function clear() : void; + public function clear(): void; } diff --git a/src/Collection/QueryCollectionInterface.php b/src/Collection/QueryCollectionInterface.php index 40605f5..ff9d90f 100644 --- a/src/Collection/QueryCollectionInterface.php +++ b/src/Collection/QueryCollectionInterface.php @@ -8,30 +8,30 @@ interface QueryCollectionInterface extends \IteratorAggregate public function add( \Spameri\ElasticQuery\Query\LeafQueryInterface $item - ) : void; + ): void; public function remove( string $key - ) : bool; + ): bool; public function get( string $key - ) : ?\Spameri\ElasticQuery\Query\LeafQueryInterface; + ): ?\Spameri\ElasticQuery\Query\LeafQueryInterface; public function isValue( string $key - ) : bool; + ): bool; - public function count() : int; + public function count(): int; - public function keys() : array; + public function keys(): array; - public function clear() : void; + public function clear(): void; } diff --git a/src/Collection/SimpleCollectionInterface.php b/src/Collection/SimpleCollectionInterface.php index 11a59a7..edf1b4b 100644 --- a/src/Collection/SimpleCollectionInterface.php +++ b/src/Collection/SimpleCollectionInterface.php @@ -6,16 +6,22 @@ interface SimpleCollectionInterface extends \IteratorAggregate { + /** + * @phpstan-param mixed $item + */ public function add( $item - ) : void; + ): void; public function remove( string $key - ) : bool; + ): bool; + /** + * @phpstan-return mixed + */ public function get( string $key ); @@ -23,15 +29,15 @@ public function get( public function isValue( string $key - ) : bool; + ): bool; - public function count() : int; + public function count(): int; - public function keys() : array; + public function keys(): array; - public function clear() : void; + public function clear(): void; } diff --git a/src/Document.php b/src/Document.php index b29222c..2aa74b6 100644 --- a/src/Document.php +++ b/src/Document.php @@ -16,6 +16,11 @@ class Document implements \Spameri\ElasticQuery\Entity\ArrayInterface */ private $body; + /** + * @var ?string + */ + private $type; + /** * @var ?string */ @@ -30,12 +35,14 @@ class Document implements \Spameri\ElasticQuery\Entity\ArrayInterface public function __construct( ?string $index , ?\Spameri\ElasticQuery\Document\BodyInterface $body = NULL + , ?string $type = NULL , ?string $id = NULL , array $options = [] ) { $this->index = $index; $this->body = $body; + $this->type = $type; $this->id = $id; $this->options = $options; } @@ -47,7 +54,13 @@ public function index(): ?string } - public function toArray() : array + public function type(): ?string + { + return $this->type; + } + + + public function toArray(): array { $array = []; @@ -59,6 +72,10 @@ public function toArray() : array $array['body'] = $this->body->toArray(); } + if ($this->type) { + $array['type'] = $this->type; + } + if ($this->id) { $array['id'] = $this->id; } diff --git a/src/Document/Body/Plain.php b/src/Document/Body/Plain.php index 6cff941..1af495e 100644 --- a/src/Document/Body/Plain.php +++ b/src/Document/Body/Plain.php @@ -20,7 +20,7 @@ public function __construct( } - public function toArray() : array + public function toArray(): array { return $this->parameters; } diff --git a/src/Document/Body/Settings.php b/src/Document/Body/Settings.php index 6b0f9af..5fe114f 100644 --- a/src/Document/Body/Settings.php +++ b/src/Document/Body/Settings.php @@ -26,18 +26,21 @@ public function __construct( } - public function toArray() : array + public function toArray(): array { $analyzers = []; - /** @var \Spameri\ElasticQuery\Mapping\AnalyzerInterface $analyzer */ + /** @var \Spameri\ElasticQuery\Mapping\AnalyzerInterface&\Spameri\ElasticQuery\Collection\Item $analyzer */ foreach ($this->analyzer as $analyzer) { - $analyzers[$analyzer->key()] = $analyzer->toArray(); + $analyzers[$analyzer->key()] = $analyzer->toArray()[$analyzer->key()]; } $filters = []; /** @var \Spameri\ElasticQuery\Mapping\FilterInterface $filter */ foreach ($this->filter as $filter) { - $filters[$filter->key()] = $filter->toArray(); + if ($filter->toArray() === [] ) { + continue; + } + $filters[$filter->key()] = $filter->toArray()[$filter->key()]; } return [ diff --git a/src/Document/Bulk.php b/src/Document/Bulk.php index 5b1d42c..7c3cf9b 100644 --- a/src/Document/Bulk.php +++ b/src/Document/Bulk.php @@ -3,7 +3,7 @@ namespace Spameri\ElasticQuery\Document; -class Bulk implements\Spameri\ElasticQuery\Entity\ArrayInterface +class Bulk implements \Spameri\ElasticQuery\Entity\ArrayInterface { /** @@ -20,7 +20,7 @@ public function __construct( } - public function toArray() : array + public function toArray(): array { return [ 'body' => $this->data, diff --git a/src/ElasticQuery.php b/src/ElasticQuery.php index cd93771..afe267f 100644 --- a/src/ElasticQuery.php +++ b/src/ElasticQuery.php @@ -6,106 +6,112 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface { - /** - * @var \Spameri\ElasticQuery\Query\QueryCollection - */ - private $query; + private \Spameri\ElasticQuery\Query\QueryCollection $query; - /** - * @var \Spameri\ElasticQuery\Filter\FilterCollection - */ - private $filter; + private \Spameri\ElasticQuery\Filter\FilterCollection $filter; - /** - * @var \Spameri\ElasticQuery\Options\SortCollection - */ - private $sort; + private \Spameri\ElasticQuery\Options\SortCollection $sort; - /** - * @var \Spameri\ElasticQuery\Aggregation\AggregationCollection - */ - private $aggregation; + private \Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation; - /** - * @var \Spameri\ElasticQuery\Options - */ - private $options; + private \Spameri\ElasticQuery\Options $options; + + private ?\Spameri\ElasticQuery\Highlight $highlight; + + private ?\Spameri\ElasticQuery\FunctionScore $functionScore; public function __construct( - ?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL - , ?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL - , ?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL - , ?\Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation = NULL - , ?Options $options = NULL + ?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL, + ?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL, + ?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL, + ?\Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation = NULL, + ?\Spameri\ElasticQuery\Highlight $highlight = NULL, + ?\Spameri\ElasticQuery\FunctionScore $functionScore = NULL, + ?\Spameri\ElasticQuery\Options $options = NULL ) { - if ( ! $query) { + if ($query === NULL) { $query = new \Spameri\ElasticQuery\Query\QueryCollection(); } - if ( ! $filter) { + if ($filter === NULL) { $filter = new \Spameri\ElasticQuery\Filter\FilterCollection(); } - if ( ! $sort) { + if ($sort === NULL) { $sort = new \Spameri\ElasticQuery\Options\SortCollection(); } - if ( ! $aggregation) { + if ($aggregation === NULL) { $aggregation = new \Spameri\ElasticQuery\Aggregation\AggregationCollection(); } - if ( ! $options) { - $options = new Options(); + if ($options === NULL) { + $options = new \Spameri\ElasticQuery\Options(); } + $this->query = $query; $this->filter = $filter; $this->sort = $sort; $this->aggregation = $aggregation; $this->options = $options; + $this->highlight = $highlight; + $this->functionScore = $functionScore; } - public function query() : \Spameri\ElasticQuery\Query\QueryCollection + public function query(): \Spameri\ElasticQuery\Query\QueryCollection { return $this->query; } - public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection + public function filter(): \Spameri\ElasticQuery\Filter\FilterCollection { return $this->filter; } - public function aggregation() : \Spameri\ElasticQuery\Aggregation\AggregationCollection + public function aggregation(): \Spameri\ElasticQuery\Aggregation\AggregationCollection { return $this->aggregation; } - public function options() : \Spameri\ElasticQuery\Options + public function options(): \Spameri\ElasticQuery\Options { return $this->options; } - public function addMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function highlight(): ?\Spameri\ElasticQuery\Highlight + { + return $this->highlight; + } + + + public function functionScore(): ?\Spameri\ElasticQuery\FunctionScore + { + return $this->functionScore; + } + + + public function addMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->query->must()->add($leafQuery); } - public function addMustNotQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addMustNotQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->query->mustNot()->add($leafQuery); } - public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->query->should()->add($leafQuery); } - public function addFilter(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addFilter(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->filter->must()->add($leafQuery); } @@ -117,7 +123,7 @@ public function addAggregation(\Spameri\ElasticQuery\Aggregation\LeafAggregation } - public function toArray() : array + public function toArray(): array { $array = $this->options->toArray(); @@ -126,9 +132,13 @@ public function toArray() : array $array['query'] = $queryArray; } + if ($this->functionScore !== NULL) { + $array['query'] = $this->functionScore->toArray($array['query']); + } + $filterArray = $this->filter->toArray(); if ($filterArray) { - $array['filter'] = $filterArray; + $array['query']['bool']['filter'] = $filterArray; } $sortArray = $this->sort->toArray(); @@ -141,6 +151,10 @@ public function toArray() : array $array['aggs'] = $aggregation; } + if ($this->highlight !== NULL) { + $array['highlight'] = $this->highlight->toArray(); + } + return $array; } diff --git a/src/Entity/ArrayInterface.php b/src/Entity/ArrayInterface.php index 82be3f2..44cea94 100644 --- a/src/Entity/ArrayInterface.php +++ b/src/Entity/ArrayInterface.php @@ -6,6 +6,6 @@ interface ArrayInterface { - public function toArray() : array; + public function toArray(): array; } diff --git a/src/Entity/EntityInterface.php b/src/Entity/EntityInterface.php index a399a4b..f192032 100644 --- a/src/Entity/EntityInterface.php +++ b/src/Entity/EntityInterface.php @@ -6,6 +6,6 @@ interface EntityInterface extends ArrayInterface { - public function key() : string; + public function key(): string; } diff --git a/src/Exception/ResponseCouldNotBeMapped.php b/src/Exception/ResponseCouldNotBeMapped.php index c70fb29..64f3755 100644 --- a/src/Exception/ResponseCouldNotBeMapped.php +++ b/src/Exception/ResponseCouldNotBeMapped.php @@ -6,6 +6,9 @@ class ResponseCouldNotBeMapped extends \InvalidArgumentException { + /** + * @param string $message [optional] The Exception message to throw. + */ public function __construct( $message, int $code = 0, diff --git a/src/Filter/FilterCollection.php b/src/Filter/FilterCollection.php index 27a7a11..9e09073 100644 --- a/src/Filter/FilterCollection.php +++ b/src/Filter/FilterCollection.php @@ -24,19 +24,19 @@ public function __construct( } - public function must() : \Spameri\ElasticQuery\Query\MustCollection + public function must(): \Spameri\ElasticQuery\Query\MustCollection { return $this->mustCollection; } - public function key() : string + public function key(): string { return ''; } - public function toArray() : array + public function toArray(): array { $array = []; /** @var \Spameri\ElasticQuery\Query\LeafQueryInterface $item */ diff --git a/src/FunctionScore.php b/src/FunctionScore.php new file mode 100644 index 0000000..8fd9b15 --- /dev/null +++ b/src/FunctionScore.php @@ -0,0 +1,62 @@ +function = $function ?? new \Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection(); + $this->scoreMode = $scoreMode; + } + + + public function function(): \Spameri\ElasticQuery\FunctionScore\FunctionScoreCollection + { + return $this->function; + } + + + public function scoreMode(): ?string + { + return $this->scoreMode; + } + + + public function toArray(array $queryPart): array + { + $functions = []; + foreach ($this->function() as $function) { + $functions[] = $function->toArray(); + } + + $array = [ + 'function_score' => [ + 'query' => $queryPart, + 'functions' => $functions, + ], + ]; + + if ($this->scoreMode !== NULL) { + $array['function_score']['score_mode'] = $this->scoreMode; + } + + return $array; + } + +} diff --git a/src/FunctionScore/FunctionScoreCollection.php b/src/FunctionScore/FunctionScoreCollection.php new file mode 100644 index 0000000..ca678d3 --- /dev/null +++ b/src/FunctionScore/FunctionScoreCollection.php @@ -0,0 +1,93 @@ + + */ + private array $collection; + + + public function __construct( + \Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface ... $collection + ) + { + $this->collection = []; + foreach ($collection as $item) { + $this->add($item); + } + } + + + /** + * @param \Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface $item + */ + public function add( + $item + ): void + { + $this->collection[$item->key()] = $item; + } + + + public function remove( + string $key + ): bool + { + if (isset($this->collection[$key])) { + unset($this->collection[$key]); + + return TRUE; + } + + return FALSE; + } + + + public function get( + string $key + ): ?\Spameri\ElasticQuery\FunctionScore\FunctionScoreInterface + { + return $this->collection[$key] ?? NULL; + } + + + public function isValue( + string $key + ): bool + { + if (isset($this->collection[$key])) { + return TRUE; + } + + return FALSE; + } + + + public function count(): int + { + return \count($this->collection); + } + + + public function keys(): array + { + return \array_map('\strval', \array_keys($this->collection)); + } + + + public function clear(): void + { + $this->collection = []; + } + + + public function getIterator(): \ArrayIterator + { + return new \ArrayIterator($this->collection); + } + +} diff --git a/src/FunctionScore/FunctionScoreInterface.php b/src/FunctionScore/FunctionScoreInterface.php new file mode 100644 index 0000000..87ec6e5 --- /dev/null +++ b/src/FunctionScore/FunctionScoreInterface.php @@ -0,0 +1,11 @@ +field = $field; + $this->factor = $factor; + $this->modifier = $modifier; + $this->missing = $missing; + } + + + public function key(): string + { + return 'field_value_factor_' . $this->field; + } + + + public function field(): string + { + return $this->field; + } + + + public function factor(): float + { + return $this->factor; + } + + + public function modifier(): string + { + return $this->modifier; + } + + + public function missing(): float + { + return $this->missing; + } + + + public function toArray(): array + { + return [ + 'field_value_factor' => [ + 'field' => $this->field, + 'factor' => $this->factor, + 'modifier' => $this->modifier, + 'missing' => $this->missing, + ], + ]; + } + +} diff --git a/src/FunctionScore/ScoreFunction/RandomScore.php b/src/FunctionScore/ScoreFunction/RandomScore.php new file mode 100644 index 0000000..997588a --- /dev/null +++ b/src/FunctionScore/ScoreFunction/RandomScore.php @@ -0,0 +1,42 @@ +seed = $seed; + } + + + public function key(): string + { + return 'random_' . $this->seed; + } + + + public function seed(): ?string + { + return $this->seed; + } + + + public function toArray(): array + { + $randomScore = new \stdClass(); + + if ($this->seed) { + $randomScore->seed = $this->seed; + } + + return [ + 'random_score' => $randomScore, + ]; + } + +} diff --git a/src/FunctionScore/ScoreFunction/Weight.php b/src/FunctionScore/ScoreFunction/Weight.php new file mode 100644 index 0000000..c04a571 --- /dev/null +++ b/src/FunctionScore/ScoreFunction/Weight.php @@ -0,0 +1,48 @@ +weight = $weight; + $this->leafQuery = $leafQuery; + } + + + public function key(): string + { + return 'weight_' . $this->leafQuery->key(); + } + + + public function weight(): float + { + return $this->weight; + } + + + public function leafQuery(): \Spameri\ElasticQuery\Query\LeafQueryInterface + { + return $this->leafQuery; + } + + + public function toArray(): array + { + return [ + 'weight' => $this->weight, + 'filter' => $this->leafQuery->toArray(), + ]; + } + +} diff --git a/src/Highlight.php b/src/Highlight.php new file mode 100644 index 0000000..b96795e --- /dev/null +++ b/src/Highlight.php @@ -0,0 +1,42 @@ +preTags = $preTags; + $this->postTags = $postTags; + $this->fields = $fields; + } + + + public function toArray(): array + { + $array = [ + 'pre_tags' => $this->preTags, + 'post_tags' => $this->postTags, + ]; + + foreach ($this->fields as $key) { + $array['fields'][$key] = [ + 'number_of_fragments' => 0, + ]; + } + + return $array; + } + +} diff --git a/src/Mapping/AllowedValues.php b/src/Mapping/AllowedValues.php new file mode 100644 index 0000000..e4ae2ff --- /dev/null +++ b/src/Mapping/AllowedValues.php @@ -0,0 +1,251 @@ + self::TYPE_TEXT, + self::TYPE_KEYWORD => self::TYPE_KEYWORD, + + self::TYPE_BOOLEAN => self::TYPE_BOOLEAN, + + self::TYPE_INTEGER => self::TYPE_INTEGER, + self::TYPE_LONG => self::TYPE_LONG, + self::TYPE_SHORT => self::TYPE_SHORT, + self::TYPE_BYTE => self::TYPE_BYTE, + self::TYPE_DOUBLE => self::TYPE_DOUBLE, + self::TYPE_FLOAT => self::TYPE_FLOAT, + self::TYPE_HALF_FLOAT => self::TYPE_HALF_FLOAT, + self::TYPE_SCALED_FLOAT => self::TYPE_SCALED_FLOAT, + + self::TYPE_DATE => self::TYPE_DATE, + + self::TYPE_INTEGER_RANGE => self::TYPE_INTEGER_RANGE, + self::TYPE_FLOAT_RANGE => self::TYPE_FLOAT_RANGE, + self::TYPE_LONG_RANGE => self::TYPE_LONG_RANGE, + self::TYPE_DOUBLE_RANGE => self::TYPE_DOUBLE_RANGE, + self::TYPE_DATE_RANGE => self::TYPE_DATE_RANGE, + + self::TYPE_OBJECT => self::TYPE_OBJECT, + self::TYPE_NESTED => self::TYPE_NESTED, + + self::TYPE_GEO_POINT => self::TYPE_GEO_POINT, + self::TYPE_GEO_SHAPE => self::TYPE_GEO_SHAPE, + + self::TYPE_IP => self::TYPE_IP, + self::TYPE_COMPLETION => self::TYPE_COMPLETION, + self::TYPE_TOKEN_COUNT => self::TYPE_TOKEN_COUNT, + self::TYPE_MURMUR3 => self::TYPE_MURMUR3, + self::TYPE_PERCOLATOR => self::TYPE_PERCOLATOR, + self::TYPE_JOIN => self::TYPE_JOIN, + self::TYPE_ALIAS => self::TYPE_ALIAS, + ]; + + // + // ANALYZERS + // + + public const ANALYZER_STANDARD = 'standard'; + public const ANALYZER_SIMPLE = 'simple'; + public const ANALYZER_WHITESPACE = 'whitespace'; + public const ANALYZER_STOP = 'stop'; + public const ANALYZER_KEYWORD = 'keyword'; + public const ANALYZER_PATTERN = 'pattern'; + public const ANALYZER_FINGERPRINT = 'fingerprint'; + + + public const ANALYZER_ARABIC = 'arabic'; + public const ANALYZER_ARMENIAN = 'armenian'; + public const ANALYZER_BASQUE = 'basque'; + public const ANALYZER_BENGALI = 'bengali'; + public const ANALYZER_BRAZILIAN = 'brazilian'; + public const ANALYZER_BULGARIAN = 'bulgarian'; + public const ANALYZER_CATALAN = 'catalan'; + public const ANALYZER_CJK = 'cjk'; + public const ANALYZER_CZECH = 'czech'; + public const ANALYZER_DANISH = 'danish'; + public const ANALYZER_DUTCH = 'dutch'; + public const ANALYZER_ENGLISH = 'english'; + public const ANALYZER_FINNISH = 'finnish'; + public const ANALYZER_FRENCH = 'french'; + public const ANALYZER_GALICIAN = 'galician'; + public const ANALYZER_GERMAN = 'german'; + public const ANALYZER_GREEK = 'greek'; + public const ANALYZER_HINDI = 'hindi'; + public const ANALYZER_HUNGARIAN = 'hungarian'; + public const ANALYZER_INDONESIAN = 'indonesian'; + public const ANALYZER_IRISH = 'irish'; + public const ANALYZER_ITALIAN = 'italian'; + public const ANALYZER_LATVIAN = 'latvian'; + public const ANALYZER_LITHUANIAN = 'lithuanian'; + public const ANALYZER_NORWEGIAN = 'norwegian'; + public const ANALYZER_PERSIAN = 'persian'; + public const ANALYZER_PORTUGUESE = 'portuguese'; + public const ANALYZER_ROMANIAN = 'romanian'; + public const ANALYZER_RUSSIAN = 'russian'; + public const ANALYZER_SORANI = 'sorani'; + public const ANALYZER_SPANISH = 'spanish'; + public const ANALYZER_SWEDISH = 'swedish'; + public const ANALYZER_TURKISH = 'turkish'; + public const ANALYZER_THAI = 'thai'; + + public const ANALYZERS = [ + self::ANALYZER_STANDARD => self::ANALYZER_STANDARD, + self::ANALYZER_SIMPLE => self::ANALYZER_SIMPLE, + self::ANALYZER_WHITESPACE => self::ANALYZER_WHITESPACE, + self::ANALYZER_STOP => self::ANALYZER_STOP, + self::ANALYZER_KEYWORD => self::ANALYZER_KEYWORD, + self::ANALYZER_PATTERN => self::ANALYZER_PATTERN, + self::ANALYZER_FINGERPRINT => self::ANALYZER_FINGERPRINT, + + self::ANALYZER_ARABIC => self::ANALYZER_ARABIC, + self::ANALYZER_ARMENIAN => self::ANALYZER_ARMENIAN, + self::ANALYZER_BASQUE => self::ANALYZER_BASQUE, + self::ANALYZER_BENGALI => self::ANALYZER_BENGALI, + self::ANALYZER_BRAZILIAN => self::ANALYZER_BRAZILIAN, + self::ANALYZER_BULGARIAN => self::ANALYZER_BULGARIAN, + self::ANALYZER_CATALAN => self::ANALYZER_CATALAN, + self::ANALYZER_CJK => self::ANALYZER_CJK, + self::ANALYZER_CZECH => self::ANALYZER_CZECH, + self::ANALYZER_DANISH => self::ANALYZER_DANISH, + self::ANALYZER_DUTCH => self::ANALYZER_DUTCH, + self::ANALYZER_ENGLISH => self::ANALYZER_ENGLISH, + self::ANALYZER_FINNISH => self::ANALYZER_FINNISH, + self::ANALYZER_FRENCH => self::ANALYZER_FRENCH, + self::ANALYZER_GALICIAN => self::ANALYZER_GALICIAN, + self::ANALYZER_GERMAN => self::ANALYZER_GERMAN, + self::ANALYZER_GREEK => self::ANALYZER_GREEK, + self::ANALYZER_HINDI => self::ANALYZER_HINDI, + self::ANALYZER_HUNGARIAN => self::ANALYZER_HUNGARIAN, + self::ANALYZER_INDONESIAN => self::ANALYZER_INDONESIAN, + self::ANALYZER_IRISH => self::ANALYZER_IRISH, + self::ANALYZER_ITALIAN => self::ANALYZER_ITALIAN, + self::ANALYZER_LITHUANIAN => self::ANALYZER_LITHUANIAN, + self::ANALYZER_NORWEGIAN=> self::ANALYZER_NORWEGIAN, + self::ANALYZER_PERSIAN => self::ANALYZER_PERSIAN, + self::ANALYZER_PORTUGUESE => self::ANALYZER_PORTUGUESE, + self::ANALYZER_ROMANIAN => self::ANALYZER_ROMANIAN, + self::ANALYZER_RUSSIAN => self::ANALYZER_RUSSIAN, + self::ANALYZER_SORANI => self::ANALYZER_SORANI, + self::ANALYZER_SPANISH => self::ANALYZER_SPANISH, + self::ANALYZER_SWEDISH => self::ANALYZER_SWEDISH, + self::ANALYZER_TURKISH => self::ANALYZER_TURKISH, + self::ANALYZER_THAI => self::ANALYZER_THAI, + ]; + + // + // TOKENIZERS + // + + public const TOKENIZER_STANDARD = 'standard'; + public const TOKENIZER_LETTER = 'letter'; + public const TOKENIZER_LOWERCASE = 'lowercase'; + public const TOKENIZER_WHITESPACE = 'whitespace'; + public const TOKENIZER_UAX_URL_EMAIL = 'uax_url_email'; + public const TOKENIZER_CLASSIC = 'classic'; + public const TOKENIZER_THAI = 'thai'; + + public const TOKENIZER_NGRAM = 'ngram'; + public const TOKENIZER_EDGE_NGRAM = 'edge_ngram'; + + public const TOKENIZER_KEYWORD = 'keyword'; + public const TOKENIZER_PATTERN = 'pattern'; + public const TOKENIZER_SIMPLE_PATTERN = 'simple_pattern'; + public const TOKENIZER_CHAR_GROUP = 'char_group'; + public const TOKENIZER_SIMPLE_PATTERN_SPLIT = 'simple_pattern_split'; + public const TOKENIZER_PATH = 'path_hierarchy'; + + public const TOKENIZERS = [ + self::TOKENIZER_STANDARD => self::TOKENIZER_STANDARD, + self::TOKENIZER_LETTER => self::TOKENIZER_LETTER, + self::TOKENIZER_LOWERCASE => self::TOKENIZER_LOWERCASE, + self::TOKENIZER_WHITESPACE => self::TOKENIZER_WHITESPACE, + self::TOKENIZER_UAX_URL_EMAIL => self::TOKENIZER_UAX_URL_EMAIL, + self::TOKENIZER_CLASSIC => self::TOKENIZER_CLASSIC, + self::TOKENIZER_THAI => self::TOKENIZER_THAI, + + self::TOKENIZER_NGRAM => self::TOKENIZER_NGRAM, + self::TOKENIZER_EDGE_NGRAM => self::TOKENIZER_EDGE_NGRAM, + + self::TOKENIZER_KEYWORD => self::TOKENIZER_KEYWORD, + self::TOKENIZER_PATTERN => self::TOKENIZER_PATTERN, + self::TOKENIZER_SIMPLE_PATTERN => self::TOKENIZER_SIMPLE_PATTERN, + self::TOKENIZER_CHAR_GROUP => self::TOKENIZER_CHAR_GROUP, + self::TOKENIZER_SIMPLE_PATTERN_SPLIT => self::TOKENIZER_SIMPLE_PATTERN_SPLIT, + self::TOKENIZER_PATH => self::TOKENIZER_PATH, + ]; + + // + // Setting blocks + // + public const BLOCK_TYPE = 'type'; + public const BLOCK_ANALYZER = 'analyzer'; + public const BLOCK_TOKENIZER = 'tokenizer'; + public const BLOCK_PROPERTIES = 'properties'; + public const BLOCK_FORMAT = 'format'; + + public const BLOCKS = [ + self::BLOCK_TYPE => self::BLOCK_TYPE, + self::BLOCK_ANALYZER => self::BLOCK_ANALYZER, + self::BLOCK_TOKENIZER => self::BLOCK_TOKENIZER, + self::BLOCK_PROPERTIES => self::BLOCK_PROPERTIES, + self::BLOCK_FORMAT => self::BLOCK_FORMAT, + ]; +} diff --git a/src/Mapping/Analyzer/AbstractDictionary.php b/src/Mapping/Analyzer/AbstractDictionary.php index a3e0266..9fe04ac 100644 --- a/src/Mapping/Analyzer/AbstractDictionary.php +++ b/src/Mapping/Analyzer/AbstractDictionary.php @@ -13,45 +13,51 @@ abstract class AbstractDictionary protected $filter; /** - * @var \Spameri\ElasticQuery\Mapping\Filter\Stop + * @var ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop */ protected $stopFilter; public function __construct( - ?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL + ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop $stopFilter = NULL ) { $this->stopFilter = $stopFilter; } - public function key() : string + public function key(): string { return $this->name(); } - public function getType() : string + public function getType(): string { return 'custom'; } - public function tokenizer() : string + public function tokenizer(): string { return 'standard'; } - public function toArray() : array + public function getStopFilter(): ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop + { + return $this->stopFilter; + } + + + public function toArray(): array { $filterArray = []; /** @var \Spameri\ElasticQuery\Mapping\FilterInterface $filter */ foreach ($this->filter() as $filter) { - if ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Hunspell) { + if ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell) { $filterArray[] = $filter->getName(); - } elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Stop) { + } elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\AbstractStop) { $filterArray[] = $filter->getName(); } else { $filterArray[] = $filter->getType(); diff --git a/src/Mapping/Analyzer/Custom/ArabicDictionary.php b/src/Mapping/Analyzer/Custom/ArabicDictionary.php index c91f292..acbe3ce 100644 --- a/src/Mapping/Analyzer/Custom/ArabicDictionary.php +++ b/src/Mapping/Analyzer/Custom/ArabicDictionary.php @@ -5,9 +5,11 @@ class ArabicDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'arabicDictionary'; + public function name(): string { - return 'arabicDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/BulgarianDictionary.php b/src/Mapping/Analyzer/Custom/BulgarianDictionary.php index 9864ddc..a7d4575 100644 --- a/src/Mapping/Analyzer/Custom/BulgarianDictionary.php +++ b/src/Mapping/Analyzer/Custom/BulgarianDictionary.php @@ -5,9 +5,11 @@ class BulgarianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'bulgarianDictionary'; + public function name(): string { - return 'bulgarianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/CatalanDictionary.php b/src/Mapping/Analyzer/Custom/CatalanDictionary.php index c75482c..ddf504d 100644 --- a/src/Mapping/Analyzer/Custom/CatalanDictionary.php +++ b/src/Mapping/Analyzer/Custom/CatalanDictionary.php @@ -5,9 +5,11 @@ class CatalanDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'catalanDictionary'; + public function name(): string { - return 'catalanDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/CommonGrams.php b/src/Mapping/Analyzer/Custom/CommonGrams.php index 1899d41..29a256b 100644 --- a/src/Mapping/Analyzer/Custom/CommonGrams.php +++ b/src/Mapping/Analyzer/Custom/CommonGrams.php @@ -16,14 +16,14 @@ class CommonGrams implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterfa private $commonGramWords; /** - * @var \Spameri\ElasticQuery\Mapping\Filter\Stop + * @var \Spameri\ElasticQuery\Mapping\Filter\AbstractStop */ private $stopFilter; public function __construct( array $commonGramWords, - ?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL + ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop $stopFilter = NULL ) { $this->commonGramWords = $commonGramWords; diff --git a/src/Mapping/Analyzer/Custom/CzechDictionary.php b/src/Mapping/Analyzer/Custom/CzechDictionary.php index 0a43d56..79247c1 100644 --- a/src/Mapping/Analyzer/Custom/CzechDictionary.php +++ b/src/Mapping/Analyzer/Custom/CzechDictionary.php @@ -5,9 +5,11 @@ class CzechDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'czechDictionary'; + public function name(): string { - return 'czechDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/DanishDictionary.php b/src/Mapping/Analyzer/Custom/DanishDictionary.php index 3a7f4c3..e053b5a 100644 --- a/src/Mapping/Analyzer/Custom/DanishDictionary.php +++ b/src/Mapping/Analyzer/Custom/DanishDictionary.php @@ -5,9 +5,11 @@ class DanishDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'danishDictionary'; + public function name(): string { - return 'danishDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/DutchDictionary.php b/src/Mapping/Analyzer/Custom/DutchDictionary.php index 943905f..ab70a62 100644 --- a/src/Mapping/Analyzer/Custom/DutchDictionary.php +++ b/src/Mapping/Analyzer/Custom/DutchDictionary.php @@ -5,9 +5,11 @@ class DutchDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'dutchDictionary'; + public function name(): string { - return 'dutchDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/EdgeNgram.php b/src/Mapping/Analyzer/Custom/EdgeNgram.php index 917ce1c..1df2b84 100644 --- a/src/Mapping/Analyzer/Custom/EdgeNgram.php +++ b/src/Mapping/Analyzer/Custom/EdgeNgram.php @@ -21,7 +21,7 @@ class EdgeNgram implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface private $maxGram; /** - * @var \Spameri\ElasticQuery\Mapping\Filter\Stop + * @var \Spameri\ElasticQuery\Mapping\Filter\AbstractStop */ private $stopFilter; @@ -29,7 +29,7 @@ class EdgeNgram implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface public function __construct( int $minGram = 2, int $maxGram = 6, - ?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL + ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop $stopFilter = NULL ) { $this->minGram = $minGram; diff --git a/src/Mapping/Analyzer/Custom/EnglishDictionary.php b/src/Mapping/Analyzer/Custom/EnglishDictionary.php index 5d889b9..6d08b7b 100644 --- a/src/Mapping/Analyzer/Custom/EnglishDictionary.php +++ b/src/Mapping/Analyzer/Custom/EnglishDictionary.php @@ -5,9 +5,11 @@ class EnglishDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'englishDictionary'; + public function name(): string { - return 'englishDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/FrenchDictionary.php b/src/Mapping/Analyzer/Custom/FrenchDictionary.php index ddbe800..b2f4bad 100644 --- a/src/Mapping/Analyzer/Custom/FrenchDictionary.php +++ b/src/Mapping/Analyzer/Custom/FrenchDictionary.php @@ -5,9 +5,11 @@ class FrenchDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'frenchDictionary'; + public function name(): string { - return 'frenchDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/GermanDictionary.php b/src/Mapping/Analyzer/Custom/GermanDictionary.php index 1d50f6f..b51aaa5 100644 --- a/src/Mapping/Analyzer/Custom/GermanDictionary.php +++ b/src/Mapping/Analyzer/Custom/GermanDictionary.php @@ -5,9 +5,11 @@ class GermanDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'germanDictionary'; + public function name(): string { - return 'germanDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/GreekDictionary.php b/src/Mapping/Analyzer/Custom/GreekDictionary.php index f5e890e..245f823 100644 --- a/src/Mapping/Analyzer/Custom/GreekDictionary.php +++ b/src/Mapping/Analyzer/Custom/GreekDictionary.php @@ -5,9 +5,11 @@ class GreekDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'greekDictionary'; + public function name(): string { - return 'greekDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/HindiDictionary.php b/src/Mapping/Analyzer/Custom/HindiDictionary.php index 4769ddd..50c7ec1 100644 --- a/src/Mapping/Analyzer/Custom/HindiDictionary.php +++ b/src/Mapping/Analyzer/Custom/HindiDictionary.php @@ -5,9 +5,11 @@ class HindiDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'hindiDictionary'; + public function name(): string { - return 'hindiDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/HungarianDictionary.php b/src/Mapping/Analyzer/Custom/HungarianDictionary.php index 18167b3..4023b5a 100644 --- a/src/Mapping/Analyzer/Custom/HungarianDictionary.php +++ b/src/Mapping/Analyzer/Custom/HungarianDictionary.php @@ -5,9 +5,11 @@ class HungarianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'hungarianDictionary'; + public function name(): string { - return 'hungarianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/IndonesianDictionary.php b/src/Mapping/Analyzer/Custom/IndonesianDictionary.php index 33f589c..3787dd9 100644 --- a/src/Mapping/Analyzer/Custom/IndonesianDictionary.php +++ b/src/Mapping/Analyzer/Custom/IndonesianDictionary.php @@ -5,9 +5,11 @@ class IndonesianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'indonesianDictionary'; + public function name(): string { - return 'indonesianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/ItalianDictionary.php b/src/Mapping/Analyzer/Custom/ItalianDictionary.php index dd137c3..91e36d1 100644 --- a/src/Mapping/Analyzer/Custom/ItalianDictionary.php +++ b/src/Mapping/Analyzer/Custom/ItalianDictionary.php @@ -5,9 +5,11 @@ class ItalianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'italianDictionary'; + public function name(): string { - return 'italianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/LatvianDictionary.php b/src/Mapping/Analyzer/Custom/LatvianDictionary.php index 134828d..58d0b75 100644 --- a/src/Mapping/Analyzer/Custom/LatvianDictionary.php +++ b/src/Mapping/Analyzer/Custom/LatvianDictionary.php @@ -5,9 +5,11 @@ class LatvianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'latvianDictionary'; + public function name(): string { - return 'latvianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/NorwegianDictionary.php b/src/Mapping/Analyzer/Custom/NorwegianDictionary.php index 890991f..e84a5db 100644 --- a/src/Mapping/Analyzer/Custom/NorwegianDictionary.php +++ b/src/Mapping/Analyzer/Custom/NorwegianDictionary.php @@ -5,9 +5,11 @@ class NorwegianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'norwegianDictionary'; + public function name(): string { - return 'norwegianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/PortugueseDictionary.php b/src/Mapping/Analyzer/Custom/PortugueseDictionary.php index 4d72ef6..93d0440 100644 --- a/src/Mapping/Analyzer/Custom/PortugueseDictionary.php +++ b/src/Mapping/Analyzer/Custom/PortugueseDictionary.php @@ -5,9 +5,11 @@ class PortugueseDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'portugueseDictionary'; + public function name(): string { - return 'portugueseDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/RomanianDictionary.php b/src/Mapping/Analyzer/Custom/RomanianDictionary.php index 5e44b63..28faeaa 100644 --- a/src/Mapping/Analyzer/Custom/RomanianDictionary.php +++ b/src/Mapping/Analyzer/Custom/RomanianDictionary.php @@ -5,9 +5,11 @@ class RomanianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'romanianDictionary'; + public function name(): string { - return 'romanianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/RussianDictionary.php b/src/Mapping/Analyzer/Custom/RussianDictionary.php index 6944063..9697907 100644 --- a/src/Mapping/Analyzer/Custom/RussianDictionary.php +++ b/src/Mapping/Analyzer/Custom/RussianDictionary.php @@ -5,9 +5,11 @@ class RussianDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'russianDictionary'; + public function name(): string { - return 'russianDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/SlovakDictionary.php b/src/Mapping/Analyzer/Custom/SlovakDictionary.php new file mode 100644 index 0000000..300ce9f --- /dev/null +++ b/src/Mapping/Analyzer/Custom/SlovakDictionary.php @@ -0,0 +1,56 @@ +filter instanceof \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection) { + $this->filter = new \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection(); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\Slovak() + ); + } + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Hunspell\Slovak() + ); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\Slovak() + ); + } + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Unique() + ); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\ASCIIFolding() + ); + } + + return $this->filter; + } + +} diff --git a/src/Mapping/Analyzer/Custom/SpanishDictionary.php b/src/Mapping/Analyzer/Custom/SpanishDictionary.php index d5fe240..ed914aa 100644 --- a/src/Mapping/Analyzer/Custom/SpanishDictionary.php +++ b/src/Mapping/Analyzer/Custom/SpanishDictionary.php @@ -5,9 +5,11 @@ class SpanishDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'spanishDictionary'; + public function name(): string { - return 'spanishDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/SwedishDictionary.php b/src/Mapping/Analyzer/Custom/SwedishDictionary.php index 6a1a0ae..d9e930b 100644 --- a/src/Mapping/Analyzer/Custom/SwedishDictionary.php +++ b/src/Mapping/Analyzer/Custom/SwedishDictionary.php @@ -5,9 +5,11 @@ class SwedishDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'swedishDictionary'; + public function name(): string { - return 'swedishDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/Synonym/AbstractSynonym.php b/src/Mapping/Analyzer/Custom/Synonym/AbstractSynonym.php index 7619556..39736a7 100644 --- a/src/Mapping/Analyzer/Custom/Synonym/AbstractSynonym.php +++ b/src/Mapping/Analyzer/Custom/Synonym/AbstractSynonym.php @@ -13,7 +13,7 @@ abstract class AbstractSynonym protected $filter; /** - * @var \Spameri\ElasticQuery\Mapping\Filter\Stop + * @var ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop */ protected $stopFilter; @@ -22,36 +22,43 @@ abstract class AbstractSynonym */ protected $synonyms; + /** + * @var ?string + */ + protected $filePath; + public function __construct( - ?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL, - array $synonyms + ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop $stopFilter = NULL, + array $synonyms= [], + ?string $filePath = NULL ) { $this->stopFilter = $stopFilter; $this->synonyms = $synonyms; + $this->filePath = $filePath; } - public function key() : string + public function key(): string { return $this->name(); } - public function getType() : string + public function getType(): string { return 'custom'; } - public function tokenizer() : string + public function tokenizer(): string { return 'standard'; } - public function toArray() : array + public function toArray(): array { $filterArray = []; /** @var \Spameri\ElasticQuery\Mapping\FilterInterface $filter */ @@ -59,7 +66,10 @@ public function toArray() : array if ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Synonym) { $filterArray[] = $filter->getName(); - } elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\Stop) { + } elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\FileSynonym) { + $filterArray[] = $filter->getName(); + + } elseif ($filter instanceof \Spameri\ElasticQuery\Mapping\Filter\AbstractStop) { $filterArray[] = $filter->getName(); } else { diff --git a/src/Mapping/Analyzer/Custom/Synonym/CzechSynonym.php b/src/Mapping/Analyzer/Custom/Synonym/CzechSynonym.php index de40e2c..2c63b62 100644 --- a/src/Mapping/Analyzer/Custom/Synonym/CzechSynonym.php +++ b/src/Mapping/Analyzer/Custom/Synonym/CzechSynonym.php @@ -5,9 +5,11 @@ class CzechSynonym extends \Spameri\ElasticQuery\Mapping\Analyzer\Custom\Synonym\AbstractSynonym { + public const NAME = 'czechSynonym'; + public function name(): string { - return 'czechSynonym'; + return self::NAME; } @@ -26,11 +28,23 @@ public function filter(): \Spameri\ElasticQuery\Mapping\Settings\Analysis\Filter new \Spameri\ElasticQuery\Mapping\Filter\Stop\Czech() ); } - $this->filter->add( - new \Spameri\ElasticQuery\Mapping\Filter\Synonym( - $this->synonyms - ) - ); + + if (\count($this->synonyms)) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Synonym( + $this->synonyms + ) + ); + } + + if ($this->filePath !== NULL) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\FileSynonym( + $this->filePath + ) + ); + } + $this->filter->add( new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() ); diff --git a/src/Mapping/Analyzer/Custom/Synonym/EnglishSynonym.php b/src/Mapping/Analyzer/Custom/Synonym/EnglishSynonym.php new file mode 100644 index 0000000..c3aec36 --- /dev/null +++ b/src/Mapping/Analyzer/Custom/Synonym/EnglishSynonym.php @@ -0,0 +1,70 @@ +filter instanceof \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection) { + $this->filter = new \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection(); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\English() + ); + } + + if (\count($this->synonyms)) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Synonym( + $this->synonyms + ) + ); + } + + if ($this->filePath !== NULL) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\FileSynonym( + $this->filePath + ) + ); + } + + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\English() + ); + } + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Unique() + ); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\ASCIIFolding() + ); + } + + return $this->filter; + } + +} diff --git a/src/Mapping/Analyzer/Custom/Synonym/GermanSynonym.php b/src/Mapping/Analyzer/Custom/Synonym/GermanSynonym.php new file mode 100644 index 0000000..89d3680 --- /dev/null +++ b/src/Mapping/Analyzer/Custom/Synonym/GermanSynonym.php @@ -0,0 +1,70 @@ +filter instanceof \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection) { + $this->filter = new \Spameri\ElasticQuery\Mapping\Settings\Analysis\FilterCollection(); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\German() + ); + } + + if (\count($this->synonyms)) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Synonym( + $this->synonyms + ) + ); + } + + if ($this->filePath !== NULL) { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\FileSynonym( + $this->filePath + ) + ); + } + + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Lowercase() + ); + if ($this->stopFilter) { + $this->filter->add($this->stopFilter); + + } else { + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Stop\German() + ); + } + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\Unique() + ); + $this->filter->add( + new \Spameri\ElasticQuery\Mapping\Filter\ASCIIFolding() + ); + } + + return $this->filter; + } + +} diff --git a/src/Mapping/Analyzer/Custom/ThaiDictionary.php b/src/Mapping/Analyzer/Custom/ThaiDictionary.php index 8171786..ceb280a 100644 --- a/src/Mapping/Analyzer/Custom/ThaiDictionary.php +++ b/src/Mapping/Analyzer/Custom/ThaiDictionary.php @@ -5,9 +5,11 @@ class ThaiDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'thaiDictionary'; + public function name(): string { - return 'thaiDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/TurkishDictionary.php b/src/Mapping/Analyzer/Custom/TurkishDictionary.php index 200a512..980e742 100644 --- a/src/Mapping/Analyzer/Custom/TurkishDictionary.php +++ b/src/Mapping/Analyzer/Custom/TurkishDictionary.php @@ -5,9 +5,11 @@ class TurkishDictionary extends \Spameri\ElasticQuery\Mapping\Analyzer\AbstractDictionary { + public const NAME = 'turkishDictionary'; + public function name(): string { - return 'turkishDictionary'; + return self::NAME; } diff --git a/src/Mapping/Analyzer/Custom/WordDelimiter.php b/src/Mapping/Analyzer/Custom/WordDelimiter.php index a9a1392..fd54e6a 100644 --- a/src/Mapping/Analyzer/Custom/WordDelimiter.php +++ b/src/Mapping/Analyzer/Custom/WordDelimiter.php @@ -11,13 +11,13 @@ class WordDelimiter implements \Spameri\ElasticQuery\Mapping\CustomAnalyzerInter private $filter; /** - * @var \Spameri\ElasticQuery\Mapping\Filter\Stop + * @var \Spameri\ElasticQuery\Mapping\Filter\AbstractStop */ private $stopFilter; public function __construct( - ?\Spameri\ElasticQuery\Mapping\Filter\Stop $stopFilter = NULL + ?\Spameri\ElasticQuery\Mapping\Filter\AbstractStop $stopFilter = NULL ) { if ($stopFilter === NULL) { diff --git a/src/Mapping/Filter/Hunspell.php b/src/Mapping/Filter/AbstractHunspell.php similarity index 63% rename from src/Mapping/Filter/Hunspell.php rename to src/Mapping/Filter/AbstractHunspell.php index ac43fbb..6c432db 100644 --- a/src/Mapping/Filter/Hunspell.php +++ b/src/Mapping/Filter/AbstractHunspell.php @@ -5,7 +5,7 @@ /** * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-hunspell-tokenfilter.html */ -abstract class Hunspell implements \Spameri\ElasticQuery\Mapping\FilterInterface +abstract class AbstractHunspell implements \Spameri\ElasticQuery\Mapping\FilterInterface { public function getType(): string @@ -14,19 +14,19 @@ public function getType(): string } - abstract public function getLocale() : string; + abstract public function getLocale(): string; - abstract public function getName() : string; + abstract public function getName(): string; - public function key() : string + public function key(): string { return $this->getName(); } - public function toArray() : array + public function toArray(): array { return [ $this->getName() => [ diff --git a/src/Mapping/Filter/Stop.php b/src/Mapping/Filter/AbstractStop.php similarity index 68% rename from src/Mapping/Filter/Stop.php rename to src/Mapping/Filter/AbstractStop.php index e59a04b..d2a56d9 100644 --- a/src/Mapping/Filter/Stop.php +++ b/src/Mapping/Filter/AbstractStop.php @@ -5,7 +5,7 @@ /** * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-stop-tokenfilter.html */ -abstract class Stop implements \Spameri\ElasticQuery\Mapping\FilterInterface +abstract class AbstractStop implements \Spameri\ElasticQuery\Mapping\FilterInterface { @@ -23,25 +23,28 @@ public function __construct( } - public function getType() : string + public function getType(): string { return 'stop'; } - abstract public function getStopWords() : array; + public function getStopWords(): array + { + return $this->extraWords; + } - abstract public function getName() : string; + abstract public function getName(): string; - public function key() : string + public function key(): string { return $this->getName(); } - public function toArray() : array + public function toArray(): array { $stopWords = $this->getStopWords(); if ($this->extraWords) { diff --git a/src/Mapping/Filter/FileSynonym.php b/src/Mapping/Filter/FileSynonym.php new file mode 100644 index 0000000..a32b3dd --- /dev/null +++ b/src/Mapping/Filter/FileSynonym.php @@ -0,0 +1,59 @@ +path = $path; + } + + + public function getType(): string + { + return 'synonym'; + } + + + public function getPath(): string + { + return $this->path; + } + + + public function getName(): string + { + return 'customSynonyms'; + } + + + public function key(): string + { + return $this->getName(); + } + + + public function toArray(): array + { + return [ + $this->getName() => [ + 'type' => $this->getType(), + 'synonyms_path' => $this->path, + ], + ]; + } + +} diff --git a/src/Mapping/Filter/Hunspell/Arabic.php b/src/Mapping/Filter/Hunspell/Arabic.php index 0d3c31a..3ab5985 100644 --- a/src/Mapping/Filter/Hunspell/Arabic.php +++ b/src/Mapping/Filter/Hunspell/Arabic.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Arabic extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Arabic extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'ar'; } - public function getName() : string + public function getName(): string { return 'dictionary_AR'; } diff --git a/src/Mapping/Filter/Hunspell/Bulgarian.php b/src/Mapping/Filter/Hunspell/Bulgarian.php index c4ace0e..af621b9 100644 --- a/src/Mapping/Filter/Hunspell/Bulgarian.php +++ b/src/Mapping/Filter/Hunspell/Bulgarian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Bulgarian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Bulgarian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'bg_BG'; } - public function getName() : string + public function getName(): string { return 'dictionary_BG'; } diff --git a/src/Mapping/Filter/Hunspell/Catalan.php b/src/Mapping/Filter/Hunspell/Catalan.php index 5d21fc9..f6c6f76 100644 --- a/src/Mapping/Filter/Hunspell/Catalan.php +++ b/src/Mapping/Filter/Hunspell/Catalan.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Catalan extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Catalan extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'ca'; } - public function getName() : string + public function getName(): string { return 'dictionary_CA'; } diff --git a/src/Mapping/Filter/Hunspell/Czech.php b/src/Mapping/Filter/Hunspell/Czech.php index d165360..2a40df2 100644 --- a/src/Mapping/Filter/Hunspell/Czech.php +++ b/src/Mapping/Filter/Hunspell/Czech.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Czech extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Czech extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'cs_CZ'; } - public function getName() : string + public function getName(): string { return 'dictionary_CZ'; } diff --git a/src/Mapping/Filter/Hunspell/Danish.php b/src/Mapping/Filter/Hunspell/Danish.php index 0d85475..3e41495 100644 --- a/src/Mapping/Filter/Hunspell/Danish.php +++ b/src/Mapping/Filter/Hunspell/Danish.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Danish extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Danish extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'da_DK'; } - public function getName() : string + public function getName(): string { return 'dictionary_DK'; } diff --git a/src/Mapping/Filter/Hunspell/Dutch.php b/src/Mapping/Filter/Hunspell/Dutch.php index 7409494..970071a 100644 --- a/src/Mapping/Filter/Hunspell/Dutch.php +++ b/src/Mapping/Filter/Hunspell/Dutch.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Dutch extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Dutch extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'nl_NL'; } - public function getName() : string + public function getName(): string { return 'dictionary_NL'; } diff --git a/src/Mapping/Filter/Hunspell/English.php b/src/Mapping/Filter/Hunspell/English.php index 0075f39..a13afbd 100644 --- a/src/Mapping/Filter/Hunspell/English.php +++ b/src/Mapping/Filter/Hunspell/English.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class English extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class English extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'en_GB'; } - public function getName() : string + public function getName(): string { return 'dictionary_EN'; } diff --git a/src/Mapping/Filter/Hunspell/French.php b/src/Mapping/Filter/Hunspell/French.php index e5e78c8..fba7cb0 100644 --- a/src/Mapping/Filter/Hunspell/French.php +++ b/src/Mapping/Filter/Hunspell/French.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class French extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class French extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'fr_FR'; } - public function getName() : string + public function getName(): string { return 'dictionary_FR'; } diff --git a/src/Mapping/Filter/Hunspell/German.php b/src/Mapping/Filter/Hunspell/German.php index 83f06c7..f7debcd 100644 --- a/src/Mapping/Filter/Hunspell/German.php +++ b/src/Mapping/Filter/Hunspell/German.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class German extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class German extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'de'; } - public function getName() : string + public function getName(): string { return 'dictionary_DE'; } diff --git a/src/Mapping/Filter/Hunspell/Greek.php b/src/Mapping/Filter/Hunspell/Greek.php index 1f2ab94..8b9cfcc 100644 --- a/src/Mapping/Filter/Hunspell/Greek.php +++ b/src/Mapping/Filter/Hunspell/Greek.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Greek extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Greek extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'el_GR'; } - public function getName() : string + public function getName(): string { return 'dictionary_GR'; } diff --git a/src/Mapping/Filter/Hunspell/Hindi.php b/src/Mapping/Filter/Hunspell/Hindi.php index 1e1233e..5d5c926 100644 --- a/src/Mapping/Filter/Hunspell/Hindi.php +++ b/src/Mapping/Filter/Hunspell/Hindi.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Hindi extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Hindi extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'hi_IN'; } - public function getName() : string + public function getName(): string { return 'dictionary_IN'; } diff --git a/src/Mapping/Filter/Hunspell/Hungarian.php b/src/Mapping/Filter/Hunspell/Hungarian.php index c8cf0a7..2929ce9 100644 --- a/src/Mapping/Filter/Hunspell/Hungarian.php +++ b/src/Mapping/Filter/Hunspell/Hungarian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Hungarian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Hungarian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'hu_HU'; } - public function getName() : string + public function getName(): string { return 'dictionary_HU'; } diff --git a/src/Mapping/Filter/Hunspell/Indonesian.php b/src/Mapping/Filter/Hunspell/Indonesian.php index adc2e9b..7a8c88a 100644 --- a/src/Mapping/Filter/Hunspell/Indonesian.php +++ b/src/Mapping/Filter/Hunspell/Indonesian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Indonesian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Indonesian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'id'; } - public function getName() : string + public function getName(): string { return 'dictionary_ID'; } diff --git a/src/Mapping/Filter/Hunspell/Italian.php b/src/Mapping/Filter/Hunspell/Italian.php index a31e2a1..78763f6 100644 --- a/src/Mapping/Filter/Hunspell/Italian.php +++ b/src/Mapping/Filter/Hunspell/Italian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Italian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Italian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'it_IT'; } - public function getName() : string + public function getName(): string { return 'dictionary_IT'; } diff --git a/src/Mapping/Filter/Hunspell/Latvian.php b/src/Mapping/Filter/Hunspell/Latvian.php index 3697c82..6f159d2 100644 --- a/src/Mapping/Filter/Hunspell/Latvian.php +++ b/src/Mapping/Filter/Hunspell/Latvian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Latvian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Latvian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'lv_LV'; } - public function getName() : string + public function getName(): string { return 'dictionary_LV'; } diff --git a/src/Mapping/Filter/Hunspell/Norwegian.php b/src/Mapping/Filter/Hunspell/Norwegian.php index 60c14a3..c55c8f8 100644 --- a/src/Mapping/Filter/Hunspell/Norwegian.php +++ b/src/Mapping/Filter/Hunspell/Norwegian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Norwegian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Norwegian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'no'; } - public function getName() : string + public function getName(): string { return 'dictionary_NO'; } diff --git a/src/Mapping/Filter/Hunspell/Portuguese.php b/src/Mapping/Filter/Hunspell/Portuguese.php index 53f50db..a7cadde 100644 --- a/src/Mapping/Filter/Hunspell/Portuguese.php +++ b/src/Mapping/Filter/Hunspell/Portuguese.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Portuguese extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Portuguese extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'pt_PT'; } - public function getName() : string + public function getName(): string { return 'dictionary_PT'; } diff --git a/src/Mapping/Filter/Hunspell/Romanian.php b/src/Mapping/Filter/Hunspell/Romanian.php index cb561cf..b3be874 100644 --- a/src/Mapping/Filter/Hunspell/Romanian.php +++ b/src/Mapping/Filter/Hunspell/Romanian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Romanian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Romanian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'ro'; } - public function getName() : string + public function getName(): string { return 'dictionary_RO'; } diff --git a/src/Mapping/Filter/Hunspell/Russian.php b/src/Mapping/Filter/Hunspell/Russian.php index d8289f8..0eb35da 100644 --- a/src/Mapping/Filter/Hunspell/Russian.php +++ b/src/Mapping/Filter/Hunspell/Russian.php @@ -2,16 +2,16 @@ namespace Spameri\ElasticQuery\Mapping\Filter\Hunspell; -class Russian extends \Spameri\ElasticQuery\Mapping\Filter\Hunspell +class Russian extends \Spameri\ElasticQuery\Mapping\Filter\AbstractHunspell { - public function getLocale() : string + public function getLocale(): string { return 'ru_RU'; } - public function getName() : string + public function getName(): string { return 'dictionary_RU'; } diff --git a/src/Mapping/Filter/Hunspell/Slovak.php b/src/Mapping/Filter/Hunspell/Slovak.php new file mode 100644 index 0000000..7d453a8 --- /dev/null +++ b/src/Mapping/Filter/Hunspell/Slovak.php @@ -0,0 +1,19 @@ +synonyms; } - public function getName() : string + public function getName(): string { return 'customSynonyms'; } - public function key() : string + public function key(): string { return $this->getName(); } - public function toArray() : array + public function toArray(): array { + $synonyms = []; + foreach ($this->synonyms as $word => $synonym) { + $synonyms[] = $word . ' => ' . $synonym; + } + return [ $this->getName() => [ 'type' => $this->getType(), - 'synonyms' => $this->synonyms, + 'synonyms' => $synonyms, ], ]; } diff --git a/src/Mapping/Settings.php b/src/Mapping/Settings.php index 20b644f..3666c80 100644 --- a/src/Mapping/Settings.php +++ b/src/Mapping/Settings.php @@ -72,18 +72,77 @@ public function addMappingField(\Spameri\ElasticQuery\Mapping\Settings\Mapping\F } + public function addMappingFieldKeyword(string $name): void + { + $this->addMappingField( + new \Spameri\ElasticQuery\Mapping\Settings\Mapping\Field( + $name, + \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_KEYWORD + ) + ); + } + + + public function addMappingFieldFloat(string $name): void + { + $this->addMappingField( + new \Spameri\ElasticQuery\Mapping\Settings\Mapping\Field( + $name, + \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_FLOAT + ) + ); + } + + + public function addMappingFieldInteger(string $name): void + { + $this->addMappingField( + new \Spameri\ElasticQuery\Mapping\Settings\Mapping\Field( + $name, + \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_INTEGER + ) + ); + } + + + public function addMappingFieldBoolean(string $name): void + { + $this->addMappingField( + new \Spameri\ElasticQuery\Mapping\Settings\Mapping\Field( + $name, + \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_BOOLEAN + ) + ); + } + + public function addMappingFieldObject(\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject $fieldObject): void { $this->mapping->addFieldObject($fieldObject); } + public function addMappingNestedObject(\Spameri\ElasticQuery\Mapping\Settings\Mapping\NestedObject $fieldObject): void + { + $this->mapping->addNestedObject($fieldObject); + } + + public function addMappingSubField(\Spameri\ElasticQuery\Mapping\Settings\Mapping\SubFields $subFields): void { $this->mapping->addSubField($subFields); } + public function removeMappingSubField(string $subFields): void + { + $this->mapping->removeSubField($subFields); + } + + + /** + * @phpstan-param \Spameri\ElasticQuery\Mapping\AnalyzerInterface&\Spameri\ElasticQuery\Collection\Item $analyzer + */ public function addAnalyzer(\Spameri\ElasticQuery\Mapping\AnalyzerInterface $analyzer): void { $this->analysis->analyzer()->add($analyzer); @@ -96,6 +155,19 @@ public function addAnalyzer(\Spameri\ElasticQuery\Mapping\AnalyzerInterface $ana } + public function removeAnalyzer(string $analyzerName): void + { + $analyzer = $this->analysis->analyzer()->get($analyzerName); + if ($analyzer instanceof \Spameri\ElasticQuery\Mapping\CustomAnalyzerInterface) { + foreach ($analyzer->filter() as $filter) { + $this->removeFilter($filter); + } + } + + $this->analysis->analyzer()->remove($analyzerName); + } + + public function addTokenizer(\Spameri\ElasticQuery\Mapping\TokenizerInterface $tokenizer): void { $this->analysis->tokenizer()->add($tokenizer); @@ -108,6 +180,12 @@ public function addFilter(\Spameri\ElasticQuery\Mapping\FilterInterface $filter) } + public function removeFilter(\Spameri\ElasticQuery\Mapping\FilterInterface $filter): void + { + $this->analysis->filter()->remove($filter->key()); + } + + public function toArray(): array { $array = [ @@ -123,9 +201,7 @@ public function toArray(): array } } - if ($this->mapping) { - $array = \array_merge($array, $this->mapping->toArray()); - } + $array = \array_merge($array, $this->mapping->toArray()); return $array; } diff --git a/src/Mapping/Settings/AliasCollection.php b/src/Mapping/Settings/AliasCollection.php index e48bee9..2a44aea 100644 --- a/src/Mapping/Settings/AliasCollection.php +++ b/src/Mapping/Settings/AliasCollection.php @@ -27,7 +27,7 @@ public function __construct( */ public function add( $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -35,7 +35,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -49,7 +49,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Mapping\Settings\Alias + ): ?\Spameri\ElasticQuery\Mapping\Settings\Alias { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -61,7 +61,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -71,25 +71,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Mapping/Settings/Analysis/AnalyzerCollection.php b/src/Mapping/Settings/Analysis/AnalyzerCollection.php index 310bd80..5c92177 100644 --- a/src/Mapping/Settings/Analysis/AnalyzerCollection.php +++ b/src/Mapping/Settings/Analysis/AnalyzerCollection.php @@ -16,6 +16,9 @@ public function __construct( ) { $this->collection = []; + /** + * @phpstan-var \Spameri\ElasticQuery\Mapping\AnalyzerInterface&\Spameri\ElasticQuery\Collection\Item $item + */ foreach ($collection as $item) { $this->add($item); } @@ -23,11 +26,11 @@ public function __construct( /** - * @param \Spameri\ElasticQuery\Mapping\AnalyzerInterface $item + * @param \Spameri\ElasticQuery\Mapping\AnalyzerInterface&\Spameri\ElasticQuery\Collection\Item $item */ public function add( $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -35,7 +38,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -49,7 +52,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Mapping\AnalyzerInterface + ): ?\Spameri\ElasticQuery\Mapping\AnalyzerInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -61,7 +64,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -71,25 +74,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Mapping/Settings/Analysis/FilterCollection.php b/src/Mapping/Settings/Analysis/FilterCollection.php index 3882395..5362aac 100644 --- a/src/Mapping/Settings/Analysis/FilterCollection.php +++ b/src/Mapping/Settings/Analysis/FilterCollection.php @@ -27,7 +27,7 @@ public function __construct( */ public function add( $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -35,7 +35,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -49,7 +49,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Mapping\FilterInterface + ): ?\Spameri\ElasticQuery\Mapping\FilterInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -61,7 +61,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -71,25 +71,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Mapping/Settings/Analysis/TokenizerCollection.php b/src/Mapping/Settings/Analysis/TokenizerCollection.php index f29548b..37fb07c 100644 --- a/src/Mapping/Settings/Analysis/TokenizerCollection.php +++ b/src/Mapping/Settings/Analysis/TokenizerCollection.php @@ -27,7 +27,7 @@ public function __construct( */ public function add( $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -35,7 +35,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -49,7 +49,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Mapping\TokenizerInterface + ): ?\Spameri\ElasticQuery\Mapping\TokenizerInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -61,7 +61,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -71,25 +71,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Mapping/Settings/Mapping.php b/src/Mapping/Settings/Mapping.php index aab38f6..d91fece 100644 --- a/src/Mapping/Settings/Mapping.php +++ b/src/Mapping/Settings/Mapping.php @@ -31,6 +31,18 @@ public function __construct( } + public function getIndexName(): string + { + return $this->indexName; + } + + + public function fields(): \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection + { + return $this->fields; + } + + public function addField(\Spameri\ElasticQuery\Mapping\Settings\Mapping\Field $field): void { $this->fields->add($field); @@ -43,12 +55,30 @@ public function addFieldObject(\Spameri\ElasticQuery\Mapping\Settings\Mapping\Fi } + public function addNestedObject(\Spameri\ElasticQuery\Mapping\Settings\Mapping\NestedObject $fieldObject): void + { + $this->fields->add($fieldObject); + } + + + public function removeFieldObject(string $field): void + { + $this->fields->remove($field); + } + + public function addSubField(\Spameri\ElasticQuery\Mapping\Settings\Mapping\SubFields $subFields): void { $this->fields->add($subFields); } + public function removeSubField(string $subFields): void + { + $this->fields->remove($subFields); + } + + public function toArray(): array { $fields = []; @@ -56,13 +86,18 @@ public function toArray(): array foreach ($this->fields as $field) { if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\SubFields) { $fields[$field->key()] = $field->toArray(); - - } elseif ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject) { + continue; + } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject) { $fields[$field->key()] = $field->toArray(); - - } else { - $fields[$field->key()] = $field->toArray()[$field->key()]; + continue; } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\NestedObject) { + $fields[$field->key()] = $field->toArray(); + continue; + } + + $fields[$field->key()] = $field->toArray()[$field->key()]; } return [ diff --git a/src/Mapping/Settings/Mapping/Field.php b/src/Mapping/Settings/Mapping/Field.php index 19ee7d0..c1173d7 100644 --- a/src/Mapping/Settings/Mapping/Field.php +++ b/src/Mapping/Settings/Mapping/Field.php @@ -29,15 +29,15 @@ class Field public function __construct( string $name, - string $type = \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPE_KEYWORD, + string $type = \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_KEYWORD, ?\Spameri\ElasticQuery\Mapping\AnalyzerInterface $analyzer = NULL, ?bool $fieldData = NULL ) { $this->name = $name; - if ( ! \in_array($type, \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPES, TRUE)) { + if ( ! \in_array($type, \Spameri\ElasticQuery\Mapping\AllowedValues::TYPES, TRUE)) { throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException( - 'Not allowed type see \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPES' + 'Not allowed type see \Spameri\ElasticQuery\Mapping\AllowedValues::TYPES' ); } $this->type = $type; @@ -46,6 +46,12 @@ public function __construct( } + public function changeAnalyzer(\Spameri\ElasticQuery\Mapping\AnalyzerInterface $newAnalyzer): void + { + $this->analyzer = $newAnalyzer; + } + + public function key(): string { return $this->name; diff --git a/src/Mapping/Settings/Mapping/FieldCollection.php b/src/Mapping/Settings/Mapping/FieldCollection.php index f3a25f3..ba601fd 100644 --- a/src/Mapping/Settings/Mapping/FieldCollection.php +++ b/src/Mapping/Settings/Mapping/FieldCollection.php @@ -27,7 +27,7 @@ public function __construct( */ public function add( $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -35,7 +35,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -49,7 +49,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface + ): ?\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -61,7 +61,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -71,25 +71,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Mapping/Settings/Mapping/FieldObject.php b/src/Mapping/Settings/Mapping/FieldObject.php index c453712..e40a8b6 100644 --- a/src/Mapping/Settings/Mapping/FieldObject.php +++ b/src/Mapping/Settings/Mapping/FieldObject.php @@ -38,6 +38,19 @@ public function toArray(): array $fields = []; /** @var \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface $field */ foreach ($this->fields as $field) { + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject) { + $fields[$field->key()] = $field->toArray(); + continue; + } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection) { + $fields[$field->key()] = $field->toArray(); + continue; + } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\NestedObject) { + $fields[$field->key()] = $field->toArray(); + continue; + } + $fields[$field->key()] = $field->toArray()[$field->key()]; } diff --git a/src/Mapping/Settings/Mapping/NestedObject.php b/src/Mapping/Settings/Mapping/NestedObject.php new file mode 100644 index 0000000..84bd421 --- /dev/null +++ b/src/Mapping/Settings/Mapping/NestedObject.php @@ -0,0 +1,62 @@ +name = $name; + $this->fields = $fields; + } + + + public function key(): string + { + return $this->name; + } + + + public function toArray(): array + { + $fields = []; + /** @var \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface $field */ + foreach ($this->fields as $field) { + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject) { + $fields[$field->key()] = $field->toArray(); + continue; + } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection) { + $fields[$field->key()] = $field->toArray(); + continue; + } + if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\NestedObject) { + $fields[$field->key()] = $field->toArray(); + continue; + } + + $fields[$field->key()] = $field->toArray()[$field->key()]; + } + + return [ + 'properties' => $fields, + 'type' => 'nested', + ]; + } + +} diff --git a/src/Mapping/Settings/Mapping/SubFields.php b/src/Mapping/Settings/Mapping/SubFields.php index 0c24c22..fb07800 100644 --- a/src/Mapping/Settings/Mapping/SubFields.php +++ b/src/Mapping/Settings/Mapping/SubFields.php @@ -24,7 +24,7 @@ class SubFields public function __construct( string $name, - string $type = \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPE_KEYWORD, + string $type = \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_KEYWORD, ?\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection $fields = NULL ) { @@ -33,9 +33,9 @@ public function __construct( $fields = new \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection(); } - if ( ! \in_array($type, \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPES, TRUE)) { + if ( ! \in_array($type, \Spameri\ElasticQuery\Mapping\AllowedValues::TYPES, TRUE)) { throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException( - 'Not allowed type see \Spameri\Elastic\Model\ValidateMapping\AllowedValues::TYPES' + 'Not allowed type see \Spameri\ElasticQuery\Mapping\AllowedValues::TYPES' ); } $this->type = $type; @@ -61,6 +61,12 @@ public function addMappingField(\Spameri\ElasticQuery\Mapping\Settings\Mapping\F } + public function removeMappingField(string $field): void + { + $this->fields->remove($field); + } + + public function toArray(): array { $array['fields'] = []; diff --git a/src/Mapping/Settings/MappingOld.php b/src/Mapping/Settings/MappingOld.php index 955e11b..d1735bb 100644 --- a/src/Mapping/Settings/MappingOld.php +++ b/src/Mapping/Settings/MappingOld.php @@ -2,7 +2,7 @@ namespace Spameri\ElasticQuery\Mapping\Settings; -class Mapping implements \Spameri\ElasticQuery\Entity\ArrayInterface +class MappingOld implements \Spameri\ElasticQuery\Entity\ArrayInterface { /** diff --git a/src/Mapping/Tokenizer/CharGroup.php b/src/Mapping/Tokenizer/CharGroup.php index d44a2e4..e39261e 100644 --- a/src/Mapping/Tokenizer/CharGroup.php +++ b/src/Mapping/Tokenizer/CharGroup.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Classic.php b/src/Mapping/Tokenizer/Classic.php index e26ef8a..f8c9c21 100644 --- a/src/Mapping/Tokenizer/Classic.php +++ b/src/Mapping/Tokenizer/Classic.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/EdgeNGram.php b/src/Mapping/Tokenizer/EdgeNGram.php index 9d7124d..0478e10 100644 --- a/src/Mapping/Tokenizer/EdgeNGram.php +++ b/src/Mapping/Tokenizer/EdgeNGram.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Keyword.php b/src/Mapping/Tokenizer/Keyword.php index b735482..a0308be 100644 --- a/src/Mapping/Tokenizer/Keyword.php +++ b/src/Mapping/Tokenizer/Keyword.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Letter.php b/src/Mapping/Tokenizer/Letter.php index 77429be..17bc9de 100644 --- a/src/Mapping/Tokenizer/Letter.php +++ b/src/Mapping/Tokenizer/Letter.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Lowercase.php b/src/Mapping/Tokenizer/Lowercase.php index 84be73f..e4a2b59 100644 --- a/src/Mapping/Tokenizer/Lowercase.php +++ b/src/Mapping/Tokenizer/Lowercase.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/NGram.php b/src/Mapping/Tokenizer/NGram.php index ed3e49b..a5ca65e 100644 --- a/src/Mapping/Tokenizer/NGram.php +++ b/src/Mapping/Tokenizer/NGram.php @@ -16,7 +16,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Path.php b/src/Mapping/Tokenizer/Path.php index 0883eda..11a309d 100644 --- a/src/Mapping/Tokenizer/Path.php +++ b/src/Mapping/Tokenizer/Path.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Pattern.php b/src/Mapping/Tokenizer/Pattern.php index 138696c..a8a8375 100644 --- a/src/Mapping/Tokenizer/Pattern.php +++ b/src/Mapping/Tokenizer/Pattern.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/SimplePattern.php b/src/Mapping/Tokenizer/SimplePattern.php index 2d908e2..7213e96 100644 --- a/src/Mapping/Tokenizer/SimplePattern.php +++ b/src/Mapping/Tokenizer/SimplePattern.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/SimplePatternSplit.php b/src/Mapping/Tokenizer/SimplePatternSplit.php index fbca643..d534f26 100644 --- a/src/Mapping/Tokenizer/SimplePatternSplit.php +++ b/src/Mapping/Tokenizer/SimplePatternSplit.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Standard.php b/src/Mapping/Tokenizer/Standard.php index dbfc0b6..ec616dd 100644 --- a/src/Mapping/Tokenizer/Standard.php +++ b/src/Mapping/Tokenizer/Standard.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Thai.php b/src/Mapping/Tokenizer/Thai.php index 97dc0de..86aacc8 100644 --- a/src/Mapping/Tokenizer/Thai.php +++ b/src/Mapping/Tokenizer/Thai.php @@ -17,7 +17,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/UaxUrlEmail.php b/src/Mapping/Tokenizer/UaxUrlEmail.php index 3988d6f..d808a7c 100644 --- a/src/Mapping/Tokenizer/UaxUrlEmail.php +++ b/src/Mapping/Tokenizer/UaxUrlEmail.php @@ -16,7 +16,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Mapping/Tokenizer/Whitespace.php b/src/Mapping/Tokenizer/Whitespace.php index 7a8c1d1..6d1e0b8 100644 --- a/src/Mapping/Tokenizer/Whitespace.php +++ b/src/Mapping/Tokenizer/Whitespace.php @@ -16,7 +16,7 @@ public function getType(): string public function toArray(): array { return [ - $this->getType() + $this->getType(), ]; } diff --git a/src/Options.php b/src/Options.php index 6cc797b..36dcff5 100644 --- a/src/Options.php +++ b/src/Options.php @@ -56,25 +56,25 @@ public function __construct( } - public function changeFrom(int $from) : void + public function changeFrom(int $from): void { $this->from = $from; } - public function changeSize(int $size) : void + public function changeSize(int $size): void { $this->size = $size; } - public function sort() : \Spameri\ElasticQuery\Options\SortCollection + public function sort(): \Spameri\ElasticQuery\Options\SortCollection { return $this->sort; } - public function scroll() : ?string + public function scroll(): ?string { return $this->scroll; } @@ -82,13 +82,13 @@ public function scroll() : ?string public function startScroll( string $scroll - ) : void + ): void { $this->scroll = $scroll; } - public function scrollId() : ?string + public function scrollId(): ?string { return $this->scrollId; } @@ -96,13 +96,13 @@ public function scrollId() : ?string public function scrollInitialized( string $scrollId - ) : void + ): void { $this->scrollId = $scrollId; } - public function toArray() : array + public function toArray(): array { $array = []; diff --git a/src/Options/Sort.php b/src/Options/Sort.php index bdc42b1..014a763 100644 --- a/src/Options/Sort.php +++ b/src/Options/Sort.php @@ -53,13 +53,13 @@ public function __construct( } - public function key() : string + public function key(): string { return $this->field; } - public function toArray() : array + public function toArray(): array { return [ $this->field => [ diff --git a/src/Options/SortCollection.php b/src/Options/SortCollection.php index 80a4dca..e45e697 100644 --- a/src/Options/SortCollection.php +++ b/src/Options/SortCollection.php @@ -6,7 +6,7 @@ class SortCollection extends \Spameri\ElasticQuery\Collection\AbstractCollection implements \Spameri\ElasticQuery\Entity\ArrayInterface { - public function toArray() : array + public function toArray(): array { $array = []; diff --git a/src/Query/Match.php b/src/Query/ElasticMatch.php similarity index 57% rename from src/Query/Match.php rename to src/Query/ElasticMatch.php index 682a831..0a9639e 100644 --- a/src/Query/Match.php +++ b/src/Query/ElasticMatch.php @@ -6,53 +6,38 @@ /** * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html */ -class Match implements LeafQueryInterface +class ElasticMatch implements \Spameri\ElasticQuery\Query\LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; /** - * @var string + * @var string|int|bool|null */ private $query; - /** - * @var string - */ - private $operator; + private string $operator; - /** - * @var null|\Spameri\ElasticQuery\Query\Match\Fuzziness - */ - private $fuzziness; + private ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness; - /** - * @var float - */ - private $boost; + private float $boost; - /** - * @var null|string - */ - private $analyzer; + private ?string $analyzer; - /** - * @var int|null - */ - private $minimumShouldMatch; + private ?int $minimumShouldMatch; + /** + * @param string|int|bool|null $query + */ public function __construct( - string $field - , $query - , float $boost = 1.0 - , string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR - , ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL - , ?string $analyzer = NULL - , ?int $minimumShouldMatch = NULL + string $field, + $query, + float $boost = 1.0, + ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL, + ?int $minimumShouldMatch = NULL, + string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR, + ?string $analyzer = NULL ) { if ( ! \in_array($operator, \Spameri\ElasticQuery\Query\Match\Operator::OPERATORS, TRUE)) { @@ -71,13 +56,19 @@ public function __construct( } - public function key() : string + public function changeAnalyzer(string $newAnalyzer): void + { + $this->analyzer = $newAnalyzer; + } + + + public function key(): string { - return 'match_' . $this->field . '_' . $this->query; + return 'match_' . $this->field . '_' . (string) $this->query; } - public function toArray() : array + public function toArray(): array { $array = [ 'match' => [ @@ -92,15 +83,15 @@ public function toArray() : array $array['match'][$this->field]['operator'] = $this->operator; } - if ($this->fuzziness && $this->fuzziness->__toString()) { + if ($this->fuzziness !== NULL) { $array['match'][$this->field]['fuzziness'] = $this->fuzziness->__toString(); } - if ($this->analyzer) { + if ($this->analyzer !== NULL) { $array['match'][$this->field]['analyzer'] = $this->analyzer; } - if ($this->minimumShouldMatch) { + if ($this->minimumShouldMatch !== NULL) { $array['match'][$this->field]['minimum_should_match'] = $this->minimumShouldMatch; } diff --git a/src/Query/Exists.php b/src/Query/Exists.php index 59ed06b..f97d919 100644 --- a/src/Query/Exists.php +++ b/src/Query/Exists.php @@ -9,10 +9,7 @@ class Exists implements LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; public function __construct( @@ -23,13 +20,13 @@ public function __construct( } - public function key() : string + public function key(): string { return 'exits_' . $this->field; } - public function toArray() : array + public function toArray(): array { return [ 'exists' => [ diff --git a/src/Query/Fuzzy.php b/src/Query/Fuzzy.php index 7a14f70..67f8e11 100644 --- a/src/Query/Fuzzy.php +++ b/src/Query/Fuzzy.php @@ -15,7 +15,7 @@ class Fuzzy implements LeafQueryInterface private $field; /** - * @var string + * @var string|int|bool|null */ private $query; @@ -40,6 +40,9 @@ class Fuzzy implements LeafQueryInterface private $maxExpansion; + /** + * @param string|int|bool|null $query + */ public function __construct( string $field , $query @@ -58,13 +61,13 @@ public function __construct( } - public function key() : string + public function key(): string { - return 'fuzzy_' . $this->field . '_' . $this->query; + return 'fuzzy_' . $this->field . '_' . (string) $this->query; } - public function toArray() : array + public function toArray(): array { // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable $array = [ diff --git a/src/Query/GeoDistance.php b/src/Query/GeoDistance.php index a9c9601..2a15dd6 100644 --- a/src/Query/GeoDistance.php +++ b/src/Query/GeoDistance.php @@ -38,15 +38,15 @@ public function __construct( } - public function key() : string + public function key(): string { return 'geo_distance_' . $this->field . '_' . $this->lat . '.' . $this->lon; } - public function toArray() : array + public function toArray(): array { - $array = [ + return [ 'pin' => [ 'location' => [ 'lat' => $this->lat, @@ -54,9 +54,6 @@ public function toArray() : array ], ], ]; - - - return $array; } } diff --git a/src/Query/Match/Fuzziness.php b/src/Query/Match/Fuzziness.php index 36ac003..745fa6b 100644 --- a/src/Query/Match/Fuzziness.php +++ b/src/Query/Match/Fuzziness.php @@ -31,7 +31,7 @@ public function __construct( } - public function __toString() : string + public function __toString(): string { return $this->fuzziness; } diff --git a/src/Query/MatchPhrase.php b/src/Query/MatchPhrase.php index ab738cf..1b3a14e 100644 --- a/src/Query/MatchPhrase.php +++ b/src/Query/MatchPhrase.php @@ -15,7 +15,7 @@ class MatchPhrase implements LeafQueryInterface private $field; /** - * @var string + * @var string|int|bool|null */ private $query; @@ -35,6 +35,9 @@ class MatchPhrase implements LeafQueryInterface private $slop; + /** + * @param string|int|bool|null $query + */ public function __construct( string $field , $query @@ -51,13 +54,19 @@ public function __construct( } - public function key() : string + public function changeAnalyzer(string $newAnalyzer): void + { + $this->analyzer = $newAnalyzer; + } + + + public function key(): string { - return 'match_phrase_' . $this->field . '_' . $this->query; + return 'match_phrase_' . $this->field . '_' . (string) $this->query; } - public function toArray() : array + public function toArray(): array { $array = [ 'match_phrase' => [ diff --git a/src/Query/MultiMatch.php b/src/Query/MultiMatch.php index acb057f..2842b61 100644 --- a/src/Query/MultiMatch.php +++ b/src/Query/MultiMatch.php @@ -9,56 +9,38 @@ class MultiMatch implements LeafQueryInterface { - /** - * @var string - */ - private $fields; + private array $fields; /** - * @var string + * @var string|int|bool|null */ private $query; - /** - * @var string - */ - private $type; + private string $type; - /** - * @var string - */ - private $operator; + private string $operator; - /** - * @var null|\Spameri\ElasticQuery\Query\Match\Fuzziness - */ - private $fuzziness; + private ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness; - /** - * @var float - */ - private $boost; + private float $boost; - /** - * @var null|string - */ - private $analyzer; + private ?string $analyzer; - /** - * @var int|null - */ - private $minimumShouldMatch; + private ?int $minimumShouldMatch; + /** + * @param string|int|bool|null $query + */ public function __construct( - array $fields - , $query - , float $boost = 1.0 - , string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS - , string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR - , ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL - , ?string $analyzer = NULL - , ?int $minimumShouldMatch = NULL + array $fields, + $query, + float $boost = 1.0, + ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL, + string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS, + ?int $minimumShouldMatch = NULL, + string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR, + ?string $analyzer = NULL ) { if ( ! \in_array($operator, \Spameri\ElasticQuery\Query\Match\Operator::OPERATORS, TRUE)) { @@ -83,13 +65,19 @@ public function __construct( } - public function key() : string + public function changeAnalyzer(string $newAnalyzer): void + { + $this->analyzer = $newAnalyzer; + } + + + public function key(): string { - return 'multiMatch_' . \implode('-', $this->fields) . '_' . $this->query; + return 'multiMatch_' . \implode('-', $this->fields) . '_' . (string) $this->query; } - public function toArray() : array + public function toArray(): array { $array = [ 'multi_match' => [ diff --git a/src/Query/MustCollection.php b/src/Query/MustCollection.php index b0c8458..d5555fd 100644 --- a/src/Query/MustCollection.php +++ b/src/Query/MustCollection.php @@ -25,7 +25,7 @@ public function __construct( public function add( \Spameri\ElasticQuery\Query\LeafQueryInterface $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -33,7 +33,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -47,7 +47,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Query\LeafQueryInterface + ): ?\Spameri\ElasticQuery\Query\LeafQueryInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -59,7 +59,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -69,25 +69,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Query/MustNotCollection.php b/src/Query/MustNotCollection.php index 1b10f4d..28c2345 100644 --- a/src/Query/MustNotCollection.php +++ b/src/Query/MustNotCollection.php @@ -25,7 +25,7 @@ public function __construct( public function add( \Spameri\ElasticQuery\Query\LeafQueryInterface $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -33,7 +33,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -47,7 +47,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Query\LeafQueryInterface + ): ?\Spameri\ElasticQuery\Query\LeafQueryInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -59,7 +59,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -69,25 +69,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Query/Nested.php b/src/Query/Nested.php new file mode 100644 index 0000000..15978cc --- /dev/null +++ b/src/Query/Nested.php @@ -0,0 +1,51 @@ +path = $path; + + if ($query === NULL) { + $query = new \Spameri\ElasticQuery\Query\QueryCollection(); + } + + $this->query = $query; + } + + + public function key(): string + { + return 'nested_' . $this->path; + } + + + public function toArray(): array + { + return [ + 'nested' => [ + 'path' => $this->path, + 'query' => [ + 'bool' => $this->query->toArray(), + ], + ], + ]; + } + + + public function getQuery(): \Spameri\ElasticQuery\Query\QueryCollection + { + return $this->query; + } + +} diff --git a/src/Query/PhrasePrefix.php b/src/Query/PhrasePrefix.php new file mode 100644 index 0000000..c6b96c7 --- /dev/null +++ b/src/Query/PhrasePrefix.php @@ -0,0 +1,49 @@ +field = $field; + $this->queryString = $queryString; + $this->boost = $boost; + $this->slop = $slop; + } + + + public function key(): string + { + return 'phrase_prefix_' . $this->field . '_' . $this->queryString; + } + + + public function toArray(): array + { + return [ + 'match_phrase_prefix' => [ + $this->field => [ + 'query' => $this->queryString, + 'boost' => $this->boost, + 'slop' => $this->slop, + ], + ], + ]; + } + +} diff --git a/src/Query/QueryCollection.php b/src/Query/QueryCollection.php index ed3566c..49b331d 100644 --- a/src/Query/QueryCollection.php +++ b/src/Query/QueryCollection.php @@ -27,11 +27,14 @@ class QueryCollection implements LeafQueryInterface private $key; + /** + * @param int|string|null $key + */ public function __construct( - ?\Spameri\ElasticQuery\Query\MustCollection $mustCollection = NULL - , ?\Spameri\ElasticQuery\Query\ShouldCollection $shouldCollection = NULL - , ?\Spameri\ElasticQuery\Query\MustNotCollection $mustNotCollection = NULL - , $key = NULL + $key = NULL, + ?\Spameri\ElasticQuery\Query\MustCollection $mustCollection = NULL, + ?\Spameri\ElasticQuery\Query\ShouldCollection $shouldCollection = NULL, + ?\Spameri\ElasticQuery\Query\MustNotCollection $mustNotCollection = NULL ) { if ( ! $mustCollection) { @@ -53,53 +56,53 @@ public function __construct( } - public function must() : \Spameri\ElasticQuery\Query\MustCollection + public function must(): \Spameri\ElasticQuery\Query\MustCollection { return $this->mustCollection; } - public function should() : \Spameri\ElasticQuery\Query\ShouldCollection + public function should(): \Spameri\ElasticQuery\Query\ShouldCollection { return $this->shouldCollection; } - public function mustNot() : \Spameri\ElasticQuery\Query\MustNotCollection + public function mustNot(): \Spameri\ElasticQuery\Query\MustNotCollection { return $this->mustNotCollection; } - public function addMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->mustCollection->add($leafQuery); } - public function addMustNotQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addMustNotQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->mustNotCollection->add($leafQuery); } - public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery) : void + public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void { $this->shouldCollection->add($leafQuery); } - public function key() : string + public function key(): string { if ($this->key) { - return $this->key; + return (string) $this->key; } return \md5(\serialize($this->toArray())); } - public function toArray() : array + public function toArray(): array { $array = []; /** @var \Spameri\ElasticQuery\Query\LeafQueryInterface $item */ diff --git a/src/Query/Range.php b/src/Query/Range.php index cd45483..da6a422 100644 --- a/src/Query/Range.php +++ b/src/Query/Range.php @@ -9,32 +9,30 @@ class Range implements LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; /** - * @var int|float|\DateTimeInterface|null + * @var int|float|string|\DateTimeInterface|null */ private $gte; /** - * @var int|float|\DateTimeInterface|null + * @var int|float|string|\DateTimeInterface|null */ private $lte; - /** - * @var float - */ - private $boost; + private float $boost; + /** + * @param int|float|string|\DateTimeInterface|null $gte + * @param int|float|string|\DateTimeInterface|null $lte + */ public function __construct( - string $field - , $gte = NULL - , $lte = NULL - , float $boost = 1.0 + string $field, + $gte = NULL, + $lte = NULL, + float $boost = 1.0 ) { if ($gte === NULL && $lte === NULL) { @@ -44,8 +42,22 @@ public function __construct( } if ($lte && $gte && $lte < $gte) { + if ($gte instanceof \DateTimeInterface) { + $gteValue = $gte->format('U'); + + } else { + $gteValue = $gte; + } + + if ($lte instanceof \DateTimeInterface) { + $lteValue = $lte->format('U'); + + } else { + $lteValue = $lte; + } + throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException( - 'Input values does not make range. From: ' . $gte . ' To: ' . $lte + 'Input values does not make range. From: ' . $gteValue . ' To: ' . $lteValue ); } @@ -56,7 +68,7 @@ public function __construct( } - public function key() : string + public function key(): string { $gte = $this->gte instanceof \DateTimeInterface ? $this->gte->format('Y-m-d H:i:s') : $this->gte; $lte = $this->lte instanceof \DateTimeInterface ? $this->lte->format('Y-m-d H:i:s') : $this->lte; @@ -65,7 +77,7 @@ public function key() : string } - public function toArray() : array + public function toArray(): array { $array = [ 'range' => [ @@ -76,11 +88,17 @@ public function toArray() : array ]; if ($this->gte !== NULL) { - $array['range'][$this->field]['gte'] = $this->gte instanceof \DateTimeInterface ? $this->gte->format('Y-m-d H:i:s') : $this->gte; + $array['range'][$this->field]['gte'] = + $this->gte instanceof \DateTimeInterface + ? $this->gte->format('Y-m-d H:i:s') + : $this->gte; } if ($this->lte !== NULL) { - $array['range'][$this->field]['lte'] = $this->lte instanceof \DateTimeInterface ? $this->lte->format('Y-m-d H:i:s') : $this->lte; + $array['range'][$this->field]['lte'] = + $this->lte instanceof \DateTimeInterface + ? $this->lte->format('Y-m-d H:i:s') + : $this->lte; } return $array; diff --git a/src/Query/ShouldCollection.php b/src/Query/ShouldCollection.php index d014551..60eb116 100644 --- a/src/Query/ShouldCollection.php +++ b/src/Query/ShouldCollection.php @@ -25,7 +25,7 @@ public function __construct( public function add( \Spameri\ElasticQuery\Query\LeafQueryInterface $item - ) : void + ): void { $this->collection[$item->key()] = $item; } @@ -33,7 +33,7 @@ public function add( public function remove( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { unset($this->collection[$key]); @@ -47,7 +47,7 @@ public function remove( public function get( string $key - ) : ?\Spameri\ElasticQuery\Query\LeafQueryInterface + ): ?\Spameri\ElasticQuery\Query\LeafQueryInterface { if (isset($this->collection[$key])) { return $this->collection[$key]; @@ -59,7 +59,7 @@ public function get( public function isValue( string $key - ) : bool + ): bool { if (isset($this->collection[$key])) { return TRUE; @@ -69,25 +69,25 @@ public function isValue( } - public function count() : int + public function count(): int { return \count($this->collection); } - public function keys() : array + public function keys(): array { return \array_map('\strval', \array_keys($this->collection)); } - public function clear() : void + public function clear(): void { $this->collection = []; } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->collection); } diff --git a/src/Query/Term.php b/src/Query/Term.php index 100656b..cce59f2 100644 --- a/src/Query/Term.php +++ b/src/Query/Term.php @@ -2,29 +2,25 @@ namespace Spameri\ElasticQuery\Query; - /** * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html */ class Term implements LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; /** - * @var string + * @var string|int|bool|float */ private $query; - /** - * @var float - */ - private $boost; + private float $boost; + /** + * @param string|int|bool|float $query + */ public function __construct( string $field , $query @@ -37,16 +33,15 @@ public function __construct( } - public function key() : string + public function key(): string { return 'term_' . $this->field . '_' . $this->query; } - public function toArray() : array + public function toArray(): array { - // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable - $array = [ + return [ 'term' => [ $this->field => [ 'value' => $this->query, @@ -54,8 +49,6 @@ public function toArray() : array ], ], ]; - - return $array; } } diff --git a/src/Query/Terms.php b/src/Query/Terms.php index 36dafa1..63c4ceb 100644 --- a/src/Query/Terms.php +++ b/src/Query/Terms.php @@ -9,20 +9,14 @@ class Terms implements LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; /** - * @var array + * @var array */ - private $query; + private array $query; - /** - * @var float - */ - private $boost; + private float $boost; public function __construct( @@ -43,23 +37,20 @@ public function __construct( } - public function key() : string + public function key(): string { return 'terms_' . $this->field . '_' . \implode('-', $this->query); } - public function toArray() : array + public function toArray(): array { - // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable - $array = [ + return [ 'terms' => [ $this->field => $this->query, 'boost' => $this->boost, ], ]; - - return $array; } } diff --git a/src/Query/WildCard.php b/src/Query/WildCard.php index d4d4eb3..e99df92 100644 --- a/src/Query/WildCard.php +++ b/src/Query/WildCard.php @@ -2,32 +2,22 @@ namespace Spameri\ElasticQuery\Query; - /** * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html */ class WildCard implements LeafQueryInterface { - /** - * @var string - */ - private $field; + private string $field; - /** - * @var string - */ - private $query; + private string $query; - /** - * @var float - */ - private $boost; + private float $boost; public function __construct( string $field - , $query + , string $query , float $boost = 1.0 ) { @@ -37,16 +27,15 @@ public function __construct( } - public function key() : string + public function key(): string { return 'wildcard_' . $this->field . '_' . $this->query; } - public function toArray() : array + public function toArray(): array { - // phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable - $array = [ + return [ 'wildcard' => [ $this->field => [ 'value' => $this->query, @@ -54,8 +43,6 @@ public function toArray() : array ], ], ]; - - return $array; } } diff --git a/src/Response/Result/Aggregation.php b/src/Response/Result/Aggregation.php index d0f208c..2751c2c 100644 --- a/src/Response/Result/Aggregation.php +++ b/src/Response/Result/Aggregation.php @@ -38,25 +38,25 @@ public function __construct( } - public function name() : string + public function name(): string { return $this->name; } - public function position() : int + public function position(): int { return $this->position; } - public function buckets() : \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection + public function buckets(): \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection { return $this->bucketCollection; } - public function countBuckets() : int + public function countBuckets(): int { $count = 0; /** @var \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket $bucket */ @@ -68,7 +68,7 @@ public function countBuckets() : int } - public function aggregations() : \Spameri\ElasticQuery\Response\Result\AggregationCollection + public function aggregations(): \Spameri\ElasticQuery\Response\Result\AggregationCollection { return $this->aggregations; } diff --git a/src/Response/Result/Aggregation/Bucket.php b/src/Response/Result/Aggregation/Bucket.php index 32cfe2b..30b01fb 100644 --- a/src/Response/Result/Aggregation/Bucket.php +++ b/src/Response/Result/Aggregation/Bucket.php @@ -30,8 +30,12 @@ class Bucket private $to; + /** + * @phpstan-param int|float|null $from + * @phpstan-param int|float|null $to + */ public function __construct( - $key + string $key , int $docCount , ?int $position = NULL , $from = NULL @@ -46,30 +50,36 @@ public function __construct( } - public function key() : string + public function key(): string { return (string) $this->key; } - public function docCount() : int + public function docCount(): int { return $this->docCount; } - public function position() : ?int + public function position(): ?int { return $this->position; } + /** + * @return int|float|null + */ public function from() { return $this->from; } + /** + * @return int|float|null + */ public function to() { return $this->to; diff --git a/src/Response/Result/Aggregation/BucketCollection.php b/src/Response/Result/Aggregation/BucketCollection.php index fde6865..d9012c0 100644 --- a/src/Response/Result/Aggregation/BucketCollection.php +++ b/src/Response/Result/Aggregation/BucketCollection.php @@ -20,7 +20,7 @@ public function __construct( } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->buckets); } diff --git a/src/Response/Result/AggregationCollection.php b/src/Response/Result/AggregationCollection.php index 9ea16c8..f8bc4e1 100644 --- a/src/Response/Result/AggregationCollection.php +++ b/src/Response/Result/AggregationCollection.php @@ -20,7 +20,7 @@ public function __construct( } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->aggregations); } diff --git a/src/Response/Result/BulkAction.php b/src/Response/Result/BulkAction.php index 18f3082..c3ef77f 100644 --- a/src/Response/Result/BulkAction.php +++ b/src/Response/Result/BulkAction.php @@ -83,61 +83,61 @@ public function __construct( } - public function action() : string + public function action(): string { return $this->action; } - public function index() : string + public function index(): string { return $this->index; } - public function type() : string + public function type(): string { return $this->type; } - public function id() : string + public function id(): string { return $this->id; } - public function version() : int + public function version(): int { return $this->version; } - public function result() : string + public function result(): string { return $this->result; } - public function shards() : \Spameri\ElasticQuery\Response\Shards + public function shards(): \Spameri\ElasticQuery\Response\Shards { return $this->shards; } - public function status() : int + public function status(): int { return $this->status; } - public function seqNo() : int + public function seqNo(): int { return $this->seqNo; } - public function primaryTerm() : int + public function primaryTerm(): int { return $this->primaryTerm; } diff --git a/src/Response/Result/BulkActionCollection.php b/src/Response/Result/BulkActionCollection.php index 553d086..ba8d171 100644 --- a/src/Response/Result/BulkActionCollection.php +++ b/src/Response/Result/BulkActionCollection.php @@ -20,7 +20,7 @@ public function __construct( } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->bulkActions); } diff --git a/src/Response/Result/Hit.php b/src/Response/Result/Hit.php index 801e08e..4426a6d 100644 --- a/src/Response/Result/Hit.php +++ b/src/Response/Result/Hit.php @@ -56,51 +56,73 @@ public function __construct( } - public function source() : array + public function source(): array { return $this->source; } + /** + * @phpstan-return mixed + */ public function getValue( string $key ) { - return $this->source[$key]; + $value = $this->source[$key] ?? NULL; + if ($value === NULL) { + return NULL; + } + + if (\strpos($key, '.') === FALSE) { + return $value; + } + + $levels = \explode('.', $key); + + foreach ($levels as $subKey) { + $value = $value[$subKey] ?? NULL; + + if ($value === NULL) { + return NULL; + } + } + + return $value; } - public function position() : int + public function position(): int { return $this->position; } - public function index() : string + public function index(): string { return $this->index; } - public function type() : string + public function type(): string { return $this->type; } - public function id() : string + public function id(): string { return $this->id; } - public function score() : float + public function score(): float { return $this->score; } - public function version() : int + public function version(): int { return $this->version; } diff --git a/src/Response/Result/HitCollection.php b/src/Response/Result/HitCollection.php index 26b3925..917d304 100644 --- a/src/Response/Result/HitCollection.php +++ b/src/Response/Result/HitCollection.php @@ -20,7 +20,7 @@ public function __construct( } - public function getIterator() : \ArrayIterator + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->hits); } diff --git a/src/Response/Result/Version.php b/src/Response/Result/Version.php index e796738..0dded47 100644 --- a/src/Response/Result/Version.php +++ b/src/Response/Result/Version.php @@ -47,7 +47,7 @@ class Version */ private $buildHash; /** - * @var string + * @var string|NULL */ private $buildDate; /** @@ -73,7 +73,7 @@ public function __construct( , ?string $buildFlavor , ?string $buildType , string $buildHash - , string $buildDate + , ?string $buildDate , bool $buildSnapshot , string $luceneVersion , ?string $minimumWireCompatibility @@ -95,7 +95,7 @@ public function __construct( public function convertVersionNumber( string $number - ) : int + ): int { $exploded = \explode('.', $number); @@ -113,61 +113,61 @@ public function convertVersionNumber( } - public function number() : string + public function number(): string { return $this->number; } - public function id() : int + public function id(): int { return $this->id; } - public function buildFlavor() : ?string + public function buildFlavor(): ?string { return $this->buildFlavor; } - public function buildType() : ?string + public function buildType(): ?string { return $this->buildType; } - public function buildHash() : string + public function buildHash(): string { return $this->buildHash; } - public function buildDate() : string + public function buildDate(): ?string { return $this->buildDate; } - public function buildSnapshot() : bool + public function buildSnapshot(): bool { return $this->buildSnapshot; } - public function luceneVersion() : string + public function luceneVersion(): string { return $this->luceneVersion; } - public function minimumWireCompatibility() : ?string + public function minimumWireCompatibility(): ?string { return $this->minimumWireCompatibility; } - public function minimumIndexCompatibility() : ?string + public function minimumIndexCompatibility(): ?string { return $this->minimumIndexCompatibility; } diff --git a/src/Response/ResultBulk.php b/src/Response/ResultBulk.php index d14696f..3eb15db 100644 --- a/src/Response/ResultBulk.php +++ b/src/Response/ResultBulk.php @@ -27,7 +27,7 @@ public function __construct( } - public function stats() : \Spameri\ElasticQuery\Response\Stats + public function stats(): \Spameri\ElasticQuery\Response\Stats { return $this->stats; } @@ -35,7 +35,7 @@ public function stats() : \Spameri\ElasticQuery\Response\Stats public function getFirstAction( string $id - ) : \Spameri\ElasticQuery\Response\Result\BulkAction + ): \Spameri\ElasticQuery\Response\Result\BulkAction { /** @var \Spameri\ElasticQuery\Response\Result\BulkAction $bulkIAction */ foreach ($this->bulkActionCollection as $bulkIAction) { diff --git a/src/Response/ResultMapper.php b/src/Response/ResultMapper.php index 8a06066..50142d4 100644 --- a/src/Response/ResultMapper.php +++ b/src/Response/ResultMapper.php @@ -8,7 +8,7 @@ class ResultMapper public function map( array $elasticSearchResponse - ) : ResultInterface + ): ResultInterface { if (isset($elasticSearchResponse['found'])) { $result = $this->mapSingleResult($elasticSearchResponse); @@ -23,7 +23,7 @@ public function map( $result = $this->mapVersionResults($elasticSearchResponse); } else { - throw new \Spameri\ElasticQuery\Exception\ResponseCouldNotBeMapped($elasticSearchResponse); + throw new \Spameri\ElasticQuery\Exception\ResponseCouldNotBeMapped((string) \json_encode($elasticSearchResponse)); } return $result; @@ -32,7 +32,7 @@ public function map( public function mapSingleResult( array $elasticSearchResponse - ) : ResultSingle + ): ResultSingle { return new ResultSingle( $this->mapHit($elasticSearchResponse, 0), @@ -43,7 +43,7 @@ public function mapSingleResult( public function mapBulkResult( array $elasticSearchResponse - ) : ResultBulk + ): ResultBulk { return new ResultBulk( $this->mapStats($elasticSearchResponse), @@ -54,7 +54,7 @@ public function mapBulkResult( public function mapVersionResults( array $elasticSearchResponse - ) : ResultVersion + ): ResultVersion { return new ResultVersion( $elasticSearchResponse['name'], @@ -65,7 +65,7 @@ public function mapVersionResults( $elasticSearchResponse['version']['build_flavor'] ?? NULL, $elasticSearchResponse['version']['build_type'] ?? NULL, $elasticSearchResponse['version']['build_hash'], - $elasticSearchResponse['version']['build_date'], + $elasticSearchResponse['version']['build_date'] ?? NULL, $elasticSearchResponse['version']['build_snapshot'], $elasticSearchResponse['version']['lucene_version'], $elasticSearchResponse['version']['minimum_wire_compatibility_version'] ?? NULL, @@ -78,7 +78,7 @@ public function mapVersionResults( public function mapSearchResults( array $elasticSearchResponse - ) : ResultSearch + ): ResultSearch { return new ResultSearch( $this->mapStats($elasticSearchResponse), @@ -91,7 +91,7 @@ public function mapSearchResults( public function mapHits( array $elasticSearchResponse - ) : \Spameri\ElasticQuery\Response\Result\HitCollection + ): \Spameri\ElasticQuery\Response\Result\HitCollection { $hits = []; foreach ($elasticSearchResponse['hits']['hits'] as $hitPosition => $hit) { @@ -107,7 +107,7 @@ public function mapHits( private function mapHit( array $hit , int $position - ) : \Spameri\ElasticQuery\Response\Result\Hit + ): \Spameri\ElasticQuery\Response\Result\Hit { return new \Spameri\ElasticQuery\Response\Result\Hit( $hit['_source'] ?? [], @@ -123,7 +123,7 @@ private function mapHit( public function mapBulkActions( array $elasticSearchResponse - ) : \Spameri\ElasticQuery\Response\Result\BulkActionCollection + ): \Spameri\ElasticQuery\Response\Result\BulkActionCollection { $bulkActions = []; foreach ($elasticSearchResponse as $actionType => $action) { @@ -139,7 +139,7 @@ public function mapBulkActions( public function mapBulkAction( array $bulkAction, string $actionType - ) : \Spameri\ElasticQuery\Response\Result\BulkAction + ): \Spameri\ElasticQuery\Response\Result\BulkAction { return new \Spameri\ElasticQuery\Response\Result\BulkAction( $actionType, @@ -158,7 +158,7 @@ public function mapBulkAction( public function mapAggregations( array $elasticSearchResponse - ) : \Spameri\ElasticQuery\Response\Result\AggregationCollection + ): \Spameri\ElasticQuery\Response\Result\AggregationCollection { $aggregationArray = []; $i = 0; @@ -180,7 +180,7 @@ private function mapAggregation( string $name , int $position , array $aggregationArray - ) : \Spameri\ElasticQuery\Response\Result\Aggregation + ): \Spameri\ElasticQuery\Response\Result\Aggregation { $i = 0; $buckets = []; @@ -209,6 +209,10 @@ private function mapAggregation( } if (isset($aggregationArray['doc_count']) && $aggregationArray['doc_count'] > 0) { + /** + * @var string $aggregationName + * @var array $aggregation + */ foreach ($aggregationArray as $aggregationName => $aggregation) { if ( ! \is_array($aggregation)) { continue; @@ -239,12 +243,12 @@ private function mapAggregation( private function mapBucket( - $bucketPosition + ?int $bucketPosition , array $bucketArray - ) : \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket + ): \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket { return new \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket( - $bucketArray['key'] ?? $bucketPosition, + $bucketArray['key'] ?? (string) $bucketPosition, $bucketArray['doc_count'], \is_int($bucketPosition) ? $bucketPosition : NULL, $bucketArray['from'] ?? NULL, @@ -255,7 +259,7 @@ private function mapBucket( public function mapStats( array $elasticSearchResponse - ) : Stats + ): Stats { $total = 0; if (\is_int($elasticSearchResponse['hits']['total'])) { @@ -275,7 +279,7 @@ public function mapStats( public function mapSingleStats( array $elasticSearchResponse - ) : StatsSingle + ): StatsSingle { return new StatsSingle( $elasticSearchResponse['version'] ?? 0, @@ -286,7 +290,7 @@ public function mapSingleStats( public function mapShards( array $elasticSearchResponse - ) : Shards + ): Shards { return new Shards( $elasticSearchResponse['_shards']['total'], diff --git a/src/Response/ResultSearch.php b/src/Response/ResultSearch.php index 1393c43..4af87dc 100644 --- a/src/Response/ResultSearch.php +++ b/src/Response/ResultSearch.php @@ -41,25 +41,25 @@ public function __construct( } - public function stats() : \Spameri\ElasticQuery\Response\Stats + public function stats(): \Spameri\ElasticQuery\Response\Stats { return $this->stats; } - public function shards() : \Spameri\ElasticQuery\Response\Shards + public function shards(): \Spameri\ElasticQuery\Response\Shards { return $this->shards; } - public function hits() : \Spameri\ElasticQuery\Response\Result\HitCollection + public function hits(): \Spameri\ElasticQuery\Response\Result\HitCollection { return $this->hitCollection; } - public function aggregations() : \Spameri\ElasticQuery\Response\Result\AggregationCollection + public function aggregations(): \Spameri\ElasticQuery\Response\Result\AggregationCollection { return $this->aggregationCollection; } @@ -67,7 +67,7 @@ public function aggregations() : \Spameri\ElasticQuery\Response\Result\Aggregati public function getHit( string $id - ) : \Spameri\ElasticQuery\Response\Result\Hit + ): \Spameri\ElasticQuery\Response\Result\Hit { /** @var \Spameri\ElasticQuery\Response\Result\Hit $hit */ foreach ($this->hitCollection as $hit) { @@ -84,7 +84,7 @@ public function getHit( public function getAggregation( string $name - ) : \Spameri\ElasticQuery\Response\Result\Aggregation + ): \Spameri\ElasticQuery\Response\Result\Aggregation { /** @var \Spameri\ElasticQuery\Response\Result\Aggregation $aggregation */ foreach ($this->aggregationCollection as $aggregation) { diff --git a/src/Response/ResultSingle.php b/src/Response/ResultSingle.php index 39d3e00..de10caf 100644 --- a/src/Response/ResultSingle.php +++ b/src/Response/ResultSingle.php @@ -26,13 +26,13 @@ public function __construct( } - public function hit() : \Spameri\ElasticQuery\Response\Result\Hit + public function hit(): \Spameri\ElasticQuery\Response\Result\Hit { return $this->hit; } - public function stats() : \Spameri\ElasticQuery\Response\StatsSingle + public function stats(): \Spameri\ElasticQuery\Response\StatsSingle { return $this->stats; } diff --git a/src/Response/ResultVersion.php b/src/Response/ResultVersion.php index eaf098c..1e6bb84 100644 --- a/src/Response/ResultVersion.php +++ b/src/Response/ResultVersion.php @@ -44,31 +44,31 @@ public function __construct( } - public function name() : string + public function name(): string { return $this->name; } - public function clusterName() : string + public function clusterName(): string { return $this->clusterName; } - public function clusterUUID() : string + public function clusterUUID(): string { return $this->clusterUUID; } - public function version() : \Spameri\ElasticQuery\Response\Result\Version + public function version(): \Spameri\ElasticQuery\Response\Result\Version { return $this->version; } - public function tagLine() : string + public function tagLine(): string { return $this->tagLine; } diff --git a/src/Response/Shards.php b/src/Response/Shards.php index 39dcb6b..9b9072c 100644 --- a/src/Response/Shards.php +++ b/src/Response/Shards.php @@ -38,25 +38,25 @@ public function __construct( } - public function total() : int + public function total(): int { return $this->total; } - public function successful() : int + public function successful(): int { return $this->successful; } - public function skipped() : int + public function skipped(): int { return $this->skipped; } - public function failed() : int + public function failed(): int { return $this->failed; } diff --git a/src/Response/Stats.php b/src/Response/Stats.php index 4d691ac..8ae1b35 100644 --- a/src/Response/Stats.php +++ b/src/Response/Stats.php @@ -32,19 +32,19 @@ public function __construct( } - public function took() : int + public function took(): int { return $this->took; } - public function timedOut() : bool + public function timedOut(): bool { return $this->timedOut; } - public function total() : int + public function total(): int { return $this->total; } diff --git a/src/Response/StatsSingle.php b/src/Response/StatsSingle.php index ab99e15..ad01811 100644 --- a/src/Response/StatsSingle.php +++ b/src/Response/StatsSingle.php @@ -26,13 +26,13 @@ public function __construct( } - public function version() : int + public function version(): int { return $this->version; } - public function found() : bool + public function found(): bool { return $this->found; } diff --git a/src/Value/BoolValue.php b/src/Value/AbstractBoolValue.php similarity index 66% rename from src/Value/BoolValue.php rename to src/Value/AbstractBoolValue.php index 0273607..ce8fb24 100644 --- a/src/Value/BoolValue.php +++ b/src/Value/AbstractBoolValue.php @@ -3,7 +3,7 @@ namespace Spameri\ElasticQuery\Value; -abstract class BoolValue implements \Spameri\ElasticQuery\Value\ValueInterface +abstract class AbstractBoolValue implements \Spameri\ElasticQuery\Value\ValueInterface { /** @@ -20,7 +20,7 @@ public function __construct( } - public function value() : bool + public function value(): bool { return $this->value; } diff --git a/src/Value/FloatValue.php b/src/Value/AbstractFloatValue.php similarity index 66% rename from src/Value/FloatValue.php rename to src/Value/AbstractFloatValue.php index 50e1e1e..4a4881f 100644 --- a/src/Value/FloatValue.php +++ b/src/Value/AbstractFloatValue.php @@ -3,7 +3,7 @@ namespace Spameri\ElasticQuery\Value; -abstract class FloatValue implements \Spameri\ElasticQuery\Value\ValueInterface +abstract class AbstractFloatValue implements \Spameri\ElasticQuery\Value\ValueInterface { /** @@ -20,7 +20,7 @@ public function __construct( } - public function value() : float + public function value(): float { return $this->value; } diff --git a/src/Value/NumberValue.php b/src/Value/AbstractNumberValue.php similarity index 65% rename from src/Value/NumberValue.php rename to src/Value/AbstractNumberValue.php index d037d85..95e5706 100644 --- a/src/Value/NumberValue.php +++ b/src/Value/AbstractNumberValue.php @@ -3,7 +3,7 @@ namespace Spameri\ElasticQuery\Value; -abstract class NumberValue implements \Spameri\ElasticQuery\Value\ValueInterface +abstract class AbstractNumberValue implements \Spameri\ElasticQuery\Value\ValueInterface { /** @@ -20,7 +20,7 @@ public function __construct( } - public function value() : int + public function value(): int { return $this->value; } diff --git a/src/Value/StringValue.php b/src/Value/AbstractStringValue.php similarity index 65% rename from src/Value/StringValue.php rename to src/Value/AbstractStringValue.php index 173b7e6..d9e43b8 100644 --- a/src/Value/StringValue.php +++ b/src/Value/AbstractStringValue.php @@ -3,7 +3,7 @@ namespace Spameri\ElasticQuery\Value; -abstract class StringValue implements \Spameri\ElasticQuery\Value\ValueInterface +abstract class AbstractStringValue implements \Spameri\ElasticQuery\Value\ValueInterface { /** @@ -20,7 +20,7 @@ public function __construct( } - public function value() : string + public function value(): string { return $this->value; } diff --git a/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt b/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt new file mode 100644 index 0000000..0efd0b0 --- /dev/null +++ b/tests/SpameriTests/ElasticQuery/ElasticQuery.phpt @@ -0,0 +1,151 @@ + [ + 'name' => [ + 'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_TEXT, + ], + 'year' => [ + 'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_INTEGER, + ] + ], + ]) + ); + + \curl_exec($ch); + \curl_close($ch); + } + + + public function testCreate() : void + { + $match = new \Spameri\ElasticQuery\Query\ElasticMatch( + 'name', + 'Avengers', + 1.0, + new \Spameri\ElasticQuery\Query\Match\Fuzziness( + \Spameri\ElasticQuery\Query\Match\Fuzziness::AUTO + ), + 2, + \Spameri\ElasticQuery\Query\Match\Operator::OR + ); + $term = new \Spameri\ElasticQuery\Query\Term( + 'name', + 'Avengers', + 1.0 + ); + + $document = new \Spameri\ElasticQuery\Document( + self::INDEX, + new \Spameri\ElasticQuery\Document\Body\Plain( + ( + new \Spameri\ElasticQuery\ElasticQuery( + new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, + new \Spameri\ElasticQuery\Query\MustCollection( + $match + ) + ), + new \Spameri\ElasticQuery\Filter\FilterCollection( + new \Spameri\ElasticQuery\Query\MustCollection( + $term + ) + ), + new \Spameri\ElasticQuery\Options\SortCollection( + new \Spameri\ElasticQuery\Options\Sort( + 'year', + \Spameri\ElasticQuery\Options\Sort::ASC + ) + ), + new \Spameri\ElasticQuery\Aggregation\AggregationCollection( + new \Spameri\ElasticQuery\Filter\FilterCollection( + new \Spameri\ElasticQuery\Query\MustCollection( + $term + ) + ), + new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection( + 'year_agg', + NULL, + new \Spameri\ElasticQuery\Aggregation\Term( + 'year' + ) + ) + ), + NULL, + NULL, + new \Spameri\ElasticQuery\Options( + 10, + 1 + ) + ) + )->toArray() + ) + ); + + $ch = \curl_init(); + \curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . $document->index() . '/_search'); + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET'); + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + \curl_setopt( + $ch, \CURLOPT_POSTFIELDS, + \json_encode($document->toArray()['body']) + ); + + \Tester\Assert::noError(static function () use ($ch) { + $response = \curl_exec($ch); + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ + $result = $resultMapper->map(\json_decode($response, TRUE)); + \Tester\Assert::type('int', $result->stats()->total()); + }); + + \curl_close($ch); + } + + + public function tearDown() : void + { + $ch = \curl_init(); + \curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX); + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE'); + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + + \curl_exec($ch); + \curl_close($ch); + } + +} + +(new ElasticQuery())->run(); diff --git a/tests/SpameriTests/ElasticQuery/Mapping/Analyzer/Custom/CzechDictionary.phpt b/tests/SpameriTests/ElasticQuery/Mapping/Analyzer/Custom/CzechDictionary.phpt index 35352fd..5bedec6 100644 --- a/tests/SpameriTests/ElasticQuery/Mapping/Analyzer/Custom/CzechDictionary.phpt +++ b/tests/SpameriTests/ElasticQuery/Mapping/Analyzer/Custom/CzechDictionary.phpt @@ -5,7 +5,10 @@ namespace SpameriTests\ElasticQuery\Mapping\Analyzer\Custom; require_once __DIR__ . '/../../../../bootstrap.php'; - +/** + * @skip + * Should be run only locally, dictionaries are not initialized on GHA yet. + */ class CzechDictionary extends \Tester\TestCase { @@ -20,13 +23,14 @@ class CzechDictionary extends \Tester\TestCase self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( $settings->toArray() - ) + ), + self::INDEX ); // Set up index and analyzer $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . $document->index()); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . $document->index() . '/'); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -35,7 +39,9 @@ class CzechDictionary extends \Tester\TestCase \json_encode($document->toArray()['body']) ); - \curl_exec($ch); + $indexCreating = \curl_exec($ch); + + \Tester\Assert::type('int', \strpos($indexCreating, '"acknowledged":true')); // Fetch settings and test if analyzer is configured @@ -45,20 +51,17 @@ class CzechDictionary extends \Tester\TestCase $responseSettings = \json_decode(\curl_exec($ch), TRUE); - $version = \SpameriTests\ElasticQuery\VersionCheck::check(); - if ($version->version()->id() > \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_7) { - \Tester\Assert::true(isset( - $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary'] - )); - \Tester\Assert::same( - 'dictionary_CZ', - $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary']['filter'][2] - ); - \Tester\Assert::same( - 'custom', - $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary']['type'] - ); - } + \Tester\Assert::true(isset( + $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary'] + )); + \Tester\Assert::same( + 'dictionary_CZ', + $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary']['filter'][2] + ); + \Tester\Assert::same( + 'custom', + $responseSettings[self::INDEX]['settings']['index']['analysis']['analyzer']['czechDictionary']['type'] + ); // Analyze text and test if output is as expected diff --git a/tests/SpameriTests/ElasticQuery/Query/Match.phpt b/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt similarity index 87% rename from tests/SpameriTests/ElasticQuery/Query/Match.phpt rename to tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt index 20c7ba2..c3de058 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Match.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt @@ -5,16 +5,16 @@ namespace SpameriTests\ElasticQuery\Query; require_once __DIR__ . '/../../bootstrap.php'; -class Match extends \Tester\TestCase +class ElasticMatch extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_match'; + private const INDEX = 'spameri_test_video_match'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -26,16 +26,16 @@ class Match extends \Tester\TestCase public function testCreate() : void { - $match = new \Spameri\ElasticQuery\Query\Match( + $match = new \Spameri\ElasticQuery\Query\ElasticMatch( 'name', 'Avengers', 1.0, - \Spameri\ElasticQuery\Query\Match\Operator::OR, new \Spameri\ElasticQuery\Query\Match\Fuzziness( \Spameri\ElasticQuery\Query\Match\Fuzziness::AUTO ), - 'standard', - 2 + 2, + \Spameri\ElasticQuery\Query\Match\Operator::OR, + 'standard' ); $array = $match->toArray(); @@ -49,18 +49,20 @@ class Match extends \Tester\TestCase \Tester\Assert::same(2, $array['match']['name']['minimum_should_match']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $match ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -88,7 +90,7 @@ class Match extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -99,4 +101,4 @@ class Match extends \Tester\TestCase } -(new Match())->run(); +(new ElasticMatch())->run(); diff --git a/tests/SpameriTests/ElasticQuery/Query/Fuzzy.phpt b/tests/SpameriTests/ElasticQuery/Query/Fuzzy.phpt index 88b4936..00b0cc4 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Fuzzy.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/Fuzzy.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class Fuzzy extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_fuzzy'; + private const INDEX = 'spameri_test_video_fuzzy'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -45,18 +45,20 @@ class Fuzzy extends \Tester\TestCase \Tester\Assert::same(100, $array['fuzzy']['name']['max_expansions']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $fuzzy ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -84,7 +86,7 @@ class Fuzzy extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Query/MatchPhrase.phpt b/tests/SpameriTests/ElasticQuery/Query/MatchPhrase.phpt index f913443..930f611 100644 --- a/tests/SpameriTests/ElasticQuery/Query/MatchPhrase.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/MatchPhrase.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class MatchPhrase extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_match_phrase'; + private const INDEX = 'spameri_test_video_match_phrase'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -43,18 +43,20 @@ class MatchPhrase extends \Tester\TestCase \Tester\Assert::same('standard', $array['match_phrase']['name']['analyzer']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $match ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -82,7 +84,7 @@ class MatchPhrase extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Query/Range.phpt b/tests/SpameriTests/ElasticQuery/Query/Range.phpt index b6ac57c..5bc6c24 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Range.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/Range.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class Range extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_range'; + private const INDEX = 'spameri_test_video_range'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -41,18 +41,20 @@ class Range extends \Tester\TestCase \Tester\Assert::same(1.0, $array['range']['id']['boost']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $range ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -80,7 +82,7 @@ class Range extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Query/Term.phpt b/tests/SpameriTests/ElasticQuery/Query/Term.phpt index ea515a0..c8651d9 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Term.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/Term.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class Term extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_term'; + private const INDEX = 'spameri_test_video_term'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -39,18 +39,20 @@ class Term extends \Tester\TestCase \Tester\Assert::same(1.0, $array['term']['name']['boost']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $term ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -78,7 +80,7 @@ class Term extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Query/Terms.phpt b/tests/SpameriTests/ElasticQuery/Query/Terms.phpt index 399ddb0..c2f3d00 100644 --- a/tests/SpameriTests/ElasticQuery/Query/Terms.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/Terms.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class Terms extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_terms'; + private const INDEX = 'spameri_test_video_terms'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -38,18 +38,20 @@ class Terms extends \Tester\TestCase \Tester\Assert::same(1.0, $array['terms']['boost']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $terms ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -77,7 +79,7 @@ class Terms extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Query/WildCard.phpt b/tests/SpameriTests/ElasticQuery/Query/WildCard.phpt index 87a2a7d..c41085f 100644 --- a/tests/SpameriTests/ElasticQuery/Query/WildCard.phpt +++ b/tests/SpameriTests/ElasticQuery/Query/WildCard.phpt @@ -8,13 +8,13 @@ require_once __DIR__ . '/../../bootstrap.php'; class WildCard extends \Tester\TestCase { - private const SPAMERI_VIDEO = 'spameri_test_video_wildcard'; + private const INDEX = 'spameri_test_video_wildcard'; public function setUp() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); @@ -39,18 +39,20 @@ class WildCard extends \Tester\TestCase \Tester\Assert::same(1.0, $array['wildcard']['name']['boost']); $document = new \Spameri\ElasticQuery\Document( - self::SPAMERI_VIDEO, + self::INDEX, new \Spameri\ElasticQuery\Document\Body\Plain( ( new \Spameri\ElasticQuery\ElasticQuery( new \Spameri\ElasticQuery\Query\QueryCollection( + NULL, new \Spameri\ElasticQuery\Query\MustCollection( $wildCard ) ) ) )->toArray() - ) + ), + self::INDEX ); $ch = curl_init(); @@ -78,7 +80,7 @@ class WildCard extends \Tester\TestCase public function tearDown() : void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::SPAMERI_VIDEO); + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX . '/'); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); diff --git a/tests/SpameriTests/ElasticQuery/Response/Result.phpt b/tests/SpameriTests/ElasticQuery/Response/Result.phpt index b01adb1..49cf52e 100644 --- a/tests/SpameriTests/ElasticQuery/Response/Result.phpt +++ b/tests/SpameriTests/ElasticQuery/Response/Result.phpt @@ -1,6 +1,6 @@ $resultObject->version()->id() ); + } elseif (\Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_2 <= $resultObject->version()->id()) { + \Tester\Assert::true( + \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_2 < $resultObject->version()->id() + ); + \Tester\Assert::true( + \Spameri\ElasticQuery\Response\Result\Version::ELASTIC_VERSION_ID_5 > $resultObject->version()->id() + ); + } else { \Tester\Assert::fail('ElasticSearch version did not match supported versions.'); } diff --git a/tests/SpameriTests/ElasticQuery/VersionCheck.php b/tests/SpameriTests/ElasticQuery/VersionCheck.php index 46bc9d4..6bb1f38 100644 --- a/tests/SpameriTests/ElasticQuery/VersionCheck.php +++ b/tests/SpameriTests/ElasticQuery/VersionCheck.php @@ -11,7 +11,7 @@ class VersionCheck private static $resultMapper; - public static function check() : \Spameri\ElasticQuery\Response\ResultVersion + public static function check(): \Spameri\ElasticQuery\Response\ResultVersion { if (self::$resultMapper === NULL) { self::$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); @@ -20,7 +20,7 @@ public static function check() : \Spameri\ElasticQuery\Response\ResultVersion /** @var \Spameri\ElasticQuery\Response\ResultVersion $resultObject */ $resultObject = self::$resultMapper->map( \json_decode( - \file_get_contents('http://127.0.0.1:9200'), + (string) \file_get_contents('http://127.0.0.1:9200'), TRUE ) ); diff --git a/travis-elastic.sh b/travis-elastic.sh deleted file mode 100644 index 0d6a14e..0000000 --- a/travis-elastic.sh +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/bash -set -ex - -setup_es() { - download_url=$1 - curl -sL $download_url > elasticsearch.tar.gz - mkdir elasticsearch - tar -xzf elasticsearch.tar.gz --strip-components=1 -C ./elasticsearch/. -} - -start_es() { - jhome=$1 - es_args=$2 - es_port=$3 - es_cluster=$4 - export JAVA_HOME=$jhome - elasticsearch/bin/elasticsearch $es_args > /tmp/$es_cluster.log & - sleep 20 - curl http://127.0.0.1:$es_port && echo "$es_cluster Elasticsearch is up!" || cat /tmp/$es_cluster.log ./elasticsearch/logs/$es_cluster.log - # curl http://127.0.0.1:$es_port && echo "ES is up!" || cat /tmp/$es_cluster.log ./elasticsearch/logs/$es_cluster.log -} - -start_es6() { - jhome=$1 - es_args=$2 - path_env=$3 - es_port=$4 - es_cluster=$5 - export JAVA_HOME=$jhome - ES_PATH_CONF=$path_env elasticsearch/bin/elasticsearch $es_args > /tmp/$es_cluster.log & - sleep 20 - curl http://127.0.0.1:$es_port && echo "$es_cluster Elasticsearch is up!" || cat /tmp/$es_cluster.log ./elasticsearch/logs/$es_cluster.log - # curl http://127.0.0.1:$es_port && echo "ES is up!" || cat /tmp/$es_cluster.log ./elasticsearch/logs/$es_cluster.log -} - -start_es7() { - es_args=$1 - path_env=$2 - es_port=$3 - es_cluster=$4 - ES_PATH_CONF=$path_env elasticsearch/bin/elasticsearch $es_args > /tmp/$es_cluster.log & - sleep 20 - curl http://127.0.0.1:$es_port && echo "$es_cluster Elasticsearch is up!" || cat /tmp/$es_cluster.log ./elasticsearch/logs/$es_cluster.log -} - -common_node_settings() { - major=$1 - minor=$2 - port=$3 - clustername=$4 - file=$5 - echo 'network.host: 127.0.0.1' > $file - echo "http.port: ${port}" >> $file - echo "cluster.name: ${clustername}" >> $file - echo "node.name: ${clustername}" >> $file - echo 'node.max_local_storage_nodes: 2' >> $file - if [[ $major -lt 7 ]]; then - echo "discovery.zen.ping.unicast.hosts: [\"127.0.0.1:${port}\"]" >> $file - else - transport=$(($port+100)) - echo "transport.port: ${transport}" >> $file - echo "discovery.seed_hosts: [\"localhost:${transport}\"]" >> $file - echo "discovery.type: single-node" >> $file - fi - if [[ $major -ge 6 ]] && [[ $minor -ge 3 ]]; then - echo 'xpack.monitoring.enabled: false' >> $file - echo 'node.ml: false' >> $file - echo 'xpack.security.enabled: false' >> $file - echo 'xpack.watcher.enabled: false' >> $file - fi -} - -setup_es https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$ES_VERSION.tar.gz - -java_home='/usr/lib/jvm/java-8-oracle' - -## Get major and minor version numbers -MAJORVER=$(echo $ES_VERSION | awk -F\. '{print $1}') -MINORVER=$(echo $ES_VERSION | awk -F\. '{print $2}') - -### Build local cluster config (since 5.4 removed most flags) -LC=elasticsearch/localcluster -mkdir -p $LC -cp elasticsearch/config/log4j2.properties $LC -cp elasticsearch/config/jvm.options $LC -common_node_settings $MAJORVER $MINORVER 9200 "local" "$LC/elasticsearch.yml" -echo 'path.repo: /' >> $LC/elasticsearch.yml -echo 'reindex.remote.whitelist: localhost:9201' >> $LC/elasticsearch.yml - - -### Build remote cluster config (since 5.4 removed most flags) -RC=elasticsearch/remotecluster -mkdir -p $RC -cp elasticsearch/config/log4j2.properties $RC -cp elasticsearch/config/jvm.options $RC -common_node_settings $MAJORVER $MINORVER 9201 remote "$RC/elasticsearch.yml" - -if [[ $MAJORVER -lt 6 ]]; then - start_es $java_home "-d -Epath.conf=$LC" 9200 "local" - start_es $java_home "-d -Epath.conf=$RC" 9201 "remote" -elif [[ $MARJORVER -eq 6 ]]; then - start_es6 $java_home " " "$LC" 9200 "local" - start_es6 $java_home " " "$RC" 9201 "remote" -else - start_es7 " " "$LC" 9200 "local" - start_es7 " " "$RC" 9201 "remote" -fi