Completed
Push — master ( 257818...6d6774 )
by Jeroen
06:17 queued 13s
created

Search/AbstractElasticaSearcher.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Search;
4
5
use Elastica\Query;
6
use Elastica\Search;
7
use Elastica\Suggest;
8
use Kunstmaan\SearchBundle\Search\Search as SearchLayer;
9
10
abstract class AbstractElasticaSearcher implements SearcherInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $indexName;
16
17
    /**
18
     * @var string
19
     */
20
    protected $indexType;
21
22
    /**
23
     * @var SearchLayer
24
     */
25
    protected $search;
26
27
    /**
28
     * @var Query
29
     */
30
    protected $query;
31
32
    /**
33
     * @var mixed
34
     */
35
    protected $data;
36
37
    /**
38
     * @var string
39
     */
40
    protected $language;
41
42
    /**
43
     * @var string
44
     */
45
    protected $contentType;
46
47
    public function __construct()
48
    {
49
        $this->query = new Query();
50
    }
51
52
    /**
53
     * @param mixed  $query
54
     * @param string $contentType
55
     *
56
     * @return mixed
57
     */
58
    abstract public function defineSearch($query, $contentType);
59
60
    /**
61
     * @param int $offset
62
     * @param int $size
63
     *
64
     * @return \Elastica\ResultSet
65
     */
66
    public function search($offset = null, $size = null)
67
    {
68
        $this->defineSearch($this->data, $this->contentType);
69
        $this->setPagination($offset, $size);
70
71
        return $this->getSearchResult();
72
    }
73
74
    /**
75
     * @return \Elastica\ResultSet
76
     */
77
    public function getSuggestions()
78
    {
79
        $suggestPhrase = new Suggest\Phrase('content-suggester', 'content');
80
        $suggestPhrase->setText($this->data);
81
        $suggestPhrase->setHighlight('<strong>', '</strong>');
82
        $suggestPhrase->setAnalyzer('suggestion_analyzer');
83
        $suggestPhrase->setConfidence(2);
84
        $suggestPhrase->setSize(1);
85
86
        $suggest = new Suggest($suggestPhrase);
87
        $this->query->setSuggest($suggest);
88
89
        return $this->getSearchResult();
90
    }
91
92
    /**
93
     * @return \Elastica\ResultSet
94
     */
95
    public function getSearchResult()
96
    {
97
        $index = $this->search->getIndex($this->getIndexName() . '_' . $this->language);
98
99
        $search = new Search($this->search->getClient());
100
        $search->addIndex($index);
101
102
        if (method_exists($search, 'getType')) {
103
            $search->addType($index->getType($this->indexType));
0 ignored issues
show
The method addType() does not seem to exist on object<Elastica\Search>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        }
105
106
        return $search->search($this->query);
107
    }
108
109
    /**
110
     * @param int $offset
111
     * @param int $size
112
     *
113
     * @return SearcherInterface
114
     */
115
    public function setPagination($offset, $size)
116
    {
117
        if (\is_int($offset)) {
118
            $this->query->setFrom($offset);
119
        }
120
121
        if (\is_int($size)) {
122
            $this->query->setSize($size);
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param string $indexName
130
     *
131
     * @return SearcherInterface
132
     */
133
    public function setIndexName($indexName)
134
    {
135
        $this->indexName = $indexName;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getIndexName()
144
    {
145
        return $this->indexName;
146
    }
147
148
    /**
149
     * @deprecated upgrade to ruflin/elastica and elasticsearch v7 and don't set an index type
150
     *
151
     * @param string $indexType
152
     *
153
     * @return SearcherInterface
154
     */
155
    public function setIndexType($indexType)
156
    {
157
        @trigger_error(sprintf('The "setIndexType" method in "%s" is deprecated since KunstmaanSearchBundle 5.6 and will be removed in KunstmaanSearchBundle 6.0. Upgrade to ruflin/elastica and elasticsearch v7 and don\'t set an index type.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
158
159
        $this->indexType = $indexType;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @deprecated upgrade to ruflin/elastica and elasticsearch v7 and don't set an index type
166
     *
167
     * @return string
168
     */
169
    public function getIndexType()
170
    {
171
        @trigger_error(sprintf('The "getIndexType" method in "%s" is deprecated since KunstmaanSearchBundle 5.6 and will be removed in KunstmaanSearchBundle 6.0. Upgrade to ruflin/elastica and elasticsearch v7 and don\'t set an index type.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
172
173
        return $this->indexType;
174
    }
175
176
    /**
177
     * @param mixed $data
178
     *
179
     * @return SearcherInterface
180
     */
181
    public function setData($data)
182
    {
183
        $this->data = $data;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return mixed
190
     * @return SearcherInterface
191
     */
192
    public function getData()
193
    {
194
        return $this->data;
195
    }
196
197
    /**
198
     * @param string $language
199
     *
200
     * @return SearcherInterface
201
     */
202
    public function setLanguage($language)
203
    {
204
        $this->language = $language;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getLanguage()
213
    {
214
        return $this->language;
215
    }
216
217
    /**
218
     * @param string $contentType
219
     *
220
     * @return SearcherInterface
221
     */
222
    public function setContentType($contentType)
223
    {
224
        $this->contentType = $contentType;
225
226
        return $this;
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getContentType()
233
    {
234
        return $this->contentType;
235
    }
236
237
    /**
238
     * @param SearchLayer $search
239
     *
240
     * @return SearcherInterface
241
     */
242
    public function setSearch($search)
243
    {
244
        $this->search = $search;
245
246
        return $this;
247
    }
248
249
    /**
250
     * @return SearchLayer
251
     */
252
    public function getSearch()
253
    {
254
        return $this->search;
255
    }
256
257
    /**
258
     * @return Query
259
     */
260
    public function getQuery()
261
    {
262
        return $this->query;
263
    }
264
}
265