Complex Nested Query Where to Place Bool Match

独自空忆成欢 提交于 2021-01-29 09:22:26

问题


I have a complex index within Elastic that I need to query by 3 parameters.

Thanks to this answered question I am able to query by 2 of the 3 parameters, however the 3rd parameter is not at the same nested level as the other two.

The schema looks this..

The following query works for the 2 of the 3 parameters...

But the 3rd parameter is at a different level the the other two so this query does not return the expected document.

Given that the bool match query for "boundedContexts.aggregateRoot.aggregateType.name" is at a different nested level, how would I write this query so that it will query on that field ?


回答1:


This works...

{
"query": {
    "nested": {
        "path": "boundedContexts",
        "query": {
            "nested": {
                "path": "boundedContexts.aggregateRoots",
                "query": {
                    "bool": {
                            "must": [
                                    { "match": { "boundedContexts.aggregateRoots.aggregateType.name": "Aggregate" } },
                                    { "nested": {
                                    "path": "boundedContexts.aggregateRoots.modelMetaData",
                                    "query": {
                                        "bool": {
                                                "must": [
                                                    
                                                    { "match": { "boundedContexts.aggregateRoots.modelMetaData.modelReferenceId": "4e7c5c0e-93a7-4bf6-9705-cf1327760e21" } },
                                                    { "match": { "boundedContexts.aggregateRoots.modelMetaData.modelType.name": "AggregateRoot" } } 
                                                ]
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    },
    "size": 1,
    "sort": [   
    {
        "generatedDate": {
            "order": "desc"
        }
    }

] }



来源:https://stackoverflow.com/questions/65738179/complex-nested-query-where-to-place-bool-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!