Multi-level nested query


#1

Hi,
I have a below sample piece of data,
"indCodes":{

                            "industryCodeTypes": [
                                {
                                    "typeDBCode": 1016,
                                    "typeDescription": "US SIC",
                                    "industryCode": [
                                        {
                                            "code": 1099,
                                            "description": "Metal mining-other metal ores",
                                            "priority": 1
                                        }
                                    ]
                                },
                                {
                                    "typeDBCode": 19295,
                                    "typeDescription": "SIC 2007",
                                    "industryCode": [
                                        {
                                            "code": 70100,
                                            "description": "Activities of head offices",
                                            "priority": 1
                                        },
                                        {
                                            "code": 7290,
                                            "description": "Mining of other non-ferrous metal ores",
                                            "priority": 2
                                        },
                                        {
                                            "code": 7000,
                                            "description": "Mining of metal ores",
                                            "priority": 3
                                        }
                                    ]
                                }
                            ]
                        }

So for the above sample data I have prepared below mapping,
"indCodes":{

                                    "properties": {
                                        "industryCodeTypes": {
                                            "type": "nested",
                                            "properties": {
                                                "industryCode": {
                                                    "type": "nested",
                                                    "properties": {
                                                        "code": {
                                                            "type": "long"
                                                        },
                                                        "description": {
                                                            "type": "text",
                                                            "fields": {
                                                                "keyword": {
                                                                    "type": "keyword",
                                                                    "ignore_above": 256
                                                                }
                                                            }
                                                        },
                                                        "priority": {
                                                            "type": "long"
                                                        }
                                                    }
                                                },
                                                "typeDescription": {
                                                    "type": "text",
                                                    "fields": {
                                                        "keyword": {
                                                            "type": "keyword",
                                                            "ignore_above": 256
                                                        }
                                                    }
                                                },
                                                "typeDBCode": {
                                                    "type": "long"
                                                }
                                            }
                                        }
                                    }

}
is the above mapping correct as per my sample data, if yes then, i need below scenarios
if i give typeDBCode: 1016 and code: 1099 - I should get one record as output which is coming
if i give typeDBCode: 19295 and code: 1099 - I should not get records but still i am getting.
Below is my query,
{
"query":{
"bool":{
"must":[{
"nested": {
"path": "standardisedFinancials.identification.industryCodes.industryCodeTypes",
"query" : {
"bool" : {
"must" : [
{ "match" : {"standardisedFinancials.identification.industryCodes.industryCodeTypes.typeDnBCode" : "19295"} }
]
}
}
}
},
{
"nested": {
"path": "standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode",
"query" : {
"bool" : {
"must" : [
{ "terms" : {"standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode.code" : ["6111","1099"]} },
{ "match" : {"standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode.priority" : "1"} }
]
}
}
}
}]
}
}
}
Kindly let me know where i did mistake.
Thanks in advance.

Thanks & Regards,
Ram Prasad G


#2

Any clues please.


(David Pilato) #3

Read this and specifically the "Also be patient" part.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.


#4

Hi,
I have a below sample piece of data,

{
	"indCodes": {
		"industryCodeTypes": [{
				"typeDBCode": 1016,
				"typeDescription": "US SIC",
				"industryCode": [{
					"code": 1099,
					"description": "Metal mining-other metal ores",
					"priority": 1
				}]
			},
			{
				"typeDBCode": 19295,
				"typeDescription": "SIC 2007",
				"industryCode": [{
						"code": 70100,
						"description": "Activities of head offices",
						"priority": 1
					},
					{
						"code": 7290,
						"description": "Mining of other non-ferrous metal ores",
						"priority": 2
					},
					{
						"code": 7000,
						"description": "Mining of metal ores",
						"priority": 3
					}
				]
			}
		]
	}
}

So for the above sample data I have prepared below mapping,

{
	"indCodes": {
		"properties": {
			"industryCodeTypes": {
				"type": "nested",
				"properties": {
					"industryCode": {
						"type": "nested",
						"properties": {
							"code": {
								"type": "long"
							},
							"description": {
								"type": "text",
								"fields": {
									"keyword": {
										"type": "keyword",
										"ignore_above": 256
									}
								}
							},
							"priority": {
								"type": "long"
							}
						}
					},
					"typeDescription": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					},
					"typeDBCode": {
						"type": "long"
					}
				}
			}
		}
	}
}

is the above mapping correct as per my sample data, if yes then, i need below scenarios
if i give typeDBCode: 1016 and code: 1099 - I should get one record as output which is coming
if i give typeDBCode: 19295 and code: 1099 - I should not get records but still i am getting.
Below is my query,

{
	"query": {
		"bool": {
			"must": [{
					"nested": {
						"path": "standardisedFinancials.identification.industryCodes.industryCodeTypes",
						"query": {
							"bool": {
								"must": [{
									"match": {
										"standardisedFinancials.identification.industryCodes.industryCodeTypes.typeDnBCode": "19295"
									}
								}]
							}
						}
					}
				},
				{
					"nested": {
						"path": "standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode",
						"query": {
							"bool": {
								"must": [{
										"terms": {
											"standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode.code": ["6111", "1099"]
										}
									},
									{
										"match": {
											"standardisedFinancials.identification.industryCodes.industryCodeTypes.industryCode.priority": "1"
										}
									}
								]
							}
						}
					}
				}
			]
		}
	}
}

Kindly let me know where i did mistake.
Thanks in advance.

Thanks & Regards,
Ram Prasad G


#5

Hi,

Could anyone please reply me with some solution.

Thanks,
Ram Prasad G


#6

Hi All,

thank you issue is fixed and able to write query for my requirment.

Thanks,
Ram Prasad G


(system) #7

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.