Searching in arrays of subtypes


(MMM) #1

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

--


(David Pilato) #2

Have a look at nested docs.

HTH

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

Le 3 sept. 2012 à 15:06, MMM 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 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

--


(MMM) #3

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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 3 sept. 2012 à 15:06, MMM <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 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

--

--


(system) #4