|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Spameri\ElasticQuery\Query; |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html |
| 8 | + */ |
| 9 | +class MultiMatch implements LeafQueryInterface |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + private $fields; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + private $query; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private $type; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + private $operator; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var null|\Spameri\ElasticQuery\Query\Match\Fuzziness |
| 34 | + */ |
| 35 | + private $fuzziness; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var float |
| 39 | + */ |
| 40 | + private $boost; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var null|string |
| 44 | + */ |
| 45 | + private $analyzer; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var int|null |
| 49 | + */ |
| 50 | + private $minimumShouldMatch; |
| 51 | + |
| 52 | + |
| 53 | + public function __construct( |
| 54 | + array $fields |
| 55 | + , $query |
| 56 | + , float $boost = 1.0 |
| 57 | + , string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS |
| 58 | + , string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR |
| 59 | + , ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL |
| 60 | + , ?string $analyzer = NULL |
| 61 | + , ?int $minimumShouldMatch = NULL |
| 62 | + ) |
| 63 | + { |
| 64 | + if ( ! \in_array($operator, \Spameri\ElasticQuery\Query\Match\Operator::OPERATORS, TRUE)) { |
| 65 | + throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException( |
| 66 | + 'Parameter $operator is invalid, see \Spameri\ElasticQuery\Query\Match\Operator::OPERATORS for valid arguments.' |
| 67 | + ); |
| 68 | + } |
| 69 | + if ( ! \in_array($type, \Spameri\ElasticQuery\Query\Match\MultiMatchType::TYPES, TRUE)) { |
| 70 | + throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException( |
| 71 | + 'Parameter $type is invalid, see \Spameri\ElasticQuery\Query\Match\MultiMatchType::TYPES for valid arguments.' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + $this->fields = $fields; |
| 76 | + $this->query = $query; |
| 77 | + $this->type = $type; |
| 78 | + $this->operator = $operator; |
| 79 | + $this->fuzziness = $fuzziness; |
| 80 | + $this->boost = $boost; |
| 81 | + $this->analyzer = $analyzer; |
| 82 | + $this->minimumShouldMatch = $minimumShouldMatch; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + public function key() : string |
| 87 | + { |
| 88 | + return 'multiMatch_' . \implode('-', $this->fields) . '_' . $this->query; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public function toArray() : array |
| 93 | + { |
| 94 | + $array = [ |
| 95 | + 'multi_match' => [ |
| 96 | + 'query' => $this->query, |
| 97 | + 'type' => $this->type, |
| 98 | + 'fields' => $this->fields, |
| 99 | + 'boost' => $this->boost, |
| 100 | + ], |
| 101 | + ]; |
| 102 | + |
| 103 | + if ($this->operator) { |
| 104 | + $array['multi_match']['operator'] = $this->operator; |
| 105 | + } |
| 106 | + |
| 107 | + if ($this->fuzziness && $this->fuzziness->__toString()) { |
| 108 | + $array['multi_match']['fuzziness'] = $this->fuzziness->__toString(); |
| 109 | + } |
| 110 | + |
| 111 | + if ($this->analyzer) { |
| 112 | + $array['multi_match']['analyzer'] = $this->analyzer; |
| 113 | + } |
| 114 | + |
| 115 | + if ($this->minimumShouldMatch) { |
| 116 | + $array['multi_match']['minimum_should_match'] = $this->minimumShouldMatch; |
| 117 | + } |
| 118 | + |
| 119 | + return $array; |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments