# Getting No mapping Error

**URL:** <https://discuss.elastic.co/t/getting-no-mapping-error/344283>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring, painless, runtime-fields\
**Created:** [October 3, 2023, 6:16am UTC](https://discuss.elastic.co/t/getting-no-mapping-error/344283 "2023-10-03T06:16:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Konagala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_konagala/32/120215_2.png) [@Anand\_Konagala](https://discuss.elastic.co/u/Anand_Konagala)\
**Post date:** [October 3, 2023, 6:16am UTC](https://discuss.elastic.co/t/getting-no-mapping-error/344283/1 "2023-10-03T06:16:46Z")

</div>

Hii,

I tried a lot of queries.. but, Its not works.. It through an error.. Can anyone try this  
Here is my mapping,

```auto
PUT array_sort
{
  "mappings": {
    "properties": {
      "Skills": {
        "type": "nested",
        "properties": {
          "text": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

```

This is the data

```auto
POST array_sort/_doc/1
{
  "Skills": [
    {"text": "java"},
    {"text": "elasticsearch"}
  ]
}

POST array_sort/_doc/2
{
  "Skills": [
    {"text": "python"},
    {"text": "ruby"},
    {"text": "javascript"}
  ]
}

POST array_sort/_doc/3
{
  "Skills": [
    {"text": "sql"},
    {"text": "html"},
    {"text": "css"}
  ]
}

```

Here is the query for finding that array size more then two,

```auto
GET array_sort/_search
{
  "query": {
    "nested": {
      "path": "Skills",
      "query": {
        "script": {
          "script": {
            "source": "doc['Skills'].length > 3",
            "lang": "painless"
          }
        }
      }
    }
  }
}

```

I tried a lot of queries But, I am getting an error.. Here is an error

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          """if (doc['Skills'].length() > 2) {
                """,
          " ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 23,
          "start": 15,
          "end": 65
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "array_sort",
        "node": "x4tf2IsOSUu4VVsm2zVwGA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            """if (doc['Skills'].length() > 2) {
                """,
            " ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 23,
            "start": 15,
            "end": 65
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [Skills] in mapping"
          }
        }
      }
    ]
  },
  "status": 400
}

```

Can you anyone try this and inform.. Why I am encountering this Error. If you find anything let me know.

Thank you,

---

<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:** [October 3, 2023, 12:20pm UTC](https://discuss.elastic.co/t/getting-no-mapping-error/344283/2 "2023-10-03T12:20:26Z")

</div>

Thank you for the reproduction script. It's helpful.

As you are using nested documents, it's tricky.

Without nested, that will work with:

```auto
PUT array_sort
{
  "mappings": {
    "properties": {
      "Skills": {
        "type": "object",
        "properties": {
          "text": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
PUT array_sort/_doc/1
{
  "Skills": [
    {"text": "java"},
    {"text": "elasticsearch"}
  ]
}
PUT array_sort/_doc/2
{
  "Skills": [
    {"text": "python"},
    {"text": "ruby"},
    {"text": "javascript"}
  ]
}
PUT array_sort/_doc/3
{
  "Skills": [
    {"text": "sql"},
    {"text": "html"},
    {"text": "css"}
  ]
}
GET array_sort/_search
{
  "query": {
    "script": {
      "script": {
        "source": "doc['Skills.text'].length > 2",
        "lang": "painless"
      }
    }
  }
}

```

`Skills` is not stored as is so you can not really access it using `doc_values`. But `text` is a `keyword` field and you can access it when it's flattened. I did not find a way to access each nested doc and I'm not sure it's doable.

And in all cases, it's not recommended as this would dramatically slow down your requests.

Instead, I'd encourage to count the number of elements in your array and add this information to your document, like:

```auto
PUT array_sort/_doc/1
{
  "SkillsSize": 2,
  "Skills": [
    {"text": "java"},
    {"text": "elasticsearch"}
  ]
}
PUT array_sort/_doc/2
{
  "SkillsSize": 3,
  "Skills": [
    {"text": "python"},
    {"text": "ruby"},
    {"text": "javascript"}
  ]
}
PUT array_sort/_doc/3
{
  "SkillsSize": 3,
  "Skills": [
    {"text": "sql"},
    {"text": "html"},
    {"text": "css"}
  ]
}

```

That way it's trivial to search for the size of the array...

Note that you can do that as well with an ingest pipeline, in case you can not do it in your application:

```auto
POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": "ctx.SkillsSize = ctx.Skills.length"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "Skills": [
          { "text": "java" },
          { "text": "elasticsearch" }
        ]
      }
    },
    {
      "_source": {
        "Skills": [
          {"text": "python"},
          {"text": "ruby"},
          {"text": "javascript"}
        ]
      }
    },
    {
      "_source": {
        "Skills": [
          {"text": "sql"},
          {"text": "html"},
          {"text": "css"}
        ]
      }
    }
  ]
}

```

This gives:

```auto
{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "Skills": [
            {
              "text": "java"
            },
            {
              "text": "elasticsearch"
            }
          ],
          "SkillsSize": 2
        },
        "_ingest": {
          "timestamp": "2023-10-03T12:19:56.810519981Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "Skills": [
            {
              "text": "python"
            },
            {
              "text": "ruby"
            },
            {
              "text": "javascript"
            }
          ],
          "SkillsSize": 3
        },
        "_ingest": {
          "timestamp": "2023-10-03T12:19:56.810563235Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "Skills": [
            {
              "text": "sql"
            },
            {
              "text": "html"
            },
            {
              "text": "css"
            }
          ],
          "SkillsSize": 3
        },
        "_ingest": {
          "timestamp": "2023-10-03T12:19:56.81056849Z"
        }
      }
    }
  ]
}

```

HTH

---

<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:** [October 31, 2023, 12:21pm UTC](https://discuss.elastic.co/t/getting-no-mapping-error/344283/3 "2023-10-31T12:21:24Z")

</div>

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