|
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
|
|
|
/** |
|
44
|
|
|
* Constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param PMF_Configuration |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(PMF_Configuration $config) |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct($config); |
|
51
|
|
|
|
|
52
|
|
|
$this->client = $this->_config->getElasticsearch(); |
|
53
|
|
|
$this->esConfig = $this->_config->getElasticsearchConfig(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Prepares the search and executes it. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $searchTerm Search term |
|
60
|
|
|
* |
|
61
|
|
|
* @throws PMF_Search_Exception |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function search($searchTerm) |
|
66
|
|
|
{ |
|
67
|
|
|
$params = [ |
|
68
|
|
|
'index' => $this->esConfig['index'], |
|
69
|
|
|
'type' => $this->esConfig['type'], |
|
70
|
|
|
'size' => 1000, |
|
71
|
|
|
'body' => [ |
|
72
|
|
|
'query' => [ |
|
73
|
|
|
'bool' => [ |
|
74
|
|
|
'should' => [ |
|
75
|
|
|
[ 'match' => [ 'question' => $searchTerm ] ], |
|
76
|
|
|
[ 'match' => [ 'answer' => $searchTerm ] ], |
|
77
|
|
|
[ 'match' => [ 'keswords' => $searchTerm ] ] |
|
78
|
|
|
] |
|
79
|
|
|
] |
|
80
|
|
|
|
|
81
|
|
|
] |
|
82
|
|
|
] |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$result = $this->client->search($params); |
|
86
|
|
|
|
|
87
|
|
|
if (0 !== $result['hits']['total']) { |
|
88
|
|
|
|
|
89
|
|
|
foreach ($result['hits']['hits'] as $hit) { |
|
90
|
|
|
$resultSet = new stdClass(); |
|
91
|
|
|
$resultSet->id = $hit['_source']['id']; |
|
92
|
|
|
$resultSet->lang = $hit['_source']['lang']; |
|
93
|
|
|
$resultSet->question = $hit['_source']['question']; |
|
94
|
|
|
$resultSet->answer = $hit['_source']['answer']; |
|
95
|
|
|
$resultSet->keywords = $hit['_source']['keywords']; |
|
96
|
|
|
$resultSet->category_id = $hit['_source']['category_id']; |
|
97
|
|
|
$resultSet->score = $hit['_score']; |
|
98
|
|
|
|
|
99
|
|
|
$this->resultSet[] = $resultSet; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} else { |
|
103
|
|
|
$this->resultSet = []; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->resultSet; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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..