Nested facet counts for matching parent records

Assume I have 2 records:
{ session:"a", activities:[ {action : "jump"}, {action : "run"} ] }
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :
"jog"}, {action : "jump"} ] }

If I do a nested facet, I will get counts based on the # of actions that
matched:
jump : 3
run : 1
jog : 2

How can I get counts for the number of parent records that matched instead?
ie:
jump : 2
run : 1
jog : 1

--

You can write your query code here ...

在 2012年10月24日星期三UTC+8上午5时28分54秒,Mike写道:

Assume I have 2 records:
{ session:"a", activities:[ {action : "jump"}, {action : "run"} ] }
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :
"jog"}, {action : "jump"} ] }

If I do a nested facet, I will get counts based on the # of actions that
matched:
jump : 3
run : 1
jog : 2

How can I get counts for the number of parent records that matched
instead? ie:
jump : 2
run : 1
jog : 1

--

Here are the mappings, records, and query:

{
"mappings" : {
"article" : {
"properties" : {
"activities" : {
"type" : "nested",
"dynamic" : "true",
"properties" : {
"action" : { "type" : "string" }
}
},
"session" : { "type" : "string" }
}
}
}
}

{ session:"a", activities:[ {action : "jump"}, {action : "run"} ] }
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :
"jog"}, {action : "jump"} ] }

{
"query" : { "match_all" : {} },
"facets": {
"test" : {
"nested" : "activities",
"terms" : { "field" : "action" }
}
}
}

On Tuesday, October 23, 2012 5:28:54 PM UTC-4, Mike wrote:

Assume I have 2 records:
{ session:"a", activities:[ {action : "jump"}, {action : "run"} ] }
{ session:"b", activities:[ {action : "jump"}, {action : "jog"}, {action :
"jog"}, {action : "jump"} ] }

If I do a nested facet, I will get counts based on the # of actions that
matched:
jump : 3
run : 1
jog : 2

How can I get counts for the number of parent records that matched
instead? ie:
jump : 2
run : 1
jog : 1

--

Does anyone know if there is a way to get back the number of parent records
that matched instead of the number of child records?

--

If you want to get the number of parent records, you need to run your facet
against parent records. You can do it by including fields from nested
objects into parent objects:

{
"mappings" : {
"article" : {
"properties" : {
"activities" : {
"type" : "nested",
"dynamic" : true,

  •        "include_in_parent": true,*
          "properties" : {
            "action" : { "type" : "string" }
          }
        },
        "session" : { "type" : "string" }
      }
    }
    
    }
    }

{
"query" : { "match_all" : {} },
"facets": {
"test" : {
"terms" : {
"field" : "activities.action"
}
}
}
}

On Friday, October 26, 2012 12:07:01 PM UTC-4, Mike wrote:

Does anyone know if there is a way to get back the number of parent
records that matched instead of the number of child records?

--