Getting No mapping Error

Hii,

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

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

This is the data

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,

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

{
  "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,

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

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

Without nested, that will work with:

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:

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:

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:

{
  "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

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