first commit
This commit is contained in:
52
Classes/System/Meilisearch/Document/Document.php
Normal file
52
Classes/System/Meilisearch/Document/Document.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Document;
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
use RuntimeException;
|
||||
use Solarium\QueryType\Update\Query\Document as SolariumDocument;
|
||||
|
||||
/**
|
||||
* Document representing the update query document
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class Document extends SolariumDocument
|
||||
{
|
||||
/**
|
||||
* Magic call method used to emulate getters as used by the template engine.
|
||||
*
|
||||
* @param string $name method name
|
||||
* @param array $arguments method arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if (substr($name, 0, 3) == 'get') {
|
||||
$field = substr($name, 3);
|
||||
$field = strtolower($field[0]) . substr($field, 1);
|
||||
return $this->fields[$field] ?? null;
|
||||
} else {
|
||||
throw new RuntimeException('Call to undefined method. Supports magic getters only.', 1311006605);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldNames()
|
||||
{
|
||||
return array_keys($this->fields);
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
/**
|
||||
* This exception or a more specific one should be thrown when the is an error in the communication with the meilisearch server.
|
||||
*/
|
||||
class MeilisearchCommunicationException extends \RuntimeException {
|
||||
|
||||
/**
|
||||
* @var ResponseAdapter
|
||||
*/
|
||||
protected $meilisearchResponse;
|
||||
|
||||
/**
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function getMeilisearchResponse(): ResponseAdapter
|
||||
{
|
||||
return $this->meilisearchResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseAdapter $meilisearchResponse
|
||||
*/
|
||||
public function setMeilisearchResponse(ResponseAdapter $meilisearchResponse)
|
||||
{
|
||||
$this->meilisearchResponse = $meilisearchResponse;
|
||||
}
|
||||
}
|
250
Classes/System/Meilisearch/MeilisearchConnection.php
Normal file
250
Classes/System/Meilisearch/MeilisearchConnection.php
Normal file
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2009-2015 Ingo Renner <ingo@typo3.org>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use MeiliSearch\Client;
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SchemaParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\StopWordParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SynonymParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchAdminService;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchReadService;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchWriteService;
|
||||
use WapplerSystems\Meilisearch\Util;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestFactoryInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Meilisearch Service Access
|
||||
*
|
||||
* @author Ingo Renner <ingo@typo3.org>
|
||||
*/
|
||||
class MeilisearchConnection
|
||||
{
|
||||
/**
|
||||
* @var MeilisearchAdminService
|
||||
*/
|
||||
protected $adminService;
|
||||
|
||||
/**
|
||||
* @var MeilisearchReadService
|
||||
*/
|
||||
protected $readService;
|
||||
|
||||
/**
|
||||
* @var MeilisearchWriteService
|
||||
*/
|
||||
protected $writeService;
|
||||
|
||||
/**
|
||||
* @var TypoScriptConfiguration
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
/**
|
||||
* @var SynonymParser
|
||||
*/
|
||||
protected $synonymParser = null;
|
||||
|
||||
/**
|
||||
* @var StopWordParser
|
||||
*/
|
||||
protected $stopWordParser = null;
|
||||
|
||||
/**
|
||||
* @var SchemaParser
|
||||
*/
|
||||
protected $schemaParser = null;
|
||||
|
||||
/**
|
||||
* @var Client[]
|
||||
*/
|
||||
protected $nodes = [];
|
||||
|
||||
/**
|
||||
* @var MeilisearchLogManager
|
||||
*/
|
||||
protected $logger = null;
|
||||
|
||||
/**
|
||||
* @var ClientInterface[]
|
||||
*/
|
||||
protected $clients = [];
|
||||
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
protected $psr7Client;
|
||||
|
||||
/**
|
||||
* @var RequestFactoryInterface
|
||||
*/
|
||||
protected $requestFactory;
|
||||
|
||||
/**
|
||||
* @var StreamFactoryInterface
|
||||
*/
|
||||
protected $streamFactory;
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $eventDispatcher;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Client $readNode
|
||||
* @param Client $writeNode
|
||||
* @param ?TypoScriptConfiguration $configuration
|
||||
* @param ?SynonymParser $synonymParser
|
||||
* @param ?StopWordParser $stopWordParser
|
||||
* @param ?SchemaParser $schemaParser
|
||||
* @param ?MeilisearchLogManager $logManager
|
||||
* @param ?ClientInterface $psr7Client
|
||||
* @param ?RequestFactoryInterface $requestFactory
|
||||
* @param ?StreamFactoryInterface $streamFactory
|
||||
* @param ?EventDispatcherInterface $eventDispatcher
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function __construct(
|
||||
Client $readNode,
|
||||
Client $writeNode,
|
||||
TypoScriptConfiguration $configuration = null,
|
||||
SynonymParser $synonymParser = null,
|
||||
StopWordParser $stopWordParser = null,
|
||||
SchemaParser $schemaParser = null,
|
||||
MeilisearchLogManager $logManager = null,
|
||||
ClientInterface $psr7Client = null,
|
||||
EventDispatcherInterface $eventDispatcher = null
|
||||
) {
|
||||
$this->nodes['read'] = $readNode;
|
||||
$this->nodes['write'] = $writeNode;
|
||||
$this->nodes['admin'] = $writeNode;
|
||||
$this->configuration = $configuration ?? Util::getMeilisearchConfiguration();
|
||||
$this->synonymParser = $synonymParser;
|
||||
$this->stopWordParser = $stopWordParser;
|
||||
$this->schemaParser = $schemaParser;
|
||||
$this->logger = $logManager;
|
||||
$this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
|
||||
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Client
|
||||
*/
|
||||
public function getNode(string $key): Client
|
||||
{
|
||||
return $this->nodes[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchAdminService
|
||||
*/
|
||||
public function getAdminService(): MeilisearchAdminService
|
||||
{
|
||||
if ($this->adminService === null) {
|
||||
$this->adminService = $this->buildAdminService();
|
||||
}
|
||||
|
||||
return $this->adminService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchAdminService
|
||||
* @noinspection PhpIncompatibleReturnTypeInspection
|
||||
*/
|
||||
protected function buildAdminService(): MeilisearchAdminService
|
||||
{
|
||||
$endpointKey = 'admin';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchAdminService::class, $client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchReadService
|
||||
*/
|
||||
public function getReadService(): MeilisearchReadService
|
||||
{
|
||||
if ($this->readService === null) {
|
||||
$this->readService = $this->buildReadService();
|
||||
}
|
||||
|
||||
return $this->readService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchReadService
|
||||
* @noinspection PhpIncompatibleReturnTypeInspection
|
||||
*/
|
||||
protected function buildReadService(): MeilisearchReadService
|
||||
{
|
||||
$endpointKey = 'read';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchReadService::class, $client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchWriteService
|
||||
*/
|
||||
public function getWriteService(): MeilisearchWriteService
|
||||
{
|
||||
if ($this->writeService === null) {
|
||||
$this->writeService = $this->buildWriteService();
|
||||
}
|
||||
|
||||
return $this->writeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchWriteService
|
||||
* @noinspection PhpIncompatibleReturnTypeInspection
|
||||
*/
|
||||
protected function buildWriteService(): MeilisearchWriteService
|
||||
{
|
||||
$endpointKey = 'write';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchWriteService::class, $client);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
* @param ?string $endpointKey
|
||||
*/
|
||||
public function setClient(Client $client, ?string $endpointKey = 'read')
|
||||
{
|
||||
$this->clients[$endpointKey] = $client;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
/**
|
||||
* This exception should be thrown when the response from meilisearch was incomplete
|
||||
*/
|
||||
class MeilisearchIncompleteResponseException extends MeilisearchCommunicationException {}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
/**
|
||||
* This exception is used when the meilisearch an 500 internal server error is thrown by the meilisearch server
|
||||
*/
|
||||
class MeilisearchInternalServerErrorException extends MeilisearchCommunicationException {}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
/**
|
||||
* This exception is used when the meilisearch server is unavailable.
|
||||
*/
|
||||
class MeilisearchUnavailableException extends MeilisearchCommunicationException {}
|
103
Classes/System/Meilisearch/Parser/SchemaParser.php
Normal file
103
Classes/System/Meilisearch/Parser/SchemaParser.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Schema\Schema;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class to parse the schema from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class SchemaParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch stopwords response from an json string to an array.
|
||||
*
|
||||
* @param string $jsonString
|
||||
* @return Schema
|
||||
*/
|
||||
public function parseJson($jsonString)
|
||||
{
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
$schemaResponse = $decodedResponse->schema;
|
||||
|
||||
$schema = GeneralUtility::makeInstance(Schema::class);
|
||||
|
||||
if ($schemaResponse === null) {
|
||||
return $schema;
|
||||
}
|
||||
|
||||
$language = $this->parseLanguage($schemaResponse);
|
||||
$schema->setLanguage($language);
|
||||
|
||||
$name = $this->parseName($schemaResponse);
|
||||
$schema->setName($name);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the language from a meilisearch schema response.
|
||||
*
|
||||
* @param \stdClass $schema
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLanguage(\stdClass $schema)
|
||||
{
|
||||
$language = 'english';
|
||||
if (!is_object($schema) || !isset($schema->fieldTypes)) {
|
||||
return $language;
|
||||
}
|
||||
|
||||
foreach ($schema->fieldTypes as $fieldType) {
|
||||
if ($fieldType->name !== 'text') {
|
||||
continue;
|
||||
}
|
||||
// we have a text field
|
||||
foreach ($fieldType->queryAnalyzer->filters as $filter) {
|
||||
if ($filter->class === 'meilisearch.ManagedSynonymGraphFilterFactory') {
|
||||
$language = $filter->managed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the schema name from the response.
|
||||
*
|
||||
* @param \stdClass $schemaResponse
|
||||
* @return string
|
||||
*/
|
||||
protected function parseName(\stdClass $schemaResponse)
|
||||
{
|
||||
return isset($schemaResponse->name) ? $schemaResponse->name : '';
|
||||
}
|
||||
}
|
74
Classes/System/Meilisearch/Parser/StopWordParser.php
Normal file
74
Classes/System/Meilisearch/Parser/StopWordParser.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Class to parse the stopwords from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class StopWordParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch stopwords response from an json string to an array.
|
||||
*
|
||||
* @param string $jsonString
|
||||
* @return array
|
||||
*/
|
||||
public function parseJson($jsonString)
|
||||
{
|
||||
$stopWords = [];
|
||||
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
|
||||
if (isset($decodedResponse->wordSet->managedList)) {
|
||||
$stopWords = (array)$decodedResponse->wordSet->managedList;
|
||||
}
|
||||
|
||||
return $stopWords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $stopWords
|
||||
* @return string
|
||||
* @throws \Apache_Meilisearch_InvalidArgumentException
|
||||
*/
|
||||
public function toJson($stopWords)
|
||||
{
|
||||
if (empty($stopWords)) {
|
||||
throw new \Apache_Meilisearch_InvalidArgumentException('Must provide stop word.');
|
||||
}
|
||||
|
||||
if (is_string($stopWords)) {
|
||||
$stopWords = [$stopWords];
|
||||
}
|
||||
|
||||
$stopWords = array_values($stopWords);
|
||||
return json_encode($stopWords);
|
||||
}
|
||||
}
|
75
Classes/System/Meilisearch/Parser/SynonymParser.php
Normal file
75
Classes/System/Meilisearch/Parser/SynonymParser.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Class to parse the synonyms from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class SynonymParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch synonyms response from an json string to an array.
|
||||
*
|
||||
* @param string $baseWord
|
||||
* @param string $jsonString
|
||||
* @return array
|
||||
*/
|
||||
public function parseJson($baseWord, $jsonString)
|
||||
{
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
$synonyms = [];
|
||||
if (!empty($baseWord)) {
|
||||
if (is_array($decodedResponse->{$baseWord})) {
|
||||
$synonyms = $decodedResponse->{$baseWord};
|
||||
}
|
||||
} else {
|
||||
if (isset($decodedResponse->synonymMappings->managedMap)) {
|
||||
$synonyms = (array)$decodedResponse->synonymMappings->managedMap;
|
||||
}
|
||||
}
|
||||
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $baseWord
|
||||
* @param array $synonyms
|
||||
* @return string
|
||||
* @throws \Apache_Meilisearch_InvalidArgumentException
|
||||
*/
|
||||
public function toJson($baseWord, $synonyms)
|
||||
{
|
||||
if (empty($baseWord) || empty($synonyms)) {
|
||||
throw new \Apache_Meilisearch_InvalidArgumentException('Must provide base word and synonyms.');
|
||||
}
|
||||
|
||||
return json_encode([$baseWord => $synonyms]);
|
||||
}
|
||||
}
|
34
Classes/System/Meilisearch/ParsingUtil.php
Normal file
34
Classes/System/Meilisearch/ParsingUtil.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
/**
|
||||
* This class provides static helper functions that are helpful during the result parsing for meilisearch.
|
||||
*/
|
||||
class ParsingUtil
|
||||
{
|
||||
/**
|
||||
* This method is used to covert a array structure with json.nl=flat to have it as return with json.nl=map.
|
||||
*
|
||||
* @param $options
|
||||
* @return array
|
||||
*/
|
||||
public static function getMapArrayFromFlatArray(array $options): array
|
||||
{
|
||||
$keyValueMap = [];
|
||||
$valueFromKeyNode = -1;
|
||||
foreach($options as $key => $value) {
|
||||
$isKeyNode = (($key % 2) == 0);
|
||||
if ($isKeyNode) {
|
||||
$valueFromKeyNode = $value;
|
||||
} else {
|
||||
if($valueFromKeyNode == -1) {
|
||||
throw new \UnexpectedValueException('No optionValue before count value');
|
||||
}
|
||||
//we have a countNode
|
||||
$keyValueMap[$valueFromKeyNode] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $keyValueMap;
|
||||
}
|
||||
}
|
44
Classes/System/Meilisearch/RequestFactory.php
Normal file
44
Classes/System/Meilisearch/RequestFactory.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use TYPO3\CMS\Core\Http\RequestFactory as CoreRequestFactory;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
class RequestFactory extends CoreRequestFactory
|
||||
{
|
||||
protected $clientOptions = [];
|
||||
|
||||
/**
|
||||
* RequestFactory constructor.
|
||||
* @param array $clientOptions
|
||||
*/
|
||||
public function __construct(array $clientOptions)
|
||||
{
|
||||
$this->clientOptions = $clientOptions;
|
||||
}
|
||||
|
||||
public function request(string $uri, string $method = 'GET', array $options = []): ResponseInterface
|
||||
{
|
||||
/* @var GuzzleClient $client */
|
||||
$client = GeneralUtility::makeInstance(GuzzleClient::class, $this->clientOptions);
|
||||
return $client->request($method, $uri, $options);
|
||||
}
|
||||
}
|
150
Classes/System/Meilisearch/ResponseAdapter.php
Normal file
150
Classes/System/Meilisearch/ResponseAdapter.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Document\Document;
|
||||
use Countable;
|
||||
|
||||
/**
|
||||
* In EXT:meilisearch 9 we have switched from the MeilisearchPhpClient to the solarium api.
|
||||
*
|
||||
* In many places of the code the class Apache_Meilisearch_Response and the property Apache_Meilisearch_Response::reponse is used.
|
||||
* To be able to refactor this we need to have a replacement for Apache_Meilisearch_Response that behaves like the original class,
|
||||
* to keep the old code working. This allows us to drop the old code of MeilisearchPhpClient and refactore the other parts step by step.
|
||||
*
|
||||
* Class ResponseAdapter
|
||||
*
|
||||
* Search response
|
||||
*
|
||||
* @property \stdClass facet_counts
|
||||
* @property \stdClass facets
|
||||
* @property \stdClass spellcheck
|
||||
* @property \stdClass response
|
||||
* @property \stdClass responseHeader
|
||||
* @property \stdClass highlighting
|
||||
* @property \stdClass debug
|
||||
* @property \stdClass lucene
|
||||
* @property string file
|
||||
* @property array file_metadata
|
||||
*
|
||||
* Luke response
|
||||
*
|
||||
* @property \stdClass index
|
||||
* @property \stdClass fields
|
||||
*/
|
||||
class ResponseAdapter implements Countable
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $responseBody;
|
||||
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
protected $data = null;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $httpStatus = 200;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $httpStatusMessage = '';
|
||||
|
||||
/**
|
||||
* ResponseAdapter constructor.
|
||||
*
|
||||
* @param string $responseBody
|
||||
* @param int $httpStatus
|
||||
* @param string $httpStatusMessage
|
||||
*/
|
||||
public function __construct($responseBody, $httpStatus = 500, $httpStatusMessage = '')
|
||||
{
|
||||
$this->data = json_decode($responseBody);
|
||||
$this->responseBody = $responseBody;
|
||||
$this->httpStatus = $httpStatus;
|
||||
$this->httpStatusMessage = $httpStatusMessage;
|
||||
|
||||
// @extensionScannerIgnoreLine
|
||||
if (isset($this->data->response) && is_array($this->data->response->docs)) {
|
||||
$documents = array();
|
||||
|
||||
// @extensionScannerIgnoreLine
|
||||
foreach ($this->data->response->docs as $originalDocument) {
|
||||
$fields = get_object_vars($originalDocument);
|
||||
$document = new Document($fields);
|
||||
$documents[] = $document;
|
||||
}
|
||||
|
||||
// @extensionScannerIgnoreLine
|
||||
$this->data->response->docs = $documents;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic get to expose the parsed data and to lazily load it
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
if (isset($this->data->$key)) {
|
||||
return $this->data->$key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function for isset function on parsed data
|
||||
*
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return isset($this->data->$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParsedData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRawResponse()
|
||||
{
|
||||
return $this->responseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHttpStatus(): int
|
||||
{
|
||||
return $this->httpStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHttpStatusMessage(): string
|
||||
{
|
||||
return $this->httpStatusMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the elements of
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count(get_object_vars($this->data));
|
||||
}
|
||||
}
|
77
Classes/System/Meilisearch/Schema/Schema.php
Normal file
77
Classes/System/Meilisearch/Schema/Schema.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Schema;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Object representation of the meilisearch schema.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class Schema
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $language = 'english';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name = '';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLanguage()
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $language
|
||||
*/
|
||||
public function setLanguage($language)
|
||||
{
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
@@ -0,0 +1,451 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\PingFailedException;
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use WapplerSystems\Meilisearch\Util;
|
||||
use Solarium\Client;
|
||||
use Solarium\Core\Client\Endpoint;
|
||||
use Solarium\Core\Client\Request;
|
||||
use Solarium\Core\Query\QueryInterface;
|
||||
use Solarium\Exception\HttpException;
|
||||
use TYPO3\CMS\Core\Http\Uri;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
abstract class AbstractMeilisearchService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $pingCache = [];
|
||||
|
||||
/**
|
||||
* @var TypoScriptConfiguration
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
/**
|
||||
* @var \WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager
|
||||
*/
|
||||
protected $logger = null;
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
protected $client = null;
|
||||
|
||||
/**
|
||||
* MeilisearchReadService constructor.
|
||||
*/
|
||||
public function __construct(Client $client, $typoScriptConfiguration = null, $logManager = null)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->configuration = $typoScriptConfiguration ?? Util::getMeilisearchConfiguration();
|
||||
$this->logger = $logManager ?? GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the core meilisearch path + core path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCorePath()
|
||||
{
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
return is_null($endpoint) ? '' : $endpoint->getPath() .'/'. $endpoint->getCore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Solarium client
|
||||
*
|
||||
* @return ?Client
|
||||
*/
|
||||
public function getClient(): ?Client
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a valid http URL given this server's host, port and path and a provided servlet name
|
||||
*
|
||||
* @param string $servlet
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
protected function _constructUrl($servlet, $params = [])
|
||||
{
|
||||
$queryString = count($params) ? '?' . http_build_query($params, null, '&') : '';
|
||||
return $this->__toString() . $servlet . $queryString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string representation of the Meilisearch connection. Specifically
|
||||
* will return the Meilisearch URL.
|
||||
*
|
||||
* @return string The Meilisearch URL.
|
||||
* @TODO: Add support for API version 2
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
if (!$endpoint instanceof Endpoint) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return $endpoint->getCoreBaseUri();
|
||||
} catch (\Exception $exception) {
|
||||
}
|
||||
return $endpoint->getScheme(). '://' . $endpoint->getHost() . ':' . $endpoint->getPort() . $endpoint->getPath() . '/' . $endpoint->getCore() . '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Endpoint|null
|
||||
*/
|
||||
public function getPrimaryEndpoint()
|
||||
{
|
||||
return is_array($this->client->getEndpoints()) ? reset($this->client->getEndpoints()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a get operation against this Meilisearch Server
|
||||
*
|
||||
* @param string $url
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawGet($url)
|
||||
{
|
||||
return $this->_sendRawRequest($url, Request::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a HTTP DELETE operation against the Meilisearch server
|
||||
*
|
||||
* @param string $url
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawDelete($url)
|
||||
{
|
||||
return $this->_sendRawRequest($url, Request::METHOD_DELETE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a post operation against this Meilisearch Server
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $rawPost
|
||||
* @param string $contentType
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawPost($url, $rawPost, $contentType = 'text/xml; charset=UTF-8')
|
||||
{
|
||||
$initializeRequest = function(Request $request) use ($rawPost, $contentType) {
|
||||
$request->setRawData($rawPost);
|
||||
$request->addHeader('Content-Type: ' . $contentType);
|
||||
return $request;
|
||||
};
|
||||
|
||||
return $this->_sendRawRequest($url, Request::METHOD_POST, $rawPost, $initializeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that performs an http request with the solarium client.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param string $body
|
||||
* @param ?\Closure $initializeRequest
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawRequest(
|
||||
string $url,
|
||||
$method = Request::METHOD_GET,
|
||||
$body = '',
|
||||
\Closure $initializeRequest = null
|
||||
) {
|
||||
$logSeverity = MeilisearchLogManager::INFO;
|
||||
$exception = null;
|
||||
$url = $this->reviseUrl($url);
|
||||
try {
|
||||
$request = $this->buildSolariumRequestFromUrl($url, $method);
|
||||
if($initializeRequest !== null) {
|
||||
$request = $initializeRequest($request);
|
||||
}
|
||||
$response = $this->executeRequest($request);
|
||||
} catch (HttpException $exception) {
|
||||
$logSeverity = MeilisearchLogManager::ERROR;
|
||||
$response = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getMessage());
|
||||
}
|
||||
|
||||
if ($this->configuration->getLoggingQueryRawPost() || $response->getHttpStatus() != 200) {
|
||||
$message = 'Querying Meilisearch using '.$method;
|
||||
$this->writeLog($logSeverity, $message, $url, $response, $exception, $body);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revise url
|
||||
* - Resolve relative paths
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
protected function reviseUrl(string $url): string
|
||||
{
|
||||
/* @var Uri $uri */
|
||||
$uri = GeneralUtility::makeInstance(Uri::class, $url);
|
||||
|
||||
if ((string)$uri->getPath() === '') {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$path = trim($uri->getPath(), '/');
|
||||
$pathsCurrent = explode('/', $path);
|
||||
$pathNew = [];
|
||||
foreach ($pathsCurrent as $pathCurrent) {
|
||||
if ($pathCurrent === '..') {
|
||||
array_pop($pathNew);
|
||||
continue;
|
||||
}
|
||||
if ($pathCurrent === '.') {
|
||||
continue;
|
||||
}
|
||||
$pathNew[] = $pathCurrent;
|
||||
}
|
||||
|
||||
$uri = $uri->withPath(implode('/', $pathNew));
|
||||
return (string)$uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the log data and writes the message to the log
|
||||
*
|
||||
* @param integer $logSeverity
|
||||
* @param string $message
|
||||
* @param string $url
|
||||
* @param ResponseAdapter $meilisearchResponse
|
||||
* @param ?\Exception $exception
|
||||
* @param string $contentSend
|
||||
*/
|
||||
protected function writeLog($logSeverity, $message, $url, $meilisearchResponse, $exception = null, $contentSend = '')
|
||||
{
|
||||
$logData = $this->buildLogDataFromResponse($meilisearchResponse, $exception, $url, $contentSend);
|
||||
$this->logger->log($logSeverity, $message, $logData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the meilisearch information to build data for the logger.
|
||||
*
|
||||
* @param ResponseAdapter $meilisearchResponse
|
||||
* @param ?\Exception $e
|
||||
* @param string $url
|
||||
* @param string $contentSend
|
||||
* @return array
|
||||
*/
|
||||
protected function buildLogDataFromResponse(ResponseAdapter $meilisearchResponse, \Exception $e = null, $url = '', $contentSend = '')
|
||||
{
|
||||
$logData = ['query url' => $url, 'response' => (array)$meilisearchResponse];
|
||||
|
||||
if ($contentSend !== '') {
|
||||
$logData['content'] = $contentSend;
|
||||
}
|
||||
|
||||
if (!empty($e)) {
|
||||
$logData['exception'] = $e->__toString();
|
||||
return $logData;
|
||||
} else {
|
||||
// trigger data parsing
|
||||
// @extensionScannerIgnoreLine
|
||||
$meilisearchResponse->response;
|
||||
$logData['response data'] = print_r($meilisearchResponse, true);
|
||||
return $logData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the /admin/ping servlet, can be used to quickly tell if a connection to the
|
||||
* server is available.
|
||||
*
|
||||
* Simply overrides the MeilisearchPhpClient implementation, changing ping from a
|
||||
* HEAD to a GET request, see http://forge.typo3.org/issues/44167
|
||||
*
|
||||
* Also does not report the time, see https://forge.typo3.org/issues/64551
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return bool TRUE if Meilisearch can be reached, FALSE if not
|
||||
*/
|
||||
public function ping($useCache = true)
|
||||
{
|
||||
try {
|
||||
$httpResponse = $this->performPingRequest($useCache);
|
||||
} catch (HttpException $exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ($httpResponse->getHttpStatus() === 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the /admin/ping servlet, can be used to get the runtime of a ping request.
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return double runtime in milliseconds
|
||||
* @throws \WapplerSystems\Meilisearch\PingFailedException
|
||||
*/
|
||||
public function getPingRoundTripRuntime($useCache = true)
|
||||
{
|
||||
try {
|
||||
$start = $this->getMilliseconds();
|
||||
$httpResponse = $this->performPingRequest($useCache);
|
||||
$end = $this->getMilliseconds();
|
||||
} catch (HttpException $e) {
|
||||
$message = 'Meilisearch ping failed with unexpected response code: ' . $e->getCode();
|
||||
/** @var $exception \WapplerSystems\Meilisearch\PingFailedException */
|
||||
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message);
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($httpResponse->getHttpStatus() !== 200) {
|
||||
$message = 'Meilisearch ping failed with unexpected response code: ' . $httpResponse->getHttpStatus();
|
||||
/** @var $exception \WapplerSystems\Meilisearch\PingFailedException */
|
||||
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message);
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return $end - $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a ping request and returns the result.
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function performPingRequest($useCache = true)
|
||||
{
|
||||
$cacheKey = (string)($this);
|
||||
if ($useCache && isset(static::$pingCache[$cacheKey])) {
|
||||
return static::$pingCache[$cacheKey];
|
||||
}
|
||||
|
||||
$pingQuery = $this->client->createPing();
|
||||
$pingResult = $this->createAndExecuteRequest($pingQuery);
|
||||
|
||||
if ($useCache) {
|
||||
static::$pingCache[$cacheKey] = $pingResult;
|
||||
}
|
||||
|
||||
return $pingResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current time in milliseconds.
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
protected function getMilliseconds()
|
||||
{
|
||||
return GeneralUtility::milliseconds();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryInterface $query
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function createAndExecuteRequest(QueryInterface $query): ResponseAdapter
|
||||
{
|
||||
$request = $this->client->createRequest($query);
|
||||
return $this->executeRequest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function executeRequest($request): ResponseAdapter
|
||||
{
|
||||
$result = $this->client->executeRequest($request);
|
||||
return new ResponseAdapter($result->getBody(), $result->getStatusCode(), $result->getStatusMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the request for Solarium.
|
||||
*
|
||||
* Important: The endpoint already contains the API information.
|
||||
* The internal Solarium will append the information including the core if set.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $httpMethod
|
||||
* @return Request
|
||||
*/
|
||||
protected function buildSolariumRequestFromUrl(string $url, $httpMethod = Request::METHOD_GET): Request
|
||||
{
|
||||
$params = [];
|
||||
parse_str(parse_url($url, PHP_URL_QUERY), $params);
|
||||
$request = new Request();
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
$api = $request->getApi() === Request::API_V1 ? 'meilisearch' : 'api';
|
||||
$coreBasePath = $endpoint->getPath() . '/' . $api . '/' . $endpoint->getCore() . '/';
|
||||
|
||||
$handler = $this->buildRelativePath($coreBasePath, $path);
|
||||
$request->setMethod($httpMethod);
|
||||
$request->setParams($params);
|
||||
$request->setHandler($handler);
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a relative path from base path to target path.
|
||||
* Required since Solarium contains the core information
|
||||
*
|
||||
* @param string $basePath
|
||||
* @param string $targetPath
|
||||
* @return string
|
||||
*/
|
||||
protected function buildRelativePath(string $basePath, string $targetPath): string
|
||||
{
|
||||
$basePath = trim($basePath, '/');
|
||||
$targetPath = trim($targetPath, '/');
|
||||
$baseElements = explode('/', $basePath);
|
||||
$targetElements = explode('/', $targetPath);
|
||||
$targetSegment = array_pop($targetElements);
|
||||
foreach ($baseElements as $i => $segment) {
|
||||
if (isset($targetElements[$i]) && $segment === $targetElements[$i]) {
|
||||
unset($baseElements[$i], $targetElements[$i]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$targetElements[] = $targetSegment;
|
||||
return str_repeat('../', count($baseElements)) . implode('/', $targetElements);
|
||||
}
|
||||
}
|
390
Classes/System/Meilisearch/Service/MeilisearchAdminService.php
Normal file
390
Classes/System/Meilisearch/Service/MeilisearchAdminService.php
Normal file
@@ -0,0 +1,390 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SchemaParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\StopWordParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SynonymParser;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Schema\Schema;
|
||||
use Solarium\Client;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class MeilisearchAdminService
|
||||
*/
|
||||
class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
{
|
||||
const PLUGINS_SERVLET = 'admin/plugins';
|
||||
const LUKE_SERVLET = 'admin/luke';
|
||||
const SYSTEM_SERVLET = 'admin/system';
|
||||
const CORES_SERVLET = '../admin/cores';
|
||||
const FILE_SERVLET = 'admin/file';
|
||||
const SCHEMA_SERVLET = 'schema';
|
||||
const SYNONYMS_SERVLET = 'schema/analysis/synonyms/';
|
||||
const STOPWORDS_SERVLET = 'schema/analysis/stopwords/';
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lukeData = [];
|
||||
|
||||
protected $systemData = null;
|
||||
|
||||
protected $pluginsData = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $meilisearchconfigName;
|
||||
|
||||
/**
|
||||
* @var SchemaParser
|
||||
*/
|
||||
protected $schemaParser = null;
|
||||
|
||||
/**
|
||||
* @var Schema
|
||||
*/
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_synonymsUrl;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_stopWordsUrl;
|
||||
|
||||
/**
|
||||
* @var SynonymParser
|
||||
*/
|
||||
protected $synonymParser = null;
|
||||
|
||||
/**
|
||||
* @var StopWordParser
|
||||
*/
|
||||
protected $stopWordParser = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param TypoScriptConfiguration $typoScriptConfiguration
|
||||
* @param SynonymParser $synonymParser
|
||||
* @param StopWordParser $stopWordParser
|
||||
* @param SchemaParser $schemaParser
|
||||
* @param MeilisearchLogManager $logManager
|
||||
*/
|
||||
public function __construct(
|
||||
Client $client,
|
||||
TypoScriptConfiguration $typoScriptConfiguration = null,
|
||||
MeilisearchLogManager $logManager = null,
|
||||
SynonymParser $synonymParser = null,
|
||||
StopWordParser $stopWordParser = null,
|
||||
SchemaParser $schemaParser = null
|
||||
)
|
||||
{
|
||||
parent::__construct($client, $typoScriptConfiguration);
|
||||
|
||||
$this->synonymParser = $synonymParser ?? GeneralUtility::makeInstance(SynonymParser::class);
|
||||
$this->stopWordParser = $stopWordParser ?? GeneralUtility::makeInstance(StopWordParser::class);
|
||||
$this->schemaParser = $schemaParser ?? GeneralUtility::makeInstance(SchemaParser::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the /admin/system servlet and retrieve system information about Meilisearch
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function system()
|
||||
{
|
||||
return $this->_sendRawGet($this->_constructUrl(self::SYSTEM_SERVLET, ['wt' => 'json']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information about the plugins installed in Meilisearch
|
||||
*
|
||||
* @return array A nested array of plugin data.
|
||||
*/
|
||||
public function getPluginsInformation()
|
||||
{
|
||||
if (count($this->pluginsData) == 0) {
|
||||
$url = $this->_constructUrl(self::PLUGINS_SERVLET, ['wt' => 'json']);
|
||||
$pluginsInformation = $this->_sendRawGet($url);
|
||||
|
||||
// access a random property to trigger response parsing
|
||||
$pluginsInformation->responseHeader;
|
||||
$this->pluginsData = $pluginsInformation;
|
||||
}
|
||||
|
||||
return $this->pluginsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* get field meta data for the index
|
||||
*
|
||||
* @param int $numberOfTerms Number of top terms to fetch for each field
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function getFieldsMetaData($numberOfTerms = 0)
|
||||
{
|
||||
return $this->getLukeMetaData($numberOfTerms)->fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves meta data about the index from the luke request handler
|
||||
*
|
||||
* @param int $numberOfTerms Number of top terms to fetch for each field
|
||||
* @return ResponseAdapter Index meta data
|
||||
*/
|
||||
public function getLukeMetaData($numberOfTerms = 0)
|
||||
{
|
||||
if (!isset($this->lukeData[$numberOfTerms])) {
|
||||
$lukeUrl = $this->_constructUrl(
|
||||
self::LUKE_SERVLET, ['numTerms' => $numberOfTerms, 'wt' => 'json', 'fl' => '*']
|
||||
);
|
||||
|
||||
$this->lukeData[$numberOfTerms] = $this->_sendRawGet($lukeUrl);
|
||||
}
|
||||
|
||||
return $this->lukeData[$numberOfTerms];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information about the Meilisearch server
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function getSystemInformation()
|
||||
{
|
||||
if (empty($this->systemData)) {
|
||||
$systemInformation = $this->system();
|
||||
|
||||
// access a random property to trigger response parsing
|
||||
$systemInformation->responseHeader;
|
||||
$this->systemData = $systemInformation;
|
||||
}
|
||||
|
||||
return $this->systemData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the meilisearchconfig.xml file installed and in use on the Meilisearch
|
||||
* server.
|
||||
*
|
||||
* @return string Name of the active meilisearchconfig.xml
|
||||
*/
|
||||
public function getMeilisearchconfigName()
|
||||
{
|
||||
if (is_null($this->meilisearchconfigName)) {
|
||||
$meilisearchconfigXmlUrl = $this->_constructUrl(self::FILE_SERVLET, ['file' => 'meilisearchconfig.xml']);
|
||||
$response = $this->_sendRawGet($meilisearchconfigXmlUrl);
|
||||
$meilisearchconfigXml = simplexml_load_string($response->getRawResponse());
|
||||
if ($meilisearchconfigXml === false) {
|
||||
throw new \InvalidArgumentException('No valid xml response from schema file: ' . $meilisearchconfigXmlUrl);
|
||||
}
|
||||
$this->meilisearchconfigName = (string)$meilisearchconfigXml->attributes()->name;
|
||||
}
|
||||
|
||||
return $this->meilisearchconfigName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Meilisearch server's version number.
|
||||
*
|
||||
* @return string Meilisearch version number
|
||||
*/
|
||||
public function getMeilisearchServerVersion()
|
||||
{
|
||||
$systemInformation = $this->getSystemInformation();
|
||||
// don't know why $systemInformation->lucene->meilisearch-spec-version won't work
|
||||
$luceneInformation = (array)$systemInformation->lucene;
|
||||
return $luceneInformation['meilisearch-spec-version'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the current core
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function reloadCore()
|
||||
{
|
||||
$response = $this->reloadCoreByName($this->getPrimaryEndpoint()->getCore());
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads a core of the connection by a given corename.
|
||||
*
|
||||
* @param string $coreName
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function reloadCoreByName($coreName)
|
||||
{
|
||||
$coreAdminReloadUrl = $this->_constructUrl(self::CORES_SERVLET) . '?action=reload&core=' . $coreName;
|
||||
$response = $this->_sendRawGet($coreAdminReloadUrl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configured schema for the current core.
|
||||
*
|
||||
* @return Schema
|
||||
*/
|
||||
public function getSchema()
|
||||
{
|
||||
if ($this->schema !== null) {
|
||||
return $this->schema;
|
||||
}
|
||||
$response = $this->_sendRawGet($this->_constructUrl(self::SCHEMA_SERVLET));
|
||||
|
||||
$this->schema = $this->schemaParser->parseJson($response->getRawResponse());
|
||||
return $this->schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currently configured synonyms
|
||||
*
|
||||
* @param string $baseWord If given a base word, retrieves the synonyms for that word only
|
||||
* @return array
|
||||
*/
|
||||
public function getSynonyms($baseWord = '')
|
||||
{
|
||||
$this->initializeSynonymsUrl();
|
||||
$synonymsUrl = $this->_synonymsUrl;
|
||||
if (!empty($baseWord)) {
|
||||
$synonymsUrl .= '/' . $baseWord;
|
||||
}
|
||||
|
||||
$response = $this->_sendRawGet($synonymsUrl);
|
||||
return $this->synonymParser->parseJson($baseWord, $response->getRawResponse());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add list of synonyms for base word to managed synonyms map
|
||||
*
|
||||
* @param string $baseWord
|
||||
* @param array $synonyms
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*
|
||||
* @throws \InvalidArgumentException If $baseWord or $synonyms are empty
|
||||
*/
|
||||
public function addSynonym($baseWord, array $synonyms)
|
||||
{
|
||||
$this->initializeSynonymsUrl();
|
||||
$json = $this->synonymParser->toJson($baseWord, $synonyms);
|
||||
$response = $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a synonym from the synonyms map
|
||||
*
|
||||
* @param string $baseWord
|
||||
* @return ResponseAdapter
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function deleteSynonym($baseWord)
|
||||
{
|
||||
$this->initializeSynonymsUrl();
|
||||
if (empty($baseWord)) {
|
||||
throw new \InvalidArgumentException('Must provide base word.');
|
||||
}
|
||||
|
||||
$response = $this->_sendRawDelete($this->_synonymsUrl . '/' . urlencode($baseWord));
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currently configured stop words
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getStopWords()
|
||||
{
|
||||
$this->initializeStopWordsUrl();
|
||||
$response = $this->_sendRawGet($this->_stopWordsUrl);
|
||||
return $this->stopWordParser->parseJson($response->getRawResponse());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds stop words to the managed stop word list
|
||||
*
|
||||
* @param array|string $stopWords string for a single word, array for multiple words
|
||||
* @return ResponseAdapter
|
||||
* @throws \InvalidArgumentException If $stopWords is empty
|
||||
*/
|
||||
public function addStopWords($stopWords)
|
||||
{
|
||||
$this->initializeStopWordsUrl();
|
||||
$json = $this->stopWordParser->toJson($stopWords);
|
||||
return $this->_sendRawPost($this->_stopWordsUrl, $json, 'application/json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a words from the managed stop word list
|
||||
*
|
||||
* @param string $stopWord stop word to delete
|
||||
* @return ResponseAdapter
|
||||
* @throws \InvalidArgumentException If $stopWords is empty
|
||||
*/
|
||||
public function deleteStopWord($stopWord)
|
||||
{
|
||||
$this->initializeStopWordsUrl();
|
||||
if (empty($stopWord)) {
|
||||
throw new \InvalidArgumentException('Must provide stop word.');
|
||||
}
|
||||
|
||||
return $this->_sendRawDelete($this->_stopWordsUrl . '/' . urlencode($stopWord));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function initializeSynonymsUrl()
|
||||
{
|
||||
if (trim($this->_synonymsUrl) !== '') {
|
||||
return;
|
||||
}
|
||||
$this->_synonymsUrl = $this->_constructUrl(self::SYNONYMS_SERVLET) . $this->getSchema()->getLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function initializeStopWordsUrl()
|
||||
{
|
||||
if (trim($this->_stopWordsUrl) !== '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->_stopWordsUrl = $this->_constructUrl(self::STOPWORDS_SERVLET) . $this->getSchema()->getLanguage();
|
||||
}
|
||||
}
|
120
Classes/System/Meilisearch/Service/MeilisearchReadService.php
Normal file
120
Classes/System/Meilisearch/Service/MeilisearchReadService.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\Domain\Search\Query\Query;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchCommunicationException;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchInternalServerErrorException;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchUnavailableException;
|
||||
use Solarium\Exception\HttpException;
|
||||
|
||||
/**
|
||||
* Class MeilisearchReadService
|
||||
*/
|
||||
class MeilisearchReadService extends AbstractMeilisearchService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $hasSearched = false;
|
||||
|
||||
/**
|
||||
* @var ResponseAdapter
|
||||
*/
|
||||
protected $responseCache = null;
|
||||
|
||||
/**
|
||||
* Performs a search.
|
||||
*
|
||||
* @param Query $query
|
||||
* @return ResponseAdapter Meilisearch response
|
||||
* @throws \RuntimeException if Meilisearch returns a HTTP status code other than 200
|
||||
*/
|
||||
public function search($query)
|
||||
{
|
||||
try {
|
||||
$request = $this->client->createRequest($query);
|
||||
$response = $this->executeRequest($request);
|
||||
$this->hasSearched = true;
|
||||
$this->responseCache = $response;
|
||||
} catch (HttpException $e) {
|
||||
$this->handleErrorResponses($e);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a search has been executed or not.
|
||||
*
|
||||
* @return bool TRUE if a search has been executed, FALSE otherwise
|
||||
*/
|
||||
public function hasSearched()
|
||||
{
|
||||
return $this->hasSearched;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the most recent response (if any)
|
||||
*
|
||||
* @return ResponseAdapter Most recent response, or NULL if a search has not been executed yet.
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->responseCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method maps the failed meilisearch requests to a meaningful exception.
|
||||
*
|
||||
* @param HttpException $exception
|
||||
* @throws MeilisearchCommunicationException
|
||||
* @return HttpException
|
||||
*/
|
||||
protected function handleErrorResponses(HttpException $exception)
|
||||
{
|
||||
$status = $exception->getCode();
|
||||
$message = $exception->getStatusMessage();
|
||||
$meilisearchRespone = new ResponseAdapter($exception->getBody());
|
||||
|
||||
if ($status === 0 || $status === 502) {
|
||||
$e = new MeilisearchUnavailableException('Meilisearch Server not available: ' . $message, 1505989391);
|
||||
$e->setMeilisearchResponse($meilisearchRespone);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($status === 500) {
|
||||
$e = new MeilisearchInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897);
|
||||
$e->setMeilisearchResponse($meilisearchRespone);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$e = new MeilisearchCommunicationException('Invalid query. Meilisearch returned an error: ' . $status . ' ' . $message, 1293109870);
|
||||
$e->setMeilisearchResponse($meilisearchRespone);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
121
Classes/System/Meilisearch/Service/MeilisearchWriteService.php
Normal file
121
Classes/System/Meilisearch/Service/MeilisearchWriteService.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use Solarium\QueryType\Extract\Query;
|
||||
|
||||
/**
|
||||
* Class MeilisearchWriteService
|
||||
*/
|
||||
class MeilisearchWriteService extends AbstractMeilisearchService
|
||||
{
|
||||
const EXTRACT_SERVLET = 'update/extract';
|
||||
|
||||
/**
|
||||
* Performs a content and meta data extraction request.
|
||||
*
|
||||
* @param Query $query An extraction query
|
||||
* @return array An array containing the extracted content [0] and meta data [1]
|
||||
*/
|
||||
public function extractByQuery(Query $query)
|
||||
{
|
||||
try {
|
||||
$response = $this->createAndExecuteRequest($query);
|
||||
return [$response->file, (array)$response->file_metadata];
|
||||
} catch (\Exception $e) {
|
||||
$param = $query->getRequestBuilder()->build($query)->getParams();
|
||||
$this->logger->log(
|
||||
MeilisearchLogManager::ERROR,
|
||||
'Extracting text and meta data through Meilisearch Cell over HTTP POST',
|
||||
[
|
||||
'query' => (array)$query,
|
||||
'parameters' => $param,
|
||||
'file' => $query->getFile(),
|
||||
'query url' => self::EXTRACT_SERVLET,
|
||||
'exception' => $e->getMessage()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all index documents of a certain type and does a commit
|
||||
* afterwards.
|
||||
*
|
||||
* @param string $type The type of documents to delete, usually a table name.
|
||||
* @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE
|
||||
*/
|
||||
public function deleteByType($type, $commit = true)
|
||||
{
|
||||
$this->deleteByQuery('type:' . trim($type));
|
||||
|
||||
if ($commit) {
|
||||
$this->commit(false, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a delete document based on a query and submit it
|
||||
*
|
||||
* @param string $rawQuery Expected to be utf-8 encoded
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function deleteByQuery($rawQuery) {
|
||||
$query = $this->client->createUpdate();
|
||||
$query->addDeleteQuery($rawQuery);
|
||||
return $this->createAndExecuteRequest($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an array of Meilisearch Documents to the index all at once
|
||||
*
|
||||
* @param array $documents Should be an array of \WapplerSystems\Meilisearch\System\Meilisearch\Document\Document instances
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function addDocuments($documents)
|
||||
{
|
||||
$update = $this->client->createUpdate();
|
||||
$update->addDocuments($documents);
|
||||
return $this->createAndExecuteRequest($update);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a commit command. Will be synchronous unless both wait parameters are set to false.
|
||||
*
|
||||
* @param boolean $expungeDeletes Defaults to false, merge segments with deletes away
|
||||
* @param boolean $waitSearcher Defaults to true, block until a new searcher is opened and registered as the main query searcher, making the changes visible
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function commit($expungeDeletes = false, $waitSearcher = true)
|
||||
{
|
||||
$update = $this->client->createUpdate();
|
||||
$update->addCommit(false, $waitSearcher, $expungeDeletes);
|
||||
return $this->createAndExecuteRequest($update);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user