Skip to content

Commit 1f13069

Browse files
committed
MultiMatch query type
- you can search in multified mapping type - with configurable match type
1 parent fc7a1db commit 1f13069

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

src/Query/Match/MultiMatchType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Query\Match;
4+
5+
6+
class MultiMatchType
7+
{
8+
9+
public const BEST_FIELDS = 'best_fields';
10+
public const MOST_FIELDS = 'most_fields';
11+
public const CROSS_FIELDS = 'cross_fields';
12+
public const PHRASE = 'phrase';
13+
public const PHRASE_PREFIX = 'phrase_prefix';
14+
public const BOOL_PREFIX = 'bool_prefix';
15+
16+
17+
public const TYPES = [
18+
self::BEST_FIELDS => self::BEST_FIELDS,
19+
self::MOST_FIELDS => self::MOST_FIELDS,
20+
self::CROSS_FIELDS => self::CROSS_FIELDS,
21+
self::PHRASE => self::PHRASE,
22+
self::PHRASE_PREFIX => self::PHRASE_PREFIX,
23+
self::BOOL_PREFIX => self::BOOL_PREFIX,
24+
];
25+
26+
}

src/Query/MultiMatch.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)