1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Elasticsearch\Client; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* phpMyFAQ Elasticsearch based search classes. |
7
|
|
|
* |
8
|
|
|
* PHP Version 5.5 |
9
|
|
|
* |
10
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
11
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
12
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
13
|
|
|
* |
14
|
|
|
* @category phpMyFAQ |
15
|
|
|
* @author Thorsten Rinne <[email protected]> |
16
|
|
|
* @copyright 2015 phpMyFAQ Team |
17
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
18
|
|
|
* @link http://www.phpmyfaq.de |
19
|
|
|
* @since 2015-12-25 |
20
|
|
|
*/ |
21
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
22
|
|
|
exit(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PMF_Search_Elasticsearch. |
27
|
|
|
* |
28
|
|
|
* @category phpMyFAQ |
29
|
|
|
* @author Thorsten Rinne <[email protected]> |
30
|
|
|
* @copyright 2015 phpMyFAQ Team |
31
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
32
|
|
|
* @link http://www.phpmyfaq.de |
33
|
|
|
* @since 2015-12-25 |
34
|
|
|
*/ |
35
|
|
|
class PMF_Search_Elasticsearch extends PMF_Search_Abstract implements PMF_Search_Interface |
36
|
|
|
{ |
37
|
|
|
/** @var Client */ |
38
|
|
|
private $client = null; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
private $esConfig = []; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $language = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param PMF_Configuration |
50
|
|
|
*/ |
51
|
|
|
public function __construct(PMF_Configuration $config) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($config); |
54
|
|
|
|
55
|
|
|
$this->client = $this->_config->getElasticsearch(); |
56
|
|
|
$this->esConfig = $this->_config->getElasticsearchConfig(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Prepares the search and executes it. |
61
|
|
|
* |
62
|
|
|
* @param string $searchTerm Search term |
63
|
|
|
* |
64
|
|
|
* @throws PMF_Search_Exception |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function search($searchTerm) |
69
|
|
|
{ |
70
|
|
|
if ('' === $this->getLanguage()) { |
71
|
|
|
$searchParams = [ |
72
|
|
|
'index' => $this->esConfig['index'], |
73
|
|
|
'type' => $this->esConfig['type'], |
74
|
|
|
'size' => 1000, |
75
|
|
|
'body' => [ |
76
|
|
|
'query' => [ |
77
|
|
|
'bool' => [ |
78
|
|
|
'should' => [ |
79
|
|
|
[ 'match' => [ 'question' => $searchTerm ] ], |
80
|
|
|
[ 'match' => [ 'answer' => $searchTerm ] ], |
81
|
|
|
[ 'match' => [ 'keywords' => $searchTerm ] ] |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
|
85
|
|
|
] |
86
|
|
|
] |
87
|
|
|
]; |
88
|
|
|
} else { |
89
|
|
|
$searchParams = [ |
90
|
|
|
'index' => $this->esConfig['index'], |
91
|
|
|
'type' => $this->esConfig['type'], |
92
|
|
|
'size' => 1000, |
93
|
|
|
'body' => [ |
94
|
|
|
'query' => [ |
95
|
|
|
'filtered' => [ |
96
|
|
|
'filter' => [ |
97
|
|
|
'term' => [ 'lang' => $this->getLanguage() ] |
98
|
|
|
], |
99
|
|
|
'query' => [ |
100
|
|
|
'bool' => [ |
101
|
|
|
'should' => [ |
102
|
|
|
[ 'match' => [ 'question' => $searchTerm ] ], |
103
|
|
|
[ 'match' => [ 'answer' => $searchTerm ] ], |
104
|
|
|
[ 'match' => [ 'keywords' => $searchTerm ] ] |
105
|
|
|
] |
106
|
|
|
] |
107
|
|
|
] |
108
|
|
|
] |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$result = $this->client->search($searchParams); |
115
|
|
|
|
116
|
|
|
if (0 !== $result['hits']['total']) { |
117
|
|
|
|
118
|
|
|
foreach ($result['hits']['hits'] as $hit) { |
119
|
|
|
$resultSet = new stdClass(); |
120
|
|
|
$resultSet->id = $hit['_source']['id']; |
121
|
|
|
$resultSet->lang = $hit['_source']['lang']; |
122
|
|
|
$resultSet->question = $hit['_source']['question']; |
123
|
|
|
$resultSet->answer = $hit['_source']['answer']; |
124
|
|
|
$resultSet->keywords = $hit['_source']['keywords']; |
125
|
|
|
$resultSet->category_id = $hit['_source']['category_id']; |
126
|
|
|
$resultSet->score = $hit['_score']; |
127
|
|
|
|
128
|
|
|
$this->resultSet[] = $resultSet; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} else { |
132
|
|
|
$this->resultSet = []; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->resultSet; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getLanguage() |
142
|
|
|
{ |
143
|
|
|
return $this->language; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $language |
148
|
|
|
*/ |
149
|
|
|
public function setLanguage($language) |
150
|
|
|
{ |
151
|
|
|
$this->language = $language; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..