Passed
Push — trunk ( 74bc07...45b10d )
by Christian
11:29 queued 12s
created

AbstractElasticsearchDefinition::getMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Framework;
4
5
use OpenSearchDSL\Query\Compound\BoolQuery;
6
use OpenSearchDSL\Query\FullText\MatchPhrasePrefixQuery;
7
use OpenSearchDSL\Query\FullText\MatchQuery;
8
use OpenSearchDSL\Query\TermLevel\WildcardQuery;
9
use Shopware\Core\Framework\Context;
10
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
12
13
/**
14
 * @package core
15
 */
16
abstract class AbstractElasticsearchDefinition
17
{
18
    abstract public function getEntityDefinition(): EntityDefinition;
19
20
    /**
21
     * @return array<mixed>
22
     */
23
    abstract public function getMapping(Context $context): array;
24
25
    /**
26
     * @param array<string> $ids
27
     *
28
     * @return array<mixed>
29
     */
30
    public function fetch(array $ids, Context $context): array
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
    public function fetch(array $ids, /** @scrutinizer ignore-unused */ Context $context): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $ids is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
    public function fetch(/** @scrutinizer ignore-unused */ array $ids, Context $context): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        return [];
33
    }
34
35
    public function buildTermQuery(Context $context, Criteria $criteria): BoolQuery
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function buildTermQuery(/** @scrutinizer ignore-unused */ Context $context, Criteria $criteria): BoolQuery

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $bool = new BoolQuery();
38
39
        $term = (string) $criteria->getTerm();
40
41
        $queries = [
42
            new MatchQuery('fullTextBoosted', $term, ['boost' => 10]), // boosted word matches
43
            new MatchQuery('fullText', $term, ['boost' => 5]), // whole word matches
44
            new MatchQuery('fullText', $term, ['fuzziness' => 'auto', 'boost' => 3]), // word matches not exactly =>
45
            new MatchPhrasePrefixQuery('fullText', $term, ['boost' => 1, 'slop' => 5]), // one of the words begins with: "Spachtel" => "Spachtelmasse"
46
            new WildcardQuery('fullText', '*' . mb_strtolower($term) . '*'), // part of a word matches: "masse" => "Spachtelmasse"
47
            new MatchQuery('fullText.ngram', $term),
48
        ];
49
50
        foreach ($queries as $query) {
51
            $bool->add($query, BoolQuery::SHOULD);
52
        }
53
54
        $bool->addParameter('minimum_should_match', 1);
55
56
        return $bool;
57
    }
58
59
    protected function stripText(string $text): string
60
    {
61
        // Remove all html elements to save up space
62
        $text = strip_tags($text);
63
64
        if (mb_strlen($text) >= 32766) {
65
            return mb_substr($text, 0, 32766);
66
        }
67
68
        return $text;
69
    }
70
}
71