Sorting on Nested data type


(iluvcode) #1

I have a nested data type. Before displaying the result, i should sort the results based on menuLevelseq property in Nested data type.

What I am trying to below is, get all the items that belong to menunodeId 742 and sort the records based on menuLevelSeq in nested datatype for menu node id 742. Any help is greatly appreciated.

Query:

GET elasticsearch_mpwreg/menunodesearchentry/_search

{
	"from": 0,
	"size": 21,
	"sort": [{
		"itemMenuNodes.menuLevelSeq": {
			"order": "asc",
			"nested_path" : "itemMenuNodes",
			"nested_filter" : {
                "term" : { "itemMenuNodes.menuNodeId" : "742" }
             }
		}
	}],
	"query": {
		"bool": {
			"filter": [{
				"bool": {
					"must_not": [{
						"term": {
							"active": {
								"value": 3
							}
						}
					}]
				}
			}, {
				"match": {
					"shownInSearch": {
						"query": true
					}
				}
			}, {
				"term": {
					"itemMenuNodes.menuNodeId":  742
					
				}
			}]
		}
	}
}

Index data:

{
	"_index": "elasticsearch_mpwreg",
	"_type": "menunodesearchentry",
	"_id": "162488",
	"_score": null,
	"_source": {
		"itemId": 162488,

		"itemMenuNodes": [{
				"menuNodeId": 219,
				"nodeText": "Casual",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					4,
					10,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 742,
				"nodeText": "Baseball Hats & Visors",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					3,
					4,
					6,
					6,
					6,
					6,
					7,
					7,
					7,
					7,
					10,
					11
				],
				"menuLevelSeq": -1
			},
			{
				"menuNodeId": 1369,
				"nodeText": "Macks Prairie Wings",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 2342,
				"nodeText": "MPW Hats & Caps",
				"itemId": 0,
				"menuNodeTextTypeId": [
					3
				],
				"menuLevelSeq": 8
			}
		],
		"hasMoreRecord": false,
		"contentTypeId": 0,
		"itemNo": "MPW111"
	},
	"sort": [-1]
}, {
	"_index": "elasticsearch_mpwreg",
	"_type": "menunodesearchentry",
	"_id": "247990",
	"_score": null,
	"_source": {
		"itemId": 247990,
		"itemMenuNodes": [{
				"menuNodeId": 219,
				"nodeText": "Casual",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					4,
					10,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 742,
				"nodeText": "Baseball Hats & Visors",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					3,
					4,
					6,
					6,
					6,
					6,
					7,
					7,
					7,
					7,
					10,
					11
				],
				"menuLevelSeq": 9
			},
			{
				"menuNodeId": 1369,
				"nodeText": "Macks Prairie Wings",
				"itemId": 0,
				"menuNodeTextTypeId": [
					1,
					1,
					3,
					11
				],
				"menuLevelSeq": 0
			},
			{
				"menuNodeId": 2342,
				"nodeText": "MPW Hats & Caps",
				"itemId": 0,
				"menuNodeTextTypeId": [
					3
				],
				"menuLevelSeq": 10
			}
		],
		"id": 247990,
		"hasMoreRecord": false,
		"contentTypeId": 0,
		"itemNo": "MPW111963"
	},
	"sort": [
		0
	]
}

(Adrien Grand) #2

At first sight the query looks good, what is the problem?


(iluvcode) #3

@jpountz ,
itemMenuNodes was not a nested data type in the above query. So I changed the itemMenuNodes to nested data type. Sorting works after changing the itemMenuNodes to nested data type but term query on "itemmenunodes.menunodeid" is not working.


(Adrien Grand) #4

You need to wrap it under a nested query.


(system) #5

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