meilisearch/Classes/Domain/Search/Query/SuggestQuery.php

87 lines
3.0 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\Query;
/***************************************************************
* 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\ParameterBuilder\ReturnFields;
use WapplerSystems\Meilisearch\Domain\Search\Query\Helper\EscapeService;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\Util;
/**
* A query specialized to get search suggestions
*
* @author Ingo Renner <ingo@typo3.org>
*/
class SuggestQuery extends Query
{
/**
* @var array
*/
protected $configuration;
/**
* @var string
*/
protected $prefix;
/**
* SuggestQuery constructor.
*
* @param string $keywords
2021-04-17 21:20:54 +02:00
* @param TypoScriptConfiguration $meilisearchConfiguration
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function __construct($keywords, $meilisearchConfiguration = null)
2021-04-17 00:26:33 +02:00
{
parent::__construct();
$keywords = (string)$keywords;
2021-04-17 21:20:54 +02:00
$meilisearchConfiguration = $meilisearchConfiguration ?? Util::getMeilisearchConfiguration();
2021-04-17 00:26:33 +02:00
$this->setQuery($keywords);
2021-04-17 21:20:54 +02:00
$this->configuration = $meilisearchConfiguration->getObjectByPathOrDefault('plugin.tx_meilisearch.suggest.', []);
2021-04-17 00:26:33 +02:00
if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) {
$this->prefix = EscapeService::escape($keywords);
} else {
$matches = [];
preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches);
$fullKeywords = trim($matches[2]);
$partialKeyword = trim($matches[3]);
$this->setQuery($fullKeywords);
$this->prefix = EscapeService::escape($partialKeyword);
}
$this->getEDisMax()->setQueryAlternative('*:*');
$this->setFields(ReturnFields::fromString($this->configuration['suggestField'])->getValues());
$this->addParam('facet', 'on');
$this->addParam('facet.prefix', $this->prefix);
$this->addParam('facet.field', $this->configuration['suggestField']);
$this->addParam('facet.limit', $this->configuration['numberOfSuggestions']);
$this->addParam('facet.mincount', 1);
$this->addParam('facet.method', 'enum');
}
}