# Filter range on array of object

**URL:** <https://discuss.elastic.co/t/filter-range-on-array-of-object/312669>\
**Category:** Elasticsearch\
**Created:** [August 23, 2022, 4:14am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669 "2022-08-23T04:14:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jake_wong/32/98140_2.png) [@Jake\_Wong](https://discuss.elastic.co/u/Jake_Wong)\
**Post date:** [August 23, 2022, 4:14am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/1 "2022-08-23T04:14:32Z")

</div>

I have a documents with many nested fields and array of objects inside them. So when I perform search, how can I get the age range between 20 to 35?

```auto
{
    "company_name": "abc company",
    "detail": {
        "staff": [
            {
                "name": "tom",
                "gender": "male",
                "age": 25
            },
            {
                "name": "peter",
                "gender": "male",
                "age": 30
            },
            {
                "name": "mary",
                "gender": "female",
                "age": 22
            },
            {
                "name": "may",
                "gender": "female",
                "age": 18
            },
            {
                "name": "joe",
                "gender": "male",
                "age": 65
            },
            {
                "name": "ken",
                "gender": "male",
                "age": 33
            },
            {
                "name": "ada",
                "gender": "female",
                "age": 17
            },
            {
                "name": "june",
                "gender": "female",
                "age": 55
            }
        ]
    }
}

```

Also, Is there a way to get the age range between 40 to 70 and male only?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [August 23, 2022, 10:07am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/2 "2022-08-23T10:07:49Z")

</div>

Hi Jake. I assume you have a nested field mapping in your index for this structure? It looks like a nested query is what you are looking for. An example is included in the docs:

> **[Nested query | Elasticsearch Guide \[8.3\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.3/query-dsl-nested-query.html)**

---

<div class="post-metadata">

**Author:** ![Jake\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jake_wong/32/98140_2.png) [@Jake\_Wong](https://discuss.elastic.co/u/Jake_Wong)\
**Post date:** [August 23, 2022, 10:31am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/3 "2022-08-23T10:31:10Z")

</div>

hi carly, I checked my mappings seems no nested field.

```auto
{
    "staff": {
        "properties": {
            "age": {
                "type": "double"
            },
            "name": {
                "type": "keyword"
            },
            "gender": {
                "type": "keyword"
            }
        }
    }
}

```

If no nested field, should i create a new one??

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [August 23, 2022, 10:38am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/4 "2022-08-23T10:38:19Z")

</div>

Thanks for clarifying. No, if you're not using nested fields I wouldn't add to that as nested searches are computationally more intensive so there may be a performance impact.

Have you considered using a range query on your age attribute for the first query:

> **[Range query | Elasticsearch Guide \[8.3\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html)**

These can also be combined in a bool query with other matches as per your second question:

> **[Boolean query | Elasticsearch Guide \[8.3\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.3/query-dsl-bool-query.html)**

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Jake\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jake_wong/32/98140_2.png) [@Jake\_Wong](https://discuss.elastic.co/u/Jake_Wong)\
**Post date:** [August 23, 2022, 10:47am UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/5 "2022-08-23T10:47:16Z")

</div>

For my case, do you means my query should like this:

```auto
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "detail. staff. gender" : "male" }
      },
      "filter" : {
        "range" : {
          "detail.staff.age" : { "gte" : 40, "lte" : 70 }
        }
      },
      "boost" : 1.0
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [August 23, 2022, 12:16pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/6 "2022-08-23T12:16:47Z")

</div>

There's probably a few ways to do it, but yes that looks to be one possible way.

Boost is normally applied to a particular clause in the query, rather than the entire bool query itself. [This blog](https://www.elastic.co/blog/how-to-improve-elasticsearch-search-relevance-with-boolean-queries) does cover boosting if that helps.

---

<div class="post-metadata">

**Author:** ![Jake\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jake_wong/32/98140_2.png) [@Jake\_Wong](https://discuss.elastic.co/u/Jake_Wong)\
**Post date:** [August 23, 2022, 1:44pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/7 "2022-08-23T13:44:29Z")

</div>

I tried the query but not working, it still return the staff who is out of age range.

I am using ES 6.3

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 23, 2022, 2:15pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/8 "2022-08-23T14:15:36Z")

</div>

Elasticsearch will always return the full document that matched. You can as far as I know not get poarts of a document. If you want only the parts that match it might be better to store each staff member as a separate document.

---

<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:** [August 23, 2022, 2:37pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/9 "2022-08-23T14:37:25Z")

</div>

I think that something like inner hits would work in that case but I'd recommend following @Christian_Dahlqvist advice.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [August 23, 2022, 2:44pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/10 "2022-08-23T14:44:06Z")

</div>

I'll defer to @Christian_Dahlqvist and @dadoonet in their recommendations. One warning is that 6.3 is EOL, so it might be worth upgrading at least to 6.8. Details of EOL and maintenance support is [here](https://www.elastic.co/support/eol).

---

<div class="post-metadata">

**Author:** ![Jake\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jake_wong/32/98140_2.png) [@Jake\_Wong](https://discuss.elastic.co/u/Jake_Wong)\
**Post date:** [August 23, 2022, 4:21pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/11 "2022-08-23T16:21:10Z")

</div>

I think @Christian_Dahlqvist advice is the best way.

---

<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:** [September 20, 2022, 4:21pm UTC](https://discuss.elastic.co/t/filter-range-on-array-of-object/312669/12 "2022-09-20T16:21:17Z")

</div>

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