Completed
Push — master ( 7fe99b...67b589 )
by Rafał
15s queued 10s
created

ArticleRepository::getSuggestedTerm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher ElasticSearch Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ElasticSearchBundle\Repository;
18
19
use Elastica\Query;
20
use Elastica\Query\BoolQuery;
21
use Elastica\Query\MatchAll;
22
use Elastica\Query\MultiMatch;
23
use Elastica\Query\Nested;
24
use Elastica\Query\Range;
25
use Elastica\Query\Term;
26
use Elastica\QueryBuilder\DSL\Suggest;
27
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
28
use FOS\ElasticaBundle\Repository;
29
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria;
30
use SWP\Bundle\ElasticSearchBundle\Loader\SearchResultLoader;
31
32
class ArticleRepository extends Repository
33
{
34
    public function findByCriteria(Criteria $criteria, array $extraFields = [], bool $searchByBody = false): PaginatorAdapterInterface
35
    {
36
        $fields = $criteria->getFilters()->getFields();
37
        $boolFilter = new BoolQuery();
38
39
        $term = $criteria->getTerm();
40
        if (null !== $term && '' !== $term) {
41
            $searchBy = ['title^10', 'lead^4', 'body^2', 'keywords.name'];
42
43
            foreach ($extraFields as $extraField) {
44
                $searchBy[] = 'extra.'.$extraField;
45
            }
46
47
            if ($searchByBody) {
48
                array_splice($searchBy, 2, 0, ['body']);
49
            }
50
51
            $boolQuery = new BoolQuery();
52
53
            $phraseMultiMatchQuery = new MultiMatch();
54
            $phraseMultiMatchQuery->setQuery($term);
55
            $phraseMultiMatchQuery->setFields($searchBy);
56
            $phraseMultiMatchQuery->setType(MultiMatch::TYPE_PHRASE);
57
            $phraseMultiMatchQuery->setParam('boost', 4);
58
59
            $boolQuery->addShould($phraseMultiMatchQuery);
60
61
            $fuzzinessMultiMatchQuery = new MultiMatch();
62
            $fuzzinessMultiMatchQuery->setQuery($term);
63
            $fuzzinessMultiMatchQuery->setFields($searchBy);
64
            $fuzzinessMultiMatchQuery->setFuzziness(1);
65
            $boolQuery->addShould($fuzzinessMultiMatchQuery);
66
67
            $multiMatchQuery = new MultiMatch();
68
            $multiMatchQuery->setQuery($term);
69
            $multiMatchQuery->setFields($searchBy);
70
            $multiMatchQuery->setOperator(MultiMatch::OPERATOR_AND);
71
            $multiMatchQuery->setParam('boost', 2);
72
            $multiMatchQuery->setFuzziness(0);
73
            $boolQuery->addShould($multiMatchQuery);
74
75
            $bool = new BoolQuery();
76
            $authorsPhraseMultiMatchQuery = new MultiMatch();
77
            $authorsPhraseMultiMatchQuery->setQuery($term);
78
            $authorsPhraseMultiMatchQuery->setFields(['authors.name', 'authors.biography']);
79
            $authorsPhraseMultiMatchQuery->setType(MultiMatch::TYPE_PHRASE);
80
            $authorsPhraseMultiMatchQuery->setParam('boost', 4);
81
            $bool->addShould($authorsPhraseMultiMatchQuery);
82
83
            $authorMultiMatchQuery = new MultiMatch();
84
            $authorMultiMatchQuery->setQuery($term);
85
            $authorMultiMatchQuery->setFields(['authors.name', 'authors.biography']);
86
            $authorMultiMatchQuery->setOperator(MultiMatch::OPERATOR_AND);
87
            $authorMultiMatchQuery->setParam('boost', 2);
88
            $authorMultiMatchQuery->setFuzziness(0);
89
            $bool->addShould($authorMultiMatchQuery);
90
91
            $fuzzinessPhraseMultiMatchQuery = new MultiMatch();
92
            $fuzzinessPhraseMultiMatchQuery->setQuery($term);
93
            $fuzzinessPhraseMultiMatchQuery->setFields(['authors.name', 'authors.biography']);
94
            $fuzzinessPhraseMultiMatchQuery->setFuzziness(1);
95
            $bool->addShould($fuzzinessPhraseMultiMatchQuery);
96
97
            $nested = new Nested();
98
            $nested->setPath('authors');
99
            $functionScore = new Query\FunctionScore();
100
            $functionScore->addWeightFunction(15, new Query\Match('authors.name', $term));
101
            $functionScore->addWeightFunction(5, new Query\Match('authors.biography', $term));
102
            $functionScore->addWeightFunction(15, new Query\MatchPhrase('authors.name', $term));
103
            $functionScore->addWeightFunction(10, new Query\MatchPhrase('authors.biography', $term));
104
            $functionScore->setQuery($bool);
105
            $nested->setQuery($functionScore);
106
107
            $boolQuery->addShould($nested);
108
            $boolFilter->addMust($boolQuery);
109
        } else {
110
            $boolFilter->addMust(new MatchAll());
111
        }
112
113 View Code Duplication
        if (null !== $fields->get('keywords') && !empty($fields->get('keywords'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
            $bool = new BoolQuery();
115
            $bool->addFilter(new Query\Terms('keywords.name', $fields->get('keywords')));
116
            $nested = new Nested();
117
            $nested->setPath('keywords');
118
            $nested->setQuery($bool);
119
            $boolFilter->addMust($nested);
120
        }
121
122 View Code Duplication
        if ((null !== $fields->get('authors')) && !empty($fields->get('authors'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
            $bool = new BoolQuery();
124
            $bool->addFilter(new Query\Terms('authors.id', $fields->get('authors')));
125
            $nested = new Nested();
126
            $nested->setPath('authors');
127
            $nested->setQuery($bool);
128
            $boolFilter->addMust($nested);
129
        }
130
131 View Code Duplication
        if (null !== $fields->get('sources') && !empty($fields->get('sources'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
            $nested = new Nested();
133
            $nested->setPath('sources');
134
            $boolQuery = new BoolQuery();
135
            $boolQuery->addMust(new Query\Terms('sources.name', $fields->get('sources')));
136
            $nested->setQuery($boolQuery);
137
            $boolFilter->addMust($nested);
138
        }
139
140 View Code Duplication
        if (null !== $fields->get('statuses') && !empty($fields->get('statuses'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
            $boolFilter->addFilter(new Query\Terms('status', $fields->get('statuses')));
142
        }
143
144
        if (null !== $fields->get('metadata') && !empty($fields->get('metadata'))) {
145
            foreach ($fields->get('metadata') as $key => $values) {
146
                foreach ((array) $values as $value) {
147
                    $boolFilter->addFilter(new Query\Match($key, $value));
148
                }
149
            }
150
        }
151
152
        if (null !== $fields->get('tenantCode')) {
153
            $boolFilter->addFilter(new Term(['tenantCode' => $fields->get('tenantCode')]));
154
        }
155
156
        $bool = new BoolQuery();
157 View Code Duplication
        if (null !== $fields->get('routes') && !empty($fields->get('routes'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
            $bool->addFilter(new Query\Terms('route.id', $fields->get('routes')));
159
        }
160
161
        if (null !== $fields->get('publishedAfter') || null !== $fields->get('publishedBefore')) {
162
            $boolFilter->addFilter(new Range(
163
                'publishedAt',
164
                [
165
                    'gte' => null !== $fields->get('publishedAfter') ? $fields->get('publishedAfter')->format('Y-m-d') : null,
166
                    'lte' => null !== $fields->get('publishedBefore') ? $fields->get('publishedBefore')->format('Y-m-d') : null,
167
                ]
168
            ));
169
170
            $boolFilter->addFilter(new Term(['isPublishable' => true]));
171
        }
172
173
        if (!empty($bool->getParams())) {
174
            $boolFilter->addMust($bool);
175
        }
176
177
        $functionScore = new Query\FunctionScore();
178
        $functionScore->setScoreMode(Query\FunctionScore::SCORE_MODE_SUM);
179
        $functionScore->setBoostMode(Query\FunctionScore::BOOST_MODE_MULTIPLY);
180
        $functionScore->addWeightFunction(1);
181
        $now = new \DateTime();
182
        $functionScore->addDecayFunction(
183
            Query\FunctionScore::DECAY_GAUSS,
184
            'publishedAt',
185
            $now->format('Y-m-d'),
186
            '31d',
187
            '1d',
188
            0.5,
189
            5
190
        );
191
192
        $functionScore->addDecayFunction(
193
            Query\FunctionScore::DECAY_GAUSS,
194
            'publishedAt',
195
            $now->format('Y-m-d'),
196
            '365d',
197
            '1d',
198
            0.5,
199
            2
200
        );
201
202
        $functionScore->setQuery($boolFilter);
203
204
        $query = Query::create($functionScore)
205
            ->addSort([
206
                '_score' => 'desc',
207
                $criteria->getOrder()->getField() => $criteria->getOrder()->getDirection(),
208
            ]);
209
210
        $query->setSize(SearchResultLoader::MAX_RESULTS);
211
        $query->setTrackScores(true);
212
213
        return $this->createPaginatorAdapter($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
214
    }
215
216
    public function getSuggestedTerm(string $term): string
217
    {
218
        $suggestQuery = new Suggest();
219
        $suggestQuery->phrase('our_suggestion', '_all');
220
221
        $phraseMultiMatchQuery = new MultiMatch();
222
        $phraseMultiMatchQuery->setQuery($term);
223
        $phraseMultiMatchQuery->setFields('_all');
0 ignored issues
show
Documentation introduced by
'_all' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
224
        $phraseMultiMatchQuery->setType(MultiMatch::TYPE_PHRASE);
225
        $phraseMultiMatchQuery->setParam('boost', 50);
226
227
        $query = new \Elastica\Query($phraseMultiMatchQuery);
228
        $suggest = new \Elastica\Suggest();
229
        $suggest->setParam(
230
            'phrase',
231
            [
232
                'text' => $term,
233
                'phrase' => ['field' => '_all'],
234
            ]
235
        );
236
237
        $query->setSuggest($suggest);
238
239
        $adapter = $this->createPaginatorAdapter($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
240
        $suggest = $adapter->getSuggests();
241
242
        return $suggest['phrase'][0]['options'][0]['text'] ?? '';
243
    }
244
}
245