"No field found for ["..."] in mapping" error with correct mapping

Hello, new to elastic here.
I am trying to run this:

GET /test/_search
{
  "query": {
    "match": {
      "doc_type": "test"
    }
  },
  "sort": {
    "_script" : {
      "type" : "number",
      "script" : {
          "lang": "painless",
          "source": "doc['price'].value + doc['price'].value"
      },
      "order" : "desc"
  }
  }
}

and I am getting this:

"type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:65)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:27)",
            "doc['price'].value + doc['price'].value",
            "    ^---- HERE"
          ],
          "script" : "doc['price'].value + doc['price'].value",
          "lang" : "painless",
          "position" : {
            "offset" : 4,
            "start" : 0,
            "end" : 39
          },
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [price] in mapping"
          }

this is part of the mapping (from _mapping request)

{
  "test": {
    "mappings": {
      "properties": {
        "doc": {
          "properties": {
            "itemId": {
              "type": "long"
            },
            "price": {
              "type": "float"
            }
        },
        "doc_type": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

My mapping seems correct, for what I am trying to do.
I have searched the internet and this forum for an answer, with no luck.
I am sorry if what I am trying may seem useless, but it's only a learning test.
I am using version 7.12

Thanks in advanced

Accessing a value in painless is the format you are using. doc['price'].value

What might be confusing is you have a doc in your mapping with price nested in there and that doesn't have anything to do with the doc in doc['price'].value.

I would try doc['doc.price'].value

you are absolutely right, obviously. thank you very much.
I am going to re-read the painless documentation

1 Like

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