Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!!! TASK: Adapt the package to Flow 7.0 interface changes #357

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ addons:
- openjdk-8-jre-headless
matrix:
include:
- php: 7.2
- php: 7.3
env: ES=6
- php: 7.2
- php: 7.3
env: ES=7
- php: 7.4
env: ES=6
Expand All @@ -23,7 +23,7 @@ cache:
- $HOME/.composer/cache

before_install:
- export NEOS_TARGET_VERSION=5.3
- export NEOS_TARGET_VERSION=6.0
- cd ..
- if [ "$ES" = 6 ]; then wget --no-check-certificate https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.6.tar.gz && tar xvfz elasticsearch-6.8.6.tar.gz && mv elasticsearch-6.8.6 elasticsearch; fi
- if [ "$ES" = 7 ]; then wget --no-check-certificate https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.0-linux-x86_64.tar.gz && tar xvfz elasticsearch-7.9.0-linux-x86_64.tar.gz && mv elasticsearch-7.9.0 elasticsearch; fi
Expand Down
44 changes: 25 additions & 19 deletions Classes/Eel/ElasticSearchQuery.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel;
Expand All @@ -15,8 +14,12 @@
*/

use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
use InvalidArgumentException;
use JsonException;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\QueryInterface;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Persistence\QueryResultInterface;

