Home > Back-end >  Elastic Search: I have an index mapping with a custom_data field that contains mutiple properties eg
Elastic Search: I have an index mapping with a custom_data field that contains mutiple properties eg

Time:09-23

Index Mapping

{
    "test": {
        "mappings": {
            "_doc": {
                "properties": {
                    "@timestamp": {
                        "type": "date"
                    },
                    "@version": {
                        "type": "long"
                    },
                    
                    "custom_data": {
                        "properties": {
                            "date1": {
                                "type": "date"
                            },
                            "number1": {
                                "type": "long"
                            },
                            "number2": {
                                "type": "long"
                            },
                            "paragraph1": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "picklist1": {
                                "type": "long"
                            },
                            "time1": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    },
                    "deleted": {
                        "type": "boolean"
                    },
                }
            }
        }
    }
}

What I am trying to do? I need to query documents by comparing month and day with given params. eg., if the params are {day: 20, month: 02}, and (Greater than) I need to retrieve all docs which has month > 2 or (month == 2 and day > 20).

Query that I am trying

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "script": {
                                                "script": {
                                                    "source": "(doc.custom_data.date1.date.getMonthOfYear() > params.month || (doc.custom_data.date1.date.getMonthOfYear() == params.month && doc.custom_data.date1.date.getDayOfMonth() > params.day))",
                                                    "params": {
                                                        "day": 17,
                                                        "month": 11
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "must_not": [
                            {
                                "match": {
                                    "deleted": true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Error

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
                    "(doc.custom_data.date1.date.getMonthOfYear() > params.month || (doc.custom_data.date1.date.getMonthOfYear() == params.month && doc.custom_data.date1.date.getDayOfMonth() > params.day))",
                    "    ^---- HERE"
                ],
                "script": "(doc.custom_data.date1.date.getMonthOfYear() > params.month || (doc.custom_data.date1.date.getMonthOfYear() == params.month && doc.custom_data.date1.date.getDayOfMonth() > params.day))",
                "lang": "painless"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "test",
                "node": "1AD0oCyfRAmvbPjRBaf_rQ",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
                        "(doc.custom_data.date1.date.getMonthOfYear() > params.month || (doc.custom_data.date1.date.getMonthOfYear() == params.month && doc.custom_data.date1.date.getDayOfMonth() > params.day))",
                        "    ^---- HERE"
                    ],
                    "script": "(doc.custom_data.date1.date.getMonthOfYear() > params.month || (doc.custom_data.date1.date.getMonthOfYear() == params.month && doc.custom_data.date1.date.getDayOfMonth() > params.day))",
                    "lang": "painless",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [custom_data] in mapping with types []"
                    }
                }
            }
        ]
    },
    "status": 500
}

Please ask if any other clarification is required.

CodePudding user response:

Your script needs to access the field like this:

doc['custom_data.date1'].date.getMonthOfYear()
  • Related