Home > Software engineering >  Elastic Search: I have an index mapping with a df field that contains property d1. I want to query w
Elastic Search: I have an index mapping with a df field that contains property d1. I want to query w

Time:09-27

Index Mapping

{
    "test": {
        "mappings": {
            "_doc": {
                "properties": {
                    "@timestamp": {
                        "type": "date"
                    },
                    "@version": {
                        "type": "long"
                    },
                    
                    "df": {
                        "properties": {
                            "d1": {
                                "type": "date"
                            }
                        }
                    },
                    "deleted": {
                        "type": "boolean"
                    },
                }
            }
        }
    }
}

What I am trying to do? I need to access d1 property inside df field. But it's not working and gives error.

Query that I am trying

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "script": {
                                                "script": {
                                                    "source": "doc.df.d1.date.getMonthOfYear()"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "must_not": [
                            {
                                "match": {
                                    "deleted": true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

This gives error. I am adding these lines because SO won't let me submit the question without this extra text.

Please ask if any other clarification is required.

CodePudding user response:

Your script needs to access the field like this:

doc['df.d1'].date.getMonthOfYear()
  • Related