Skip to content

Commit 5119fb2

Browse files
kmiladisaimaz
authored andcommitted
Add not equal (MUST_NOT) to findBy repository methode: attribute must beginning by "!" (#797)
* update mapping cache issue (#726) * version bump to 5.1 for master branch alias * reset composer.json
1 parent 59438d1 commit 5119fb2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Service/Repository.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ONGR\ElasticsearchBundle\Result\ArrayIterator;
1515
use ONGR\ElasticsearchBundle\Result\RawIterator;
1616
use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery;
17+
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
1718
use ONGR\ElasticsearchDSL\Search;
1819
use ONGR\ElasticsearchDSL\Sort\FieldSort;
1920
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
@@ -159,8 +160,16 @@ public function findBy(
159160
}
160161

161162
foreach ($criteria as $field => $value) {
163+
if (preg_match('/^!(.+)$/', $field)) {
164+
$boolType = BoolQuery::MUST_NOT;
165+
$field = preg_replace('/^!/', '', $field);
166+
} else {
167+
$boolType = BoolQuery::MUST;
168+
}
169+
162170
$search->addQuery(
163-
new QueryStringQuery(is_array($value) ? implode(' OR ', $value) : $value, ['default_field' => $field])
171+
new QueryStringQuery(is_array($value) ? implode(' OR ', $value) : $value, ['default_field' => $field]),
172+
$boolType
164173
);
165174
}
166175

Tests/Functional/Service/RepositoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ public function getFindByData()
149149
1,
150150
];
151151

152+
// Case #7 find when title not equal foo.
153+
$out[] = [
154+
[2, 3, 4],
155+
['!title' => 'foo'],
156+
];
157+
158+
// Case #8 find when title not equal foo or title not equal bar.
159+
$out[] = [
160+
[3, 4],
161+
[
162+
'!title' => [
163+
'foo',
164+
'bar',
165+
],
166+
],
167+
];
168+
152169
return $out;
153170
}
154171

0 commit comments

Comments
 (0)