1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\ReturnFields; |
18
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
19
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A query specialized to get search suggestions |
23
|
|
|
* |
24
|
|
|
* @author Ingo Renner <[email protected]> |
25
|
|
|
* @copyright (c) 2009-2015 Ingo Renner <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class SuggestQuery extends Query |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $configuration; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $prefix; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SuggestQuery constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $keywords |
43
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
44
|
|
|
*/ |
45
|
7 |
|
public function __construct($keywords, $solrConfiguration = null) |
46
|
|
|
{ |
47
|
7 |
|
parent::__construct(); |
48
|
7 |
|
$keywords = (string)$keywords; |
49
|
|
|
|
50
|
7 |
|
$solrConfiguration = $solrConfiguration ?? Util::getSolrConfiguration(); |
51
|
|
|
|
52
|
7 |
|
$this->setQuery($keywords); |
53
|
7 |
|
$this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []); |
54
|
|
|
|
55
|
7 |
|
if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) { |
56
|
|
|
$this->prefix = $keywords; |
57
|
|
|
} else { |
58
|
7 |
|
$matches = []; |
59
|
7 |
|
preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches); |
60
|
7 |
|
$fullKeywords = trim($matches[2]); |
61
|
7 |
|
$partialKeyword = trim($matches[3]); |
62
|
|
|
|
63
|
7 |
|
$this->setQuery($fullKeywords); |
64
|
7 |
|
$this->prefix = $partialKeyword; |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
$this->getEDisMax()->setQueryAlternative('*:*'); |
68
|
7 |
|
$this->setFields(ReturnFields::fromString(($this->configuration['suggestField'] ?? null))->getValues()); |
69
|
7 |
|
$this->addParam('facet', 'on'); |
70
|
7 |
|
$this->addParam('facet.prefix', $this->prefix); |
71
|
7 |
|
$this->addParam('facet.field', ($this->configuration['suggestField'] ?? null)); |
72
|
7 |
|
$this->addParam('facet.limit', ($this->configuration['numberOfSuggestions'] ?? null)); |
73
|
7 |
|
$this->addParam('facet.mincount', 1); |
74
|
7 |
|
$this->addParam('facet.method', 'enum'); |
75
|
7 |
|
} |
76
|
|
|
} |
77
|
|
|
|