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
|
|
|
* Elasticsearch mapping |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $mappings = [ |
49
|
|
|
'faqs' => [ |
50
|
|
|
'_source' => [ |
51
|
|
|
'enabled' => true |
52
|
|
|
], |
53
|
|
|
'properties' => [ |
54
|
|
|
'id' => [ |
55
|
|
|
'type' => 'integer' |
56
|
|
|
], |
57
|
|
|
'question' => [ |
58
|
|
|
'type' => 'string', |
59
|
|
|
'analyzer' => 'standard' |
60
|
|
|
], |
61
|
|
|
'answer' => [ |
62
|
|
|
'type' => 'string', |
63
|
|
|
'analyzer' => 'standard' |
64
|
|
|
], |
65
|
|
|
'keywords' => [ |
66
|
|
|
'type' => 'string', |
67
|
|
|
'analyzer' => 'standard' |
68
|
|
|
], |
69
|
|
|
'categories' => [ |
70
|
|
|
'type' => 'string', |
71
|
|
|
'analyzer' => 'standard' |
72
|
|
|
] |
73
|
|
|
] |
74
|
|
|
] |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Constructor. |
79
|
|
|
* |
80
|
|
|
* @param PMF_Configuration |
81
|
|
|
*/ |
82
|
|
|
public function __construct(PMF_Configuration $config) |
83
|
|
|
{ |
84
|
|
|
parent::__construct($config); |
85
|
|
|
|
86
|
|
|
$this->client = $this->_config->getElasticsearch(); |
87
|
|
|
$this->esConfig = $this->_config->getElasticsearchConfig(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Prepares the search and executes it. |
92
|
|
|
* |
93
|
|
|
* @param string $searchTerm Search term |
94
|
|
|
* |
95
|
|
|
* @return resource |
96
|
|
|
* |
97
|
|
|
* @throws PMF_Search_Exception |
98
|
|
|
*/ |
99
|
|
|
public function search($searchTerm) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function index(Array $faq) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function bulkIndex(Array $faqs) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function update(Array $faq) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function delete($faqId) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Puts phpMyFAQ Elasticsearch mapping into index. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function putMapping() |
130
|
|
|
{ |
131
|
|
|
$response = $this->getMapping(); |
132
|
|
|
|
133
|
|
|
if (0 === count($response[$this->esConfig['index']]['mappings'])) { |
134
|
|
|
|
135
|
|
|
$params = [ |
136
|
|
|
'index' => $this->esConfig['index'], |
137
|
|
|
'type' => $this->esConfig['type'], |
138
|
|
|
'body' => $this->mappings |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
$response = $this->client->indices()->putMapping($params); |
142
|
|
|
|
143
|
|
|
if (isset($response['acknowledged']) && true === $response['acknowledged']) { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the current mapping. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getMapping() |
157
|
|
|
{ |
158
|
|
|
return $this->client->indices()->getMapping(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.