# Filter on array of object

**URL:** <https://discuss.elastic.co/t/filter-on-array-of-object/270004>\
**Category:** Kibana\
**Created:** [April 13, 2021, 12:27pm UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004 "2021-04-13T12:27:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [April 13, 2021, 12:27pm UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004/1 "2021-04-13T12:27:49Z")

</div>

I have a documents with many nested fields and array of objects inside them. So when I perform search, how can I get only the matching object inside the array of object, not the entire document?

```auto
{
  "name": "Olivia",
  "jobs": [
    {
      "title": "Accountant",
      "offices": [
        {
          "city": "Paris",
          "address": "Her adress"
        },
        {
          "city": "NYC",
          "address": "New adress"
        }
      ]
    },
    {
      "title": "Judge",
      "offices": [
        {
          "city": "Paris",
          "address": "Judge adress"
        },
        {
          "city": "NYC",
          "address": "New judge adress"
        }
      ]
    }
  ]
}

```

Jobs and offices are nested What I want to have in my search are: her name, jobs and offices where the city is "Paris", in normal search I get both of the offices where city are Paris and NYC

Is there a way to get only the offices having "Paris" as city?

---

<div class="post-metadata">

**Author:** ![azasypkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azasypkin/32/42216_2.png) [@azasypkin](https://discuss.elastic.co/u/azasypkin)\
**Post date:** [April 14, 2021, 9:48am UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004/2 "2021-04-14T09:48:41Z")

</div>

Hi @My-project-repositor,

I don't think it's possible, unless you change the way you store your data. The only slightly related thing that's available is [Source Filtering](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#get-source-filtering).

Best,  
Oleg

---

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [April 14, 2021, 10:13am UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004/3 "2021-04-14T10:13:52Z")

</div>

Thank you, I found the solution  
We can use inner hits

```auto
{
  "query": {
    "nested": {
      "path": "jobs.offices",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "jobs.offices.city": "Paris"
              }
            }
          ]
        }
      },
      "inner_hits": {
        "_source": [
          "jobs.offices.address"
        ]
      }
    }
  }
}

```

This link also helped [stackoverflow.com/a/63133940/4604579](https://stackoverflow.com/a/63133940/4604579)

---

<div class="post-metadata">

**Author:** ![azasypkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azasypkin/32/42216_2.png) [@azasypkin](https://discuss.elastic.co/u/azasypkin)\
**Post date:** [April 14, 2021, 10:16am UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004/4 "2021-04-14T10:16:39Z")

</div>

Awesome, thanks for sharing your finding!

---

<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:** [May 12, 2021, 10:17am UTC](https://discuss.elastic.co/t/filter-on-array-of-object/270004/5 "2021-05-12T10:17:17Z")

</div>

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