I'm trying to get the counts of my leads object and contacts objects based on the query
e.g below is my index
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Affiliate",
"participant" : "Seychelles",
"name" : "Leroy Smith",
"contacts" : [
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "Mail"
},
{
"promotion" : "21 Days Free Camping",
"contactType" : "Email"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Affiliate",
"participant" : "Marketing Info",
"name" : "Edwin Stone",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "Email"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "VIC",
"participant" : "CAMPING WORLD - SURFSIDE",
"name" : "Dustin Hall",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "Mail"
},
{
"promotion" : "21 Days Free Camping",
"contactType" : "Five9"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Web",
"participant" : "ROCKYFORKRANCHRESORT.COM",
"name" : "Martin Reyes",
"contacts" : [
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "SMS"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Mail",
"participant" : "Vinguage",
"name" : "Jesse Johnson",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "Email"
},
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "SMS"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "VIC",
"participant" : "JACKSONVILLE RV MEGA SHOW",
"name" : "Patricia Schroeder MD",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "SMS"
},
{
"promotion" : "21 Days Free Camping",
"contactType" : "SMS"
},
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "Five9"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "VIC",
"participant" : "CAMPING WORLD - SURFSIDE",
"name" : "Steven Smith",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "Five9"
},
{
"promotion" : "21 Days Free Camping",
"contactType" : "SMS"
},
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "SMS"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Mail",
"participant" : "Vinguage",
"name" : "Aaron Ferguson",
"contacts" : [
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "SMS"
},
{
"promotion" : "21 Days Free Camping",
"contactType" : "Five9"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "Web",
"participant" : "WALLYWORLDRESORT.COM",
"name" : "Faith Flores",
"contacts" : [
{
"promotion" : "3 days 4 nights Free Camping",
"contactType" : "Email"
}
]
}
]
}
},
{
"_index" : "myleads",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.0,
"_source" : {
"leads" : [
{
"program" : "VIC",
"participant" : "CAMPING WORLD - ROANOKE",
"name" : "Dr. Heather Collins",
"contacts" : [
{
"promotion" : "21 Days Free Camping",
"contactType" : "Mail"
}
]
}
]
}
}
]
}
}
I want to query or perform aggregations in a way that my result does not contain some inner objects (contacts in my case) which does not match the query. e.g I want to get the counts of contacts where contactType = Mail.
Right now when I apply query on contactType = Mail. I get inner objects with contactType Mail as well as other contacts with diff contactType because they belong to same parent document.
Please help, I want to get the correct count of my leads and contacts. Similarly If I try to generate buckets based on this contactType=Mail query, I get other stuff too in my contactType bucket.