|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2009-2015 Ingo Renner <[email protected]> |
|
8
|
|
|
* All rights reserved |
|
9
|
|
|
* |
|
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
11
|
|
|
* free software; you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* The GNU General Public License can be found at |
|
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
18
|
|
|
* |
|
19
|
|
|
* This script is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
25
|
|
|
***************************************************************/ |
|
26
|
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\ReturnFields; |
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Helper\EscapeService; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A query specialized to get search suggestions |
|
34
|
|
|
* |
|
35
|
|
|
* @author Ingo Renner <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class SuggestQuery extends Query |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $configuration; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $prefix; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* SuggestQuery constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $keywords |
|
53
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function __construct($keywords, $solrConfiguration = null) |
|
56
|
|
|
{ |
|
57
|
5 |
|
parent::__construct(); |
|
58
|
5 |
|
$keywords = (string)$keywords; |
|
59
|
|
|
|
|
60
|
5 |
|
$solrConfiguration = $solrConfiguration ?? Util::getSolrConfiguration(); |
|
61
|
|
|
|
|
62
|
5 |
|
$this->setQuery($keywords); |
|
63
|
5 |
|
$this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []); |
|
64
|
|
|
|
|
65
|
5 |
|
if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) { |
|
66
|
|
|
$this->prefix = EscapeService::escape($keywords); |
|
67
|
|
|
} else { |
|
68
|
5 |
|
$matches = []; |
|
69
|
5 |
|
preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches); |
|
70
|
5 |
|
$fullKeywords = trim($matches[2]); |
|
71
|
5 |
|
$partialKeyword = trim($matches[3]); |
|
72
|
|
|
|
|
73
|
5 |
|
$this->setQuery($fullKeywords); |
|
74
|
5 |
|
$this->prefix = EscapeService::escape($partialKeyword); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
$this->getEDisMax()->setQueryAlternative('*:*'); |
|
78
|
5 |
|
$this->setFields(ReturnFields::fromString($this->configuration['suggestField'])->getValues()); |
|
79
|
5 |
|
$this->addParam('facet', 'on'); |
|
80
|
5 |
|
$this->addParam('facet.prefix', $this->prefix); |
|
81
|
5 |
|
$this->addParam('facet.field', $this->configuration['suggestField']); |
|
82
|
5 |
|
$this->addParam('facet.limit', $this->configuration['numberOfSuggestions']); |
|
83
|
5 |
|
$this->addParam('facet.mincount', 1); |
|
84
|
5 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|