# Querying embedded arrays vs named objects

**URL:** <https://discuss.elastic.co/t/querying-embedded-arrays-vs-named-objects/121996>\
**Category:** Elasticsearch\
**Created:** [March 1, 2018, 8:33am UTC](https://discuss.elastic.co/t/querying-embedded-arrays-vs-named-objects/121996 "2018-03-01T08:33:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Spence](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Richard\_Spence](https://discuss.elastic.co/u/Richard_Spence)\
**Post date:** [March 1, 2018, 8:33am UTC](https://discuss.elastic.co/t/querying-embedded-arrays-vs-named-objects/121996/1 "2018-03-01T08:33:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 29, 2018, 8:34am UTC](https://discuss.elastic.co/t/querying-embedded-arrays-vs-named-objects/121996/2 "2018-03-29T08:34:13Z")

</div>

Closed on March 29, 2018, 8:34am UTC

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