meilisearch/Classes/Search.php

292 lines
8.4 KiB
PHP
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\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 WapplerSystems\Meilisearch\Domain\Search\Query\Query;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchCommunicationException;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
2021-04-17 00:26:33 +02:00
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
2021-04-17 21:20:54 +02:00
* Class to handle meilisearch search requests
2021-04-17 00:26:33 +02:00
*
* @author Ingo Renner <ingo@typo3.org>
*/
class Search
{
/**
2021-04-17 21:20:54 +02:00
* An instance of the Meilisearch service
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @var MeilisearchConnection
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected $meilisearch = null;
2021-04-17 00:26:33 +02:00
/**
* The search query
*
* @var Query
*/
protected $query = null;
/**
* The search response
*
* @var ResponseAdapter
*/
protected $response = null;
/**
* @var TypoScriptConfiguration
*/
protected $configuration;
// TODO Override __clone to reset $response and $hasSearched
/**
2021-04-17 21:20:54 +02:00
* @var \WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager
2021-04-17 00:26:33 +02:00
*/
protected $logger = null;
/**
* Constructor
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchConnection $meilisearchConnection The Meilisearch connection to use for searching
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function __construct(MeilisearchConnection $meilisearchConnection = null)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$this->logger = GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
$this->meilisearch = $meilisearchConnection;
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
if (is_null($meilisearchConnection)) {
2021-04-17 00:26:33 +02:00
/** @var $connectionManager ConnectionManager */
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
2021-04-17 21:20:54 +02:00
$this->meilisearch = $connectionManager->getConnectionByPageId($GLOBALS['TSFE']->id, Util::getLanguageUid());
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
$this->configuration = Util::getMeilisearchConfiguration();
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Gets the Meilisearch connection used by this search.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @return MeilisearchConnection Meilisearch connection
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function getMeilisearchConnection()
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
return $this->meilisearch;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Sets the Meilisearch connection used by this search.
2021-04-17 00:26:33 +02:00
*
* Since WapplerSystems\Meilisearch\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to
* be able to switch between multiple cores/connections during
* one request
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchConnection $meilisearchConnection
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function setMeilisearchConnection(MeilisearchConnection $meilisearchConnection)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$this->meilisearch = $meilisearchConnection;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Executes a query against a Meilisearch server.
2021-04-17 00:26:33 +02:00
*
* 1) Gets the query string
* 2) Conducts the actual search
* 3) Checks debug settings
*
* @param Query $query The query with keywords, filters, and so on.
* @param int $offset Result offset for pagination.
* @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object.
2021-04-17 21:20:54 +02:00
* @return ResponseAdapter Meilisearch response
2021-04-17 00:26:33 +02:00
* @throws \Exception
*/
public function search(Query $query, $offset = 0, $limit = 10)
{
$this->query = $query;
if (!empty($limit)) {
$query->setRows($limit);
}
$query->setStart($offset);
try {
2021-04-17 21:20:54 +02:00
$response = $this->meilisearch->getReadService()->search($query);
2021-04-17 00:26:33 +02:00
if ($this->configuration->getLoggingQueryQueryString()) {
2021-04-17 21:20:54 +02:00
$this->logger->log(MeilisearchLogManager::INFO,
'Querying Meilisearch, getting result',
2021-04-17 00:26:33 +02:00
[
'query string' => $query->getQuery(),
'query parameters' => $query->getRequestBuilder()->build($query)->getParams(),
'response' => json_decode($response->getRawResponse(), true)
]
);
}
2021-04-17 21:20:54 +02:00
} catch (MeilisearchCommunicationException $e) {
2021-04-17 00:26:33 +02:00
if ($this->configuration->getLoggingExceptions()) {
$this->logger->log(
2021-04-17 21:20:54 +02:00
MeilisearchLogManager::ERROR,
'Exception while querying Meilisearch',
2021-04-17 00:26:33 +02:00
[
'exception' => $e->__toString(),
'query' => (array)$query,
'offset' => $offset,
'limit' => $query->getRows()
]
);
}
throw $e;
}
$this->response = $response;
return $this->response;
}
/**
2021-04-17 21:20:54 +02:00
* Sends a ping to the meilisearch server to see whether it is available.
2021-04-17 00:26:33 +02:00
*
* @param bool $useCache Set to true if the cache should be used.
* @return bool Returns TRUE on successful ping.
* @throws \Exception Throws an exception in case ping was not successful.
*/
public function ping($useCache = true)
{
2021-04-17 21:20:54 +02:00
$meilisearchAvailable = false;
2021-04-17 00:26:33 +02:00
try {
2021-04-17 21:20:54 +02:00
if (!$this->meilisearch->getReadService()->ping($useCache)) {
throw new \Exception('Meilisearch Server not responding.', 1237475791);
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
$meilisearchAvailable = true;
2021-04-17 00:26:33 +02:00
} catch (\Exception $e) {
if ($this->configuration->getLoggingExceptions()) {
$this->logger->log(
2021-04-17 21:20:54 +02:00
MeilisearchLogManager::ERROR,
'Exception while trying to ping the meilisearch server',
2021-04-17 00:26:33 +02:00
[
$e->__toString()
]
);
}
}
2021-04-17 21:20:54 +02:00
return $meilisearchAvailable;
2021-04-17 00:26:33 +02:00
}
/**
* Gets the query object.
*
* @return Query
*/
public function getQuery()
{
return $this->query;
}
/**
2021-04-17 21:20:54 +02:00
* Gets the Meilisearch response
2021-04-17 00:26:33 +02:00
*
* @return ResponseAdapter
*/
public function getResponse()
{
return $this->response;
}
public function getRawResponse()
{
return $this->response->getRawResponse();
}
public function getResponseHeader()
{
return $this->getResponse()->responseHeader;
}
public function getResponseBody()
{
// @extensionScannerIgnoreLine
return $this->getResponse()->response;
}
/**
2021-04-17 21:20:54 +02:00
* Gets the time Meilisearch took to execute the query and return the result.
2021-04-17 00:26:33 +02:00
*
* @return int Query time in milliseconds
*/
public function getQueryTime()
{
return $this->getResponseHeader()->QTime;
}
/**
* Gets the number of results per page.
*
* @return int Number of results per page
*/
public function getResultsPerPage()
{
return $this->getResponseHeader()->params->rows;
}
/**
* Gets the result offset.
*
* @return int Result offset
*/
public function getResultOffset()
{
// @extensionScannerIgnoreLine
return $this->response->response->start;
}
public function getDebugResponse()
{
// @extensionScannerIgnoreLine
return $this->response->debug;
}
public function getHighlightedContent()
{
$highlightedContent = false;
if ($this->response->highlighting) {
$highlightedContent = $this->response->highlighting;
}
return $highlightedContent;
}
}