Skip to content

Commit aa37bdb

Browse files
committed
feat(elastic): add post-filter functionality
- Introduced postFilter property to ElasticQuery - Added methods to manage post-filters in query construction - Updated query serialization to include post-filters
1 parent f32b870 commit aa37bdb

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/ElasticQuery.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
1212

1313
private \Spameri\ElasticQuery\Filter\FilterCollection $filter;
1414

15+
private \Spameri\ElasticQuery\Query\QueryCollection $postFilter;
16+
1517
private \Spameri\ElasticQuery\Options\SortCollection $sort;
1618

1719
private \Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation;
@@ -21,6 +23,7 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
2123
public function __construct(
2224
\Spameri\ElasticQuery\Query\QueryCollection|null $query = null,
2325
\Spameri\ElasticQuery\Filter\FilterCollection|null $filter = null,
26+
\Spameri\ElasticQuery\Query\QueryCollection|null $postFilter = null,
2427
\Spameri\ElasticQuery\Options\SortCollection|null $sort = null,
2528
\Spameri\ElasticQuery\Aggregation\AggregationCollection|null $aggregation = null,
2629
private \Spameri\ElasticQuery\Highlight|null $highlight = null,
@@ -34,6 +37,9 @@ public function __construct(
3437
if ($filter === null) {
3538
$filter = new \Spameri\ElasticQuery\Filter\FilterCollection();
3639
}
40+
if ($postFilter === null) {
41+
$postFilter = new \Spameri\ElasticQuery\Query\QueryCollection();
42+
}
3743
if ($sort === null) {
3844
$sort = new \Spameri\ElasticQuery\Options\SortCollection();
3945
}
@@ -46,6 +52,7 @@ public function __construct(
4652

4753
$this->query = $query;
4854
$this->filter = $filter;
55+
$this->postFilter = $postFilter;
4956
$this->sort = $sort;
5057
$this->aggregation = $aggregation;
5158
$this->options = $options;
@@ -106,6 +113,18 @@ public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $l
106113
}
107114

108115

116+
public function postFilter(): \Spameri\ElasticQuery\Query\QueryCollection
117+
{
118+
return $this->postFilter;
119+
}
120+
121+
122+
public function addPostFilterMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void
123+
{
124+
$this->postFilter->must()->add($leafQuery);
125+
}
126+
127+
109128
public function addFilter(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void
110129
{
111130
$this->filter->must()->add($leafQuery);
@@ -136,6 +155,11 @@ public function toArray(): array
136155
$array['query']['bool']['filter'] = $filterArray;
137156
}
138157

158+
$postFilterArray = $this->postFilter->toArray();
159+
if ($postFilterArray) {
160+
$array['post_filter'] = $postFilterArray;
161+
}
162+
139163
$sortArray = $this->sort->toArray();
140164
if ($sortArray) {
141165
$array['sort'] = $sortArray;

0 commit comments

Comments
 (0)