Querying embedded arrays vs named objects


(Richard Spence) #1

I'm trying to decide how best to model data between these 2 options:

Explicitly named objects....

"categories" : {
		"food" : {
			"categoryName" : "Food",
			"score" : 1
		},
		"shopping" : {
			"categoryName" : "Shopping",
			"score" : 1
		}
}

And an array of unnamed objects...

"categories" : [
		{
			"categoryName" : "Food",
			"score" : 1
		},
		{
			"categoryName" : "Shopping",
			"score" : 1
		}
]

The query I want to do will need to sum up the scores and return the docs in descending order. I can see how to do this for the first model as the scores can be referenced using the dot notation eg:

"sort" : {
        "_script" : {
            "type" : "number",
            "script" : "doc['categories.food.score'].value + doc['categories.shopping.score'].value",
            "order" : "desc"
        }
    }

But I can't see a similar way to get the same result from the second model using arrays as I would need to say - get the score for the category where the name = "Food".

Can you tell me if its possible? I can't find any examples online.


(system) #2

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