0

parent index : candidate , type: profile; child index : candidate , type: application

I have parent data like this in elasticserch

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "candidate",
                "_type": "profile",
                "_id": "64afc27d87209b7083e26a6b",
                "_score": 1.0,
                "_source": {
                    "overview": {
                        "mobile_not_analyze": "141222222",
                        "country_code": "86",
                        "city": {},
                        "name_analyzed": "最高学历61",
                        "degree": 6,
                        "mobile": "141222222",
                        "name": "最高学历61"
                    },
                    "educations_search": [
                        {
                            "college": {
                                "code": "43",
                                "name": "发生过撒啊啊"
                            },
                            "start_time": "2022-01-01",
                            "major": {},
                            "college_tags": [],
                            "degree": {
                                "code": "6",
                                "name": "硕士"
                            },
                            "end_time": "2023-06-01"
                        },
                        {
                            "college": {
                                "code": "43",
                                "name": "阿斯弗"
                            },
                            "start_time": "2023-01-01",
                            "major": {},
                            "college_tags": [],
                            "degree": {
                                "code": "4",
                                "name": "大专"
                            },
                            "end_time": "2023-02-01"
                        }
                    ]
                }
            }
        ]
    }
}

What I want to do is filter out using conditions like max degree_code and end_time between 1685548800000 and 1685635200000.What should i do? i want to get the data use query which like :

{
    "size": 20,
    "track_scores": true,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "terms": {
                                    "company_id": [
                                        "39978"
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "has_parent": {
                        "query": {
                            "bool": {
                                "filter": {
                                    "nested": {
                                        "path": "educations_search",
                                        "query": {
                                            "bool": {
                                                "must": [
                                                    {
                                                        "range": {
                                                            "educations_search.end_time": {
                                                                "include_lower": true,
                                                                "include_upper": true,
                                                                "from": 1685548800000,
                                                                "to": 1685635200000
                                                            }
                                                        }
                                                    },
                                                    {
                                                        "script": {
                                                            "script": {
                                                                "inline": "int maxDegreeCode = -1;def maxDegreeObject ;def startDate = new Date(1698748800000); def endDate = new Date(1699094400000); for (education in doc['educations_search'].value) {int degreeCode = Integer.parseInt(education['degree']['code'].value); if (degreeCode > maxDegreeCode) {     maxDegreeCode = degreeCode;     maxDegreeObject = education }}; if (maxDegreeObject != null) {def maxDegreeEndFilter = new Date(maxDegreeObject['end_time'].value); return maxDegreeCode == Integer.parseInt(maxDegreeObject['degree']['code'].value)         && maxDegreeEndFilter >= startDate && maxDegreeEndFilter <= endDate} else {return false}"
                                                            }
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    }
                                },
                                "must": {
                                    "match": {
                                        "company_id": {
                                            "query": 39978,
                                            "type": "boolean"
                                        }
                                    }
                                }
                            }
                        },
                        "parent_type": "profile"
                    }
                }
            ]
        }
    },
    "from": 0
}

but it's not working and return an empty an error:

"caused_by": {
                    "type": "illegal_argument_exception",
                    "reason": "No field found for [_parent.educations_search] in mapping with types [application]"
                }

It seems like instead of applying the script function to the and then passing its result to the child, ElasticSearch is trying to apply the condition itself to the child,causing the error. What am I wrong here? How can I query these child documents based on a parent field? I still use Script like this:

{
    "script": {
        "script": {
            "inline": "int maxDegreeCode = -1; def maxDegreeObject; def startDate = new Date(1698748800000); def endDate = new Date(1699094400000); for (education in _source.educations_search) {int degreeCode = Integer.parseInt(education.degree.code); if (degreeCode > maxDegreeCode) {     maxDegreeCode = degreeCode; maxDegreeObject = education }}; if (maxDegreeObject != null) {def maxDegreeEndFilter = new Date(maxDegreeObject.end_time); return maxDegreeCode == Integer.parseInt(maxDegreeObject.degree.code) && maxDegreeEndFilter >= startDate && maxDegreeEndFilter <= endDate} else {return false}"
        }
    }
}

but it's return an empty.

ZhuXJun
  • 25
  • 5

0 Answers0