# How to match all item in nested array

**URL:** <https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873>\
**Category:** Elasticsearch\
**Created:** [January 11, 2019, 9:18am UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873 "2019-01-11T09:18:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kit\_Tsang](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@Kit\_Tsang](https://discuss.elastic.co/u/Kit_Tsang)\
**Post date:** [January 11, 2019, 9:18am UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/1 "2019-01-11T09:18:33Z")

</div>

The mapping:

```
 "working_hour": {
          "type": "nested",
          "properties": {
            "from": {
              "type": "long"
            }
          }
        }

```

Data example:

Record 1:

```
   "_source" : {
          "working_hour" : [
            {
              "from" : 3
            },
            {
              "from" : 6
            }
          ]
        }

```

record 2:

```
 "_source" : {
      "working_hour" : [
        {
          "from" : 6
        },
        {
          "from" : 6
        }
      ]
    }

```

query:

```
    "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "working_hour",
            "query": {
              "range": {
                "working_hour.from": {
                  "gte": 5,
                  "lte": 7
                }
              }
            }
          }
        }
      ]
    }
  }

```

My expected result only "record 2" will be returned. However, record 1 and record 2 return. How can I create a query?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 11, 2019, 9:52am UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/2 "2019-01-11T09:52:20Z")

</div>

Looks like a candiate for a [range field](https://www.elastic.co/blog/numeric-and-date-ranges-in-elasticsearch-just-another-brick-in-the-wall)?

---

<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 11, 2019, 10:08am UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/3 "2019-01-11T10:08:06Z")

</div>

This is working well:

```auto
DELETE test
PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "working_hour": {
          "type": "nested",
          "properties": {
            "from": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}
PUT test/_doc/1
{
  "working_hour": [
    {
      "from": 3
    },
    {
      "from": 6
    }
  ]
}
PUT test/_doc/2
{
  "working_hour": [
    {
      "from": 6
    },
    {
      "from": 6
    }
  ]
}
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "working_hour",
            "query": {
              "range": {
                "working_hour.from": {
                  "gte": 3,
                  "lte": 4
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

The response is:

```auto
{
  "took" : 59,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "working_hour" : [
            {
              "from" : 3
            },
            {
              "from" : 6
            }
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Kit\_Tsang](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@Kit\_Tsang](https://discuss.elastic.co/u/Kit_Tsang)\
**Post date:** [January 11, 2019, 2:05pm UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/4 "2019-01-11T14:05:01Z")

</div>

Sorry, I made a typo mistake the query should be the following:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "working_hour",
            "query": {
              "range": {
                "working_hour.from": {
                  "gte": 5,
                  "lte": 7
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

And expect record 2 return only

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 11, 2019, 2:39pm UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/5 "2019-01-11T14:39:20Z")

</div>

> [@Kit\_Tsang](#):
>
> And expect record 2 return only

I presume that's the "contains" operator then if you use the new range field.  
It will be more efficient to execute and easier to express these sorts of queries.

---

<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 8, 2019, 2:39pm UTC](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873/6 "2019-02-08T14:39:27Z")

</div>

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