# Querying on multiple flattened sub-fields for an array of items

**URL:** <https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990>\
**Category:** Elasticsearch\
**Created:** [January 20, 2022, 8:00pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990 "2022-01-20T20:00:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robert-n-stewart](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@robert-n-stewart](https://discuss.elastic.co/u/robert-n-stewart)\
**Post date:** [January 20, 2022, 8:00pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/1 "2022-01-20T20:00:05Z")

</div>

If I have a flattened or object field named X, which is an array of objects with subfields A and B, how can I query for an item that has specific values for both A and B?

E.g. if I run the following:

```auto
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "X.A": "v1"
               }
            },
            {
               "term": {
                  "X.B": "v2"
               }
            }
         ]
      }
   }
}

```

It'll include hits like the following hit which I don't want:

```auto
{"x": [
   {
      "A": "v1",
      "B": "v0",
   },
   {
      "A": "v0",
      "B": "v2",
   }
]}

```

I only want hits like:

```auto
{"X": [
   {
      "A": "v1",
      "B": "v2"
   }
]}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 21, 2022, 4:41am UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/2 "2022-01-21T04:41:29Z")

</div>

> [@robert-n-stewart](#):
>
> ```auto
> {"x": [
> {
> "A": "v1",
> "B": "v0",
> },
> {
> "A": "v0",
> "B": "v2",
> }
> ]}
> 
> ```

Objects in array has no meaning otherwise it is in a nsted field. It seems you have to use nested field type to get the desired result.

---

<div class="post-metadata">

**Author:** ![robert-n-stewart](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@robert-n-stewart](https://discuss.elastic.co/u/robert-n-stewart)\
**Post date:** [January 21, 2022, 4:02pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/3 "2022-01-21T16:02:49Z")

</div>

Thats too bad. Nested fields blow up the document count as they create a document for every sub-object

---

<div class="post-metadata">

**Author:** ![robert-n-stewart](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@robert-n-stewart](https://discuss.elastic.co/u/robert-n-stewart)\
**Post date:** [January 21, 2022, 4:06pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/4 "2022-01-21T16:06:00Z")

</div>

I wonder why they can't order the internal arrays used per subfield to be in sync with the order of objects in the array

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 21, 2022, 5:00pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/5 "2022-01-21T17:00:01Z")

</div>

In my guess, one of the possible reason is that the internal structure for index of multi-value field is not array.

---

<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 18, 2022, 5:00pm UTC](https://discuss.elastic.co/t/querying-on-multiple-flattened-sub-fields-for-an-array-of-items/294990/6 "2022-02-18T17:00:43Z")

</div>

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