Trying to query on length of the nested field

Hi,
i have a mapping for an index of field resume.profile.locations, which is an nested field
Screenshot from 2023-09-26 14-45-45

Here, I need to perform a scripting query... I am getting error call no mapping for that field
but, I gave a mapping for that field

I don't know why I am encountering this error..
Any one please check that query and the response..

Thanks&Regards,

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with </> icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

Hi @dadoonet ,

Thanks for the response.

GET mr-profiles/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "resume.profile.locations",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "resume.profile.locations"
                    }
                  },
                  {
                    "script": {
                      "script": {
                        "source": "doc['resume.profile.locations'].size() > 2",
                        "lang": "painless"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}```


This the query.. what I performed I got,

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
"doc['resume.profile.locations'].size() > 2",
" ^---- HERE"
],
"script": "doc['resume.profile.locations'].size() > 2",
"lang": "painless",
"position": {
"offset": 4,
"start": 0,
"end": 42
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "mr-profiles",
"node": "9PF_FW5aQcmLIxDothVTOg",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
"org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
"doc['resume.profile.locations'].size() > 2",
" ^---- HERE"
],
"script": "doc['resume.profile.locations'].size() > 2",
"lang": "painless",
"position": {
"offset": 4,
"start": 0,
"end": 42
},
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [resume.profile.locations] in mapping"
}
}
}
]
},
"status": 400
}```
This is Error what i got..
Here, I gave a mapping to that field "resume.profile.locations" but, why it is showing an error

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Have a look at the Elastic Stack and Solutions Help · Forums and Slack | Elastic page. It contains also lot of useful information on how to ask for help.

Hi @dadoonet

this is the query,

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "resume.profile.locations",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "resume.profile.locations"
                    }
                  },
                  {
                    "script": {
                      "script": {
                        "source": "doc['resume.profile.locations'].size() > 2",
                        "lang": "painless"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

I got this Error,

 "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [resume.profile.locations] in mapping"
          }

But, the field "resume.profile.locations" have a mapping of type nested.
Is that query is correct or not. How can I solve this..

Please share at least a sample document. The best thing to do is to provide a script as I mentioned. So we can just copy/paste it and play around with it. In case you did not read the links I shared, here is what a script is:

DELETE index
PUT index/_doc/1
{
  "foo": "bar"
}
GET index/_search
{
  "query": {
    "match": {
      "foo": "bar"
    }
  }
}

Hi,

this the mapping

PUT index
{
"mapping":{
"properties":{
      "locations":{
        "type": "nested",
        "properties":{
          "state":{"type": "text"},
          "city":{"type": "text"}
        }
      }
    }
  }
}

Data:

PUT index/_doc/1
{
  "location": [{
    "state": "A",
    "city": "a"
  }]
}
PUT index/_doc/2
{
  "location": [{
    "state": "A",
    "city": "a"
  },
  {
    "state": "A",
    "city": "a"
  }]
}

Here, I need the length of the field "location" is more than 1. I performed a query,

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "location",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "location"
                    }
                  },
                  {
                    "script": {
                      "script": {
                        "source": "doc['location'].size() > 1",
                        "lang": "painless"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

When I performed this query I got,

"caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [resume.profile.locations] in mapping"
          }

Can you tell how can I solve this.. Is that query is correct or not

Thankyou,

It's actually the same question as posted at Getting No mapping Error right?

I answered there so let's keep the discussion in one single place. Thanks!

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