# How to query fields using script that are under nested structure and are disabled for indexing

**URL:** <https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884>\
**Category:** Elasticsearch\
**Created:** [January 6, 2020, 11:53am UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884 "2020-01-06T11:53:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_cutling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_cutling/32/50353_2.png) [@michael\_cutling](https://discuss.elastic.co/u/michael_cutling)\
**Post date:** [January 6, 2020, 11:53am UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/1 "2020-01-06T11:53:02Z")

</div>

how to query fields using script that are under nested structure and are disabled for indexing ?

For example, my mappings:

```
PUT test
{
  "mappings": {
  "doc": {
    "properties": {
      "id": { "type": "keyword" },
      "students": {
        "type": "nested",
        "properties": {
          "firstName": { "type": "text" },
          "lastName": { "type": "text" },
          "subjects": {
            "type" : "nested",
            "enabled": false
          }
        }
      }
    }
  }
}
}

PUT test/doc/1
{
  "id": "test123",
  "students": [
      {
        "firstName": "foo",
        "lastName": "baz",
        "subjects": [
          {
            "id": "1",
            "name": "english"
          }
        ]
      },
      {
        "firstName": "foo",
        "lastName": "bar",
        "subjects": [
          {
            "id": "2",
            "name": "science"
          },
          {
            "id": "1",
            "name": "english"
          }
        ]
      }
    ]
}

```

So for instance if `subjects` is an array of objects with below structure:

```
{
  "id": 1,
  "name": "english"
},
{
  "id": 2,
  "name": "science"
}
  
   ....

```

How can I search for all documents with a particular subject's name? For example: Find all students data that has subject as "english". Do I need to use scripted queries? Any example on how to this on ES 6.2?

---

<div class="post-metadata">

**Author:** ![michael\_cutling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_cutling/32/50353_2.png) [@michael\_cutling](https://discuss.elastic.co/u/michael_cutling)\
**Post date:** [January 9, 2020, 10:57am UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/2 "2020-01-09T10:57:40Z")

</div>

any help here? I am stuck in this for sometime now!!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 9, 2020, 4:30pm UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/3 "2020-01-09T16:30:12Z")

</div>

Have you set `enabled: false` in the `subjects` field, if you want to search on those fields?

See [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/enabled.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/enabled.html)

Also your query then actually needs to contain two `nested` queries, one for subjects and one for students.

---

<div class="post-metadata">

**Author:** ![michael\_cutling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_cutling/32/50353_2.png) [@michael\_cutling](https://discuss.elastic.co/u/michael_cutling)\
**Post date:** [January 10, 2020, 4:00am UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/4 "2020-01-10T04:00:09Z")

</div>

Thanks for the response. Yes "subjects" field is set as "enabled": false so these are not directly queryable using query. I think I need to wrote some script in query to fetch these fields. So need help in that regard or any other way to query these fields?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 10, 2020, 12:31pm UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/5 "2020-01-10T12:31:18Z")

</div>

I do not understand what you refer to with 'directly' queryable. Either a field can be queried or not, what kind of 'in between' do you have in mind?

---

<div class="post-metadata">

**Author:** ![michael\_cutling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_cutling/32/50353_2.png) [@michael\_cutling](https://discuss.elastic.co/u/michael_cutling)\
**Post date:** [January 10, 2020, 12:36pm UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/6 "2020-01-10T12:36:08Z")

</div>

As fields has enabled: false setting, these fields are not indexed and I cannot run a match/term query. Question here is how can I find documents satisfying a certain criteria on these fields. For example - How can I search for all documents with a particular subject's name in my example?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 10, 2020, 12:47pm UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/7 "2020-01-10T12:47:56Z")

</div>

you need to enable a field to be searchable. you could try only enabling one field and keep the rest disabled if that helps.

---

<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 7, 2020, 12:58pm UTC](https://discuss.elastic.co/t/how-to-query-fields-using-script-that-are-under-nested-structure-and-are-disabled-for-indexing/213884/8 "2020-02-07T12:58:22Z")

</div>

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