Search code examples
phpelasticsearchelastica

Include 'NULL' values in filter along with range filter in Elastica


I'm using Elastica and I need to create the filter that will get NULL values along with values that lower than 100.

For now my code looks like this:

$this->filter = $qb->query()->bool();
$this->filter->addShould($qb->query()->range('price', ['lte' => 100]));

It returns data with price lower than 100. I also need to get data with null values. So far I tried:

$this->filter->addMustNot($qb->query()->exists('price')); // returns 0 items
$this->filter->addShould($qb->query()->missing('price')); // doesn't work. Gives undefined query "missing" in Facade.php

Could someone help me with this issue? Or how to fix the problem with undefined query "missing" or to create another filter that will fit my needs. Thanks.


Solution

  • Used same range() to make it work how I need. So it looks like this:

    $this->filter->addMustNot($qb->query()->range('price', ['gte' => 100]));
    

    I'm using addMustNot so it filters all values that matches that filter (everything less than 100 will be returned, even if it's null value).