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

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?

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

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

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

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?

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?

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

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

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