Skip to content

Commit 17cd206

Browse files
authored
Merge pull request #55 from UrosPurtic/master
improve search for esv24 when search terms containg empty space,hyphe…
2 parents 298ca5c + 704edb3 commit 17cd206

7 files changed

Lines changed: 81 additions & 4 deletions

File tree

src/Common/Selection/Operator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Operator
1515
const IN = 'IN';
1616
const LIKE = 'LIKE';
1717
const LIKE_CI = 'LIKE_CI';
18+
const MULTI_LIKE_CI = 'MULTI_LIKE_CI';
1819
const LESS_THAN = 'LESS_THAN';
1920
const LESS_THAN_OR_EQUAL = 'LESS_THAN_OR_EQUAL';
2021
const NOT_EQUAL = 'NOT_EQUAL';
@@ -65,6 +66,7 @@ private function isValid()
6566
self::EXISTS,
6667
self::QUERY_STRING,
6768
self::CONSISTENT_RANDOM_KEY,
69+
self::MULTI_LIKE_CI,
6870
];
6971

7072
if (!in_array($this->symbol, $validSymbols)) {

src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function format($name, Operator $operator, ValueInterface $value)
7575
case Operator::LIKE_CI:
7676
$query = $this->getOperatorBasedOnEsVersion('LikeCIOperator', $name, $value);
7777
break;
78+
case Operator::MULTI_LIKE_CI:
79+
$query = $this->getOperatorBasedOnEsVersion('MultiLikeCIOperator', $name, $value);
80+
break;
7881
case Operator::MISSING:
7982
$query = new MissingOperator($name, $value);
8083
break;

src/Engine/Elasticsearch/ElasticsearchIdentity.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,11 @@ public function queryString($value)
209209
$this->operator(Operator::QUERY_STRING, new SingleValue($value));
210210
return $this;
211211
}
212+
213+
public function multiLikeCI($value)
214+
{
215+
$this->arrayException($value);
216+
$this->operator(Operator::MULTI_LIKE_CI, new SingleValue($value));
217+
return $this;
218+
}
212219
}

src/Engine/Elasticsearch/Operators/LikeCIOperator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function format()
2929
{
3030
return [
3131
QueryConnector::NAME_QUERY_STRING => [
32-
QueryConnector::NAME_QUERY_STRING_QUERY => $this->name . ":" . QueryConnector::WILDCARD . $this->value . QueryConnector::WILDCARD
32+
QueryConnector::NAME_QUERY_STRING_QUERY => $this->name . ":" . QueryConnector::ROUND_BRACKET_OPEN . QueryConnector::WILDCARD . $this->value . QueryConnector::WILDCARD . QueryConnector::ROUND_BRACKET_CLOSE
3333
]
3434
];
3535
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace G4\DataMapper\Engine\Elasticsearch\Operators;
4+
5+
use G4\DataMapper\Common\QueryOperatorInterface;
6+
use G4\DataMapper\Common\QueryConnector;
7+
use G4\DataMapper\Common\SingleValue;
8+
9+
/**
10+
* Class MultiLikeCIOperator
11+
*
12+
* @package G4\DataMapper\Engine\Elasticsearch\Operators
13+
*/
14+
class MultiLikeCIOperator extends LikeCIOperator implements QueryOperatorInterface
15+
{
16+
17+
public function __construct($name, SingleValue $value)
18+
{
19+
parent::__construct($name, $this->formatValue($value));
20+
}
21+
22+
public function format()
23+
{
24+
return parent::format(); // TODO: Change the autogenerated stub
25+
}
26+
27+
private function formatValue(SingleValue $value)
28+
{
29+
return new SingleValue(str_replace(["-","_"," "], "* *", $value));
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace G4\DataMapper\Engine\Elasticsearch\Version7\Operators;
4+
5+
use G4\DataMapper\Common\QueryOperatorInterface;
6+
use G4\DataMapper\Common\SingleValue;
7+
8+
/**
9+
* Class MultipleLikeCIOperator
10+
* dummy class to prevent multiplelikeoperator falling to default/version 2.4 es operator
11+
* @package G4\DataMapper\Engine\Elasticsearch\Operators
12+
*/
13+
class MultipleLikeCIOperator extends LikeCIOperator implements QueryOperatorInterface
14+
{
15+
public function __construct($name, SingleValue $value)
16+
{
17+
parent::__construct($name, $value);
18+
}
19+
20+
public function format()
21+
{
22+
return parent::format();
23+
}
24+
}

tests/unit/src/Engine/Elasticsearch/ElasticsearchComparisonFormatterTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,16 @@ public function testLikeCI()
179179
->method('getSymbol')
180180
->willReturn(Operator::LIKE_CI);
181181

182-
$this->assertEquals(['query_string' => ['query' => 'name:*lada*']], $this->comparisonFormatter->format('name', $this->operatorMock, new SingleValue('lada')));
182+
$this->assertEquals(['query_string' => ['query' => 'name:(*lada*)']], $this->comparisonFormatter->format('name', $this->operatorMock, new SingleValue('lada')));
183+
}
184+
185+
public function testMultiLikeCI()
186+
{
187+
$this->operatorMock->expects($this->any())
188+
->method('getSymbol')
189+
->willReturn(Operator::MULTI_LIKE_CI);
190+
191+
$this->assertEquals(['query_string' => ['query' => 'username:(*my** **name* *is** **rio*)']], $this->comparisonFormatter->format('username', $this->operatorMock, new SingleValue('my_name is-rio')));
183192
}
184193

185194
public function testLikeCIVersion7()
@@ -191,8 +200,8 @@ public function testLikeCIVersion7()
191200
->willReturn(Operator::LIKE_CI);
192201

193202
$this->assertEquals(
194-
['query_string' => ['query' => 'name:*lada*']],
195-
$comparisonFormatter->format('name', $this->operatorMock, new SingleValue('LADA'))
203+
['query_string' => ['query' => 'name:*dacia*']],
204+
$comparisonFormatter->format('name', $this->operatorMock, new SingleValue('DACIA'))
196205
);
197206
}
198207

@@ -234,4 +243,5 @@ public function testQueryStringVersion7()
234243
$comparisonFormatter->format('', $this->operatorMock, new SingleValue('username: *test* OR email: *test*'))
235244
);
236245
}
246+
237247
}

0 commit comments

Comments
 (0)