|
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 |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
return []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function buildTermQuery(Context $context, Criteria $criteria): BoolQuery |
|
|
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.