'Elasticsearch Query Filter Less Than Equals to Null Field

I have codes like this:

case "promo":
            productFitlerQueryBuilder.must(QueryBuilders.rangeQuery("itemVariants.priceDetail.totalPromoPercentage").gt(0));
            break;
case "-promo":
            productFitlerQueryBuilder.must(QueryBuilders.rangeQuery("itemVariants.priceDetail.totalPromoPercentage").lte(0));
            break;

It's filtering the result by totalPromoPercentage field which sometimes can be null. Is my code already proper to handle null possibility? Or is null already handled and converted to 0 by ElasticSearch?

If you guys have ElasticSearch documentation, it will be very helpful.



Solution 1:[1]

If you want to avoid null data in your output you can filter the data using exists query before the range query https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html

GET /_search
{
  "query": {
    "exists": {
      "field": "user"
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akmal