Home > front end >  ElasticSearch painless: No field found for * in mapping
ElasticSearch painless: No field found for * in mapping

Time:12-28

I am new to ElasticSearch and I try to figure out why painless doesn't recognise a field that exists in my documents but it complains that the field not found in the mapping.

Just to note that I am using ElastciSearch 8.5.3.

My index mapping looks like this:

{
    "companies": {
        "mapping": [
            "dynamic_templates": [
                // ... other properties here
                "branch_working_days_nested": {
                    "path_match": "branch.*.working_days",
                    "mapping": { "type": "nested" }
                }
            ],
            "properties": {
                "branch": {
                    "properties": {
                        // ... other properties here
                        "working_days": {
                            "properties": {
                                "friday": {
                                    "properties": {
                                        "close": { "type": "long" },
                                        "open": { "type": "long" },
                                        "isOpen": { "type": "long" },
                                    }
                                },
                                // The other week days here with same properties
                            }
                        }
                    }
                },
                // Other properties here
            }
        ]
    }

The a typical document I index is like that:

{
  // other properties here
  "branch": [
    {
      // other properties here
      "working_days": {
        "sunday": [
          {
            "open": -1,
            "close": -1,
            "id": "1YyMEHJe",
            "isOpen": false
          }
        ],
        "monday": [
          {
            "open": 830,
            "close": 1645,
            "id": "TkyMEHJe",
            "isOpen": true
          },
          {
            "open": 1800,
            "close": 2200,
            "id": "TkyMEHFa",
            "isOpen": true
          }
        ],
        // other days here
      }
    },
    {
      // other properties here
      "working_days": {
        "sunday": [
          {
            "open": -1,
            "close": -1,
            "id": "1YyMEHJe",
            "isOpen": false
          }
        ],
        "monday": [
          {
            "open": 1800,
            "close": 2200,
            "id": "TkyMEHFa",
            "isOpen": true
          }
        ],
        // other days here
      }
    }
  ],
  // other properties here
}

And then when I try to execute a Query like this:

GET companies/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": """
            def branches = doc['branch'];

            if (1 > 0) { return 1; } else { return 2; }
          """,
          "lang": "painless"
        },
        "order": "asc"
      }
    }
  ]
}

I get the following error:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
          "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:171)",
          "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          """branches = doc['branch'];
            """,
          "               ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 32,
          "start": 17,
          "end": 55
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "companies",
        "node": "HdHD67GdSTOeliS3pzI-hA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
            "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:171)",
            "[email protected]/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            """branches = doc['branch'];
            """,
            "               ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 32,
            "start": 17,
            "end": 55
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [branch] in mapping"
          }
        }
      }
    ]
  },
  "status": 400
}

Any idea why I have this nasty issue?

Worth mentioned that I have try to map the branch as array but this returns another error because by default it seems that all fields can be arrays.

I don't know what else I could try. Can someone help please?

CodePudding user response:

You are not able to access because branch field is type of nested and it will be not accessible using doc.

You can use below script and it will work for your usecase.

GET companies/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": """
            def branches = params._source.branch;
            for (nested in params._source.branch) {
              def a= nested;
              // your bussiness logic
            }
            if (1 > 0) { return 1; } else { return 2; }
          """,
          "lang": "painless"
        },
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "total_element": {
      "script": {
        "source": "params._source.branch.length"
      }
    }
  }
}
  • Related