# How to filter null objects?

**URL:** <https://discuss.elastic.co/t/how-to-filter-null-objects/116553>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 11:40pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553 "2018-01-22T23:40:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![allansene](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/allansene/32/26999_2.png) [@allansene](https://discuss.elastic.co/u/allansene)\
**Post date:** [January 22, 2018, 11:40pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/1 "2018-01-22T23:40:21Z")

</div>

I have to filter payloads like this on an ElasticSearch query:

```
{
  "bestPrices": {
    "cia1": {},
    "cia2": {}
  }
}

```

I must return only results like:

```
{
  "bestPrices": {
    "cia1": {
      "gol": {
        "price1": 799,
        "price2": null,
        "miles": 25000
      }
    },
    "cia2": {
      "gol": {
        "price1": null,
        "price2": null,
        "miles": null
      }
    }
  }
}

```

I'm trying exists query, but seems that it do not apply to this particular situation:

```
{
  "exists": {
    "field": "searchIntention.bestSalePrices.cia1"
  }
}

```

I'm using ElasticSearch 6.1

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 23, 2018, 7:54am UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/2 "2018-01-23T07:54:54Z")

</div>

This is working well:

```auto
DELETE test
PUT test/doc/1
{
  "bestPrices": {
    "cia1": {
      "gol": {
        "price1": 799,
        "price2": null,
        "miles": 25000
      }
    },
    "cia2": {
      "gol": {
        "price1": null,
        "price2": null,
        "miles": null
      }
    }
  }
}
PUT test/doc/2
{
  "bestPrices": {
    "cia2": {
      "gol": {
        "price1": null,
        "price2": null,
        "miles": null
      }
    }
  }
}
GET test/_search
{
  "query": {
    "exists": {
      "field": "bestPrices.cia1"
    }
  }
}

```

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "bestPrices": {
            "cia1": {
              "gol": {
                "price1": 799,
                "price2": null,
                "miles": 25000
              }
            },
            "cia2": {
              "gol": {
                "price1": null,
                "price2": null,
                "miles": null
              }
            }
          }
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![allansene](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/allansene/32/26999_2.png) [@allansene](https://discuss.elastic.co/u/allansene)\
**Post date:** [January 25, 2018, 1:24pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/3 "2018-01-25T13:24:04Z")

</div>

Thanks @dadoonet! Actually, the test was something like

```
PUT test/doc/2
    {
      "bestPrices": {
        "cia2": {
          "gol": {}
        }
      }
    }

```

But works anyway... 🙂 Now I have another problem...

My documents may have this mapping:

```
PUT test/doc/3
{
  "bestPrices": {
    "cia2": {
      "other_name": {
        "price1": 200,
        "price2": 300,
        "miles": null
      }
    }
  }
}

```

And I must filter `prices` greater than 0. Something like that, does not work 😕 :

```
GET test/_search
{
  "query": {
    "range": {
      "bestPrices.cia2.*.price1": {
        "gte": 1
      }
    }
  }
}

```

Can I filter `inner` fields like these? Should I open a new topic?

Thanks again 👍

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 25, 2018, 2:22pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/4 "2018-01-25T14:22:42Z")

</div>

If possible I'd solve that at index time using `copy_to` or by computing a sum which you can also do with an ingest pipeline with a script processor.

---

<div class="post-metadata">

**Author:** ![allansene](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/allansene/32/26999_2.png) [@allansene](https://discuss.elastic.co/u/allansene)\
**Post date:** [January 25, 2018, 8:54pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/5 "2018-01-25T20:54:42Z")

</div>

Yeah... I gonna implement some transformation to aggregate this result before indexing the document. Thanks David 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 22, 2018, 8:55pm UTC](https://discuss.elastic.co/t/how-to-filter-null-objects/116553/6 "2018-02-22T20:55:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