/**
* This ElasticSearchQuery object is just used inside ElasticSearchQueryResult->getQuery(), so that pagination
Expand Down Expand Up @@ -49,11 +52,12 @@ public function __construct(ElasticSearchQueryBuilder $elasticSearchQueryBuilder
*
* @param bool $cacheResult If the result cache should be used
* @return ElasticSearchQueryResult The query result
* @throws JsonException
* @api
*/
public function execute($cacheResult = false)
public function execute($cacheResult = false): QueryResultInterface
{
$queryHash = md5($this->queryBuilder->getIndexName() . json_encode($this->queryBuilder->getRequest()));
$queryHash = md5($this->queryBuilder->getIndexName() . json_encode($this->queryBuilder->getRequest(), JSON_THROW_ON_ERROR));
if ($cacheResult === true && isset(self::$runtimeQueryResultCache[$queryHash])) {
return self::$runtimeQueryResultCache[$queryHash];
}
Expand All @@ -66,57 +70,59 @@ public function execute($cacheResult = false)
/**
* {@inheritdoc}
*/
public function count()
public function count(): int
{
// FIXME Check that results are fetched!

return $this->queryBuilder->getTotalItems();
}

/**
* {@inheritdoc}
* @param int|null $limit
* @return QueryInterface
* @throws IllegalObjectTypeException
*/
public function setLimit($limit)
public function setLimit(?int $limit): QueryInterface
{
if ($limit < 1 || !is_int($limit)) {
throw new \InvalidArgumentException('Expecting integer greater than zero for limit');
throw new InvalidArgumentException('Expecting integer greater than zero for limit');
}

$this->queryBuilder->limit($limit);
return $this;
}

/**
* {@inheritdoc}
*/
public function getLimit()
public function getLimit(): int
{
return $this->queryBuilder->getLimit();
}

/**
* {@inheritdoc}
*/
public function setOffset($offset)
public function setOffset(?int $offset): QueryInterface
{
if ($offset < 1 || !is_int($offset)) {
throw new \InvalidArgumentException('Expecting integer greater than zero for offset');
throw new InvalidArgumentException('Expecting integer greater than zero for offset', 1605474906);
}

$this->queryBuilder->from($offset);
return $this;
}

/**
* {@inheritdoc}
*/
public function getOffset()
public function getOffset(): int
{
return $this->queryBuilder->getFrom();
}

/**
* {@inheritdoc}
*/
public function getType()
public function getType(): string
{
return NodeInterface::class;
}
Expand All @@ -125,7 +131,7 @@ public function getType()
* {@inheritdoc}
* @throws Exception
*/
public function setOrderings(array $orderings)
public function setOrderings(array $orderings): QueryInterface
{
throw new Exception(__FUNCTION__ . ' not implemented', 1421749035);
}
Expand All @@ -134,7 +140,7 @@ public function setOrderings(array $orderings)
* {@inheritdoc}
* @throws Exception
*/
public function getOrderings()
public function getOrderings(): array
{
throw new Exception(__FUNCTION__ . ' not implemented', 1421749036);
}
Expand All @@ -143,7 +149,7 @@ public function getOrderings()
* {@inheritdoc}
* @throws Exception
*/
public function matching($constraint)
public function matching($constraint): QueryInterface
{
throw new Exception(__FUNCTION__ . ' not implemented', 1421749037);
}
Expand Down Expand Up @@ -269,7 +275,7 @@ public function greaterThanOrEqual($propertyName, $operand)
* {@inheritdoc}
* @throws Exception
*/
public function setDistinct($distinct = true)
public function setDistinct(bool $distinct = true): QueryInterface
{
throw new Exception(__FUNCTION__ . ' not implemented', 1421749051);
}
Expand All @@ -278,7 +284,7 @@ public function setDistinct($distinct = true)
* {@inheritdoc}
* @throws Exception
*/
public function isDistinct()
public function isDistinct(): bool
{
throw new Exception(__FUNCTION__ . ' not implemented', 1421749052);
}
Expand Down
26 changes: 15 additions & 11 deletions Classes/Eel/ElasticSearchQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
use Flowpack\ElasticSearch\Domain\Model\Index;
use Flowpack\ElasticSearch\Domain\Model\Mapping;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\QueryResultInterface;
use Psr\Log\LoggerInterface;
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
use Neos\ContentRepository\Domain\Model\NodeInterface;
Expand Down Expand Up @@ -620,20 +619,22 @@ protected function evaluateResult(array $result): SearchResult
/**
* Get a query result object for lazy execution of the query
*
* @param bool $cacheResult
* @return ElasticSearchQueryResult
* @return \Traversable
* @throws \JsonException
* @api
*/
public function execute(): \Traversable
public function execute(bool $cacheResult = true): QueryResultInterface
{
$elasticSearchQuery = new ElasticSearchQuery($this);
return $elasticSearchQuery->execute(true);
return $elasticSearchQuery->execute($cacheResult);
}

/**
* Get a uncached query result object for lazy execution of the query
*
* @return ElasticSearchQueryResult
* @throws \JsonException
* @api
*/
public function executeUncached(): ElasticSearchQueryResult
Expand Down Expand Up @@ -662,7 +663,7 @@ public function count(): int
$treatedContent = $response->getTreatedContent();
$count = (int)$treatedContent['count'];

$this->logThisQuery && $this->logger->debug('Count Query Log (' . $this->logMessage . '): ' . 'Indexname: ' . $this->getIndexName() . ' ' . $request . ' -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count, LogEnvironment::fromMethodName(__METHOD__));
$this->logThisQuery && $this->logger->debug('Count Query Log (' . $this->logMessage . '): Indexname: ' . $this->getIndexName() . ' ' . $request . ' -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count, LogEnvironment::fromMethodName(__METHOD__));

return $count;
}
Expand All @@ -673,12 +674,13 @@ public function count(): int
* @param string $searchWord
* @param array $options Options to configure the query_string, see https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html
* @return QueryBuilderInterface
* @throws \JsonException
* @api
*/
public function fulltext(string $searchWord, array $options = []): QueryBuilderInterface
{
// We automatically enable result highlighting when doing fulltext searches. It is up to the user to use this information or not use it.
$this->request->fulltext(trim(json_encode($searchWord, JSON_UNESCAPED_UNICODE), '"'), $options);
$this->request->fulltext(trim(json_encode($searchWord, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE), '"'), $options);
$this->request->highlight(150, 2);

return $this;
Expand Down Expand Up @@ -926,8 +928,8 @@ public function cacheLifetime(): int
$minTimestamps = array_filter([
$this->getNearestFutureDate('neos_hidden_before_datetime'),
$this->getNearestFutureDate('neos_hidden_after_datetime')
], function ($value) {
return $value != 0;
], static function ($value) {
return $value !== 0;
});

if (empty($minTimestamps)) {
Expand All @@ -951,7 +953,7 @@ protected function getNearestFutureDate(string $dateField): int
{
$request = clone $this->request;

$convertDateResultToTimestamp = function (array $dateResult): int {
$convertDateResultToTimestamp = static function (array $dateResult): int {
if (!isset($dateResult['value_as_string'])) {
return 0;
}
Expand Down Expand Up @@ -1023,9 +1025,11 @@ protected function convertValue($value)
}

/**
* Retrieve the indexname
* Retrieve the indexName
*
* @return string
* @throws Exception
* @throws Exception\ConfigurationException
*/
public function getIndexName(): string
{
Expand Down
6 changes: 3 additions & 3 deletions Classes/Eel/ElasticSearchQueryResult.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel;
Expand All @@ -16,6 +15,7 @@

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Persistence\QueryInterface;
use Neos\Flow\Persistence\QueryResultInterface;

class ElasticSearchQueryResult implements QueryResultInterface, ProtectedContextAwareInterface
Expand Down Expand Up @@ -66,7 +66,7 @@ protected function initialize(): void
/**
* @return ElasticSearchQuery
*/
public function getQuery()
public function getQuery(): QueryInterface
{
return clone $this->elasticSearchQuery;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public function getFirst()
/**
* {@inheritdoc}
*/
public function toArray()
public function toArray(): array
{
$this->initialize();

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"homepage": "http://flowpack.org/",
"license": ["LGPL-3.0-only"],
"require": {
"php": "^7.2",
"php": "^7.3",
"ext-json": "*",

"flowpack/elasticsearch": " ^5.0 || dev-master",
"neos/content-repository": "^5.0",
"neos/content-repository-search": "^4.0 || dev-master",
"neos/eel": "^6.0",
"neos/flow": "^6.0",
"neos/flow": "^7.0",
"neos/fluid-adaptor": "^6.0",
"neos/utility-arrays": "^6.0"
},
Expand Down