# Searching in arrays of subtypes

**URL:** <https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917>\
**Category:** Elasticsearch\
**Created:** [September 3, 2012, 1:06pm UTC](https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917 "2012-09-03T13:06:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMM](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@MMM](https://discuss.elastic.co/u/MMM)\
**Post date:** [September 3, 2012, 1:06pm UTC](https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917/1 "2012-09-03T13:06:01Z")

</div>

Hello,

sorry if this question was already answered, but I havent found any  
answer on my problem.

_Example:_  
I have a database of school with classes and kids. Each class have name,  
year ... and an array of subobjects - kids.

...  
{

name: "A",

year: "2",

kids: [

{ firstName: "Peter", lastName: "Jackson"}

]

},  
{

name: "B",  
year: "1",  
kids: [

{ firstName: "Peter", lastName: "Pan"},

{ firstName: "Michael", lastName: "Jackson"}

]

}  
...

The data are nice stored (indexed) in elastic search, everything seems to  
be OK.  
So now here is the Question...  
\*  
\*  
_Find classes where "Peter Jackson" ist._

So my query:

{  
"query": {  
"bool": {  
"must": [  
{  
"field": {  
"kids.firstName": "Peter"  
}  
},  
{  
"field": {  
"kids.lastName": "Jackson"  
}  
}  
]  
}  
}  
}

The response ist Class A and Class B. (Because kids.firstName in B has  
Peter and kids.lastName in B has Jackson)

How can I do it to get just Class A???

1. Scripting?  
-for example: for (kid in kids) { kid.firstName = "Peter" AND  
kid.lastName = "Jackson"}
2. Parent/Child types?

- means extract kids in other type

1. ???

Please provide some testing/example code.

thanks!  
Matus

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 3, 2012, 3:52pm UTC](https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917/2 "2012-09-03T15:52:14Z")

</div>

Have a look at nested docs.

## HTH

David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 3 sept. 2012 à 15:06, MMM [homer68@gmail.com](mailto:homer68@gmail.com) a écrit :

> Hello,
> 
> sorry if this question was already answered, but I havent found any answer on my problem.
> 
> Example:  
> I have a database of school with classes and kids. Each class have name, year ... and an array of subobjects - kids.
> 
> ...  
> {  
> name: "A",  
> year: "2",  
> kids: [  
> { firstName: "Peter", lastName: "Jackson"}  
> ]  
> },  
> {  
> name: "B",  
> year: "1",  
> kids: [  
> { firstName: "Peter", lastName: "Pan"},  
> { firstName: "Michael", lastName: "Jackson"}  
> ]  
> }  
> ...
> 
> The data are nice stored (indexed) in Elasticsearch, everything seems to be OK.  
> So now here is the Question...
> 
> Find classes where "Peter Jackson" ist.
> 
> So my query:
> 
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "field": {  
> "kids.firstName": "Peter"  
> }  
> },  
> {  
> "field": {  
> "kids.lastName": "Jackson"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> The response ist Class A and Class B. (Because kids.firstName in B has Peter and kids.lastName in B has Jackson)
> 
> How can I do it to get just Class A???
> 
> 1. Scripting?  
> -for example: for (kid in kids) { kid.firstName = "Peter" AND kid.lastName = "Jackson"}
> 2. Parent/Child types?
> 
> - means extract kids in other type
> 
> 1. ???
> 
> Please provide some testing/example code.
> 
> ## thanks! Matus

--

---

<div class="post-metadata">

**Author:** ![MMM](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@MMM](https://discuss.elastic.co/u/MMM)\
**Post date:** [September 5, 2012, 11:55am UTC](https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917/3 "2012-09-05T11:55:40Z")

</div>

Thanks!!!

That's working. I have try the parent/child way too.

Am Montag, 3. September 2012 17:52:43 UTC+2 schrieb David Pilato:

> Have a look at nested docs.
> 
> ## HTH
> 
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 3 sept. 2012 à 15:06, MMM \<[hom...@gmail.com](mailto:hom...@gmail.com) \<javascript:\>\> a écrit :
> 
> Hello,
> 
> sorry if this question was already answered, but I havent found any  
> answer on my problem.
> 
> _Example:_  
> I have a database of school with classes and kids. Each class have name,  
> year ... and an array of subobjects - kids.
> 
> ...  
> {
> 
> name: "A",
> 
> year: "2",
> 
> kids: [
> 
> { firstName: "Peter", lastName: "Jackson"}
> 
> ]
> 
> },  
> {
> 
> name: "B",  
> year: "1",  
> kids: [
> 
> { firstName: "Peter", lastName: "Pan"},
> 
> { firstName: "Michael", lastName: "Jackson"}
> 
> ]
> 
> }  
> ...
> 
> The data are nice stored (indexed) in Elasticsearch, everything seems to  
> be OK.  
> So now here is the Question...  
> \*  
> \*  
> _Find classes where "Peter Jackson" ist._
> 
> So my query:
> 
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "field": {  
> "kids.firstName": "Peter"  
> }  
> },  
> {  
> "field": {  
> "kids.lastName": "Jackson"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> The response ist Class A and Class B. (Because kids.firstName in B has  
> Peter and kids.lastName in B has Jackson)
> 
> How can I do it to get just Class A???
> 
> 1. Scripting?  
> -for example: for (kid in kids) { kid.firstName = "Peter" AND  
> kid.lastName = "Jackson"}
> 2. Parent/Child types?
> 
> - means extract kids in other type
> 
> 1. ???
> 
> Please provide some testing/example code.
> 
> thanks!  
> Matus
> 
> --

--

---

<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:** [July 6, 2017, 3:14am UTC](https://discuss.elastic.co/t/searching-in-arrays-of-subtypes/8917/4 "2017-07-06T03:14:03Z")

</div>


