Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/search/aggregations/bucket.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ include::bucket/nested-aggregation.asciidoc[]

include::bucket/reverse-nested-aggregation.asciidoc[]

include::bucket/children-aggregation.asciidoc[]

include::bucket/terms-aggregation.asciidoc[]

include::bucket/significantterms-aggregation.asciidoc[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
[[search-aggregations-bucket-children-aggregation]]
=== Children Aggregation

A special single bucket aggregation that enables aggregating from buckets on parent document types to buckets on child documents.

This aggregation relies on the <<mapping-parent-field,_parent field>> in the mapping. This aggregation has a single option:
* `type` - The what child type the buckets in the parent space should be mapped to.

For example, let's say we have an index of questions and answers. The answer type has the following `_parent` field in the mapping:
[source,js]
--------------------------------------------------
{
"answer" : {
"_parent" : {
"type" : "question"
}
}
}
--------------------------------------------------

The question typed document contain a tag field and the answer typed documents contain an owner field. With the `children`
aggregation the tag buckets can be mapped to the owner buckets in a single request even though the two fields exist in
two different kinds of documents.

An example of a question typed document:
[source,js]
--------------------------------------------------
{
"body": "<p>I have Windows 2003 server and i bought a new Windows 2008 server...",
"title": "Whats the best way to file transfer my site from server to a newer one?",
"tags": [
"windows-server-2003",
"windows-server-2008",
"file-transfer"
],
}
--------------------------------------------------

An example of an answer typed document:
[source,js]
--------------------------------------------------
{
"owner": {
"location": "Norfolk, United Kingdom",
"display_name": "Sam",
"id": 48
},
"body": "<p>Unfortunately your pretty much limited to FTP...",
"creation_date": "2009-05-04T13:45:37.030"
}
--------------------------------------------------

The following request can be built that connects the two together:

[source,js]
--------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a GET /stackoverflow/question to make clear that the query needs to apply to the question type?

{
"aggs": {
"top-tags": {
"terms": {
"field": "tags",
"size": 10
},
"aggs": {
"to-answers": {
"children": {
"type" : "answer" <1>
},
"aggs": {
"top-names": {
"terms": {
"field": "owner.display_name",
"size": 10
}
}
}
}
}
}
}
}
--------------------------------------------------

<1> The `type` points to type / mapping with the name `answer`.

The above example returns the top question tags and per tag the top answer owners.

Possible response:

[source,js]
--------------------------------------------------
{
"aggregations": {
"top-tags": {
"buckets": [
{
"key": "windows-server-2003",
"doc_count": 25365, <1>
"to-answers": {
"doc_count": 36004, <2>
"top-names": {
"buckets": [
{
"key": "Sam",
"doc_count": 274
},
{
"key": "chris",
"doc_count": 19
},
{
"key": "david",
"doc_count": 14
},
...
]
}
}
},
{
"key": "linux",
"doc_count": 18342,
"to-answers": {
"doc_count": 6655,
"top-names": {
"buckets": [
{
"key": "abrams",
"doc_count": 25
},
{
"key": "ignacio",
"doc_count": 25
},
{
"key": "vazquez",
"doc_count": 25
},
...
]
}
}
},
{
"key": "windows",
"doc_count": 18119,
"to-answers": {
"doc_count": 24051,
"top-names": {
"buckets": [
{
"key": "molly7244",
"doc_count": 265
},
{
"key": "david",
"doc_count": 27
},
{
"key": "chris",
"doc_count": 26
},
...
]
}
}
},
{
"key": "osx",
"doc_count": 10971,
"to-answers": {
"doc_count": 5902,
"top-names": {
"buckets": [
{
"key": "diago",
"doc_count": 4
},
{
"key": "albert",
"doc_count": 3
},
{
"key": "asmus",
"doc_count": 3
},
...
]
}
}
},
{
"key": "ubuntu",
"doc_count": 8743,
"to-answers": {
"doc_count": 8784,
"top-names": {
"buckets": [
{
"key": "ignacio",
"doc_count": 9
},
{
"key": "abrams",
"doc_count": 8
},
{
"key": "molly7244",
"doc_count": 8
},
...
]
}
}
},
{
"key": "windows-xp",
"doc_count": 7517,
"to-answers": {
"doc_count": 13610,
"top-names": {
"buckets": [
{
"key": "molly7244",
"doc_count": 232
},
{
"key": "chris",
"doc_count": 9
},
{
"key": "john",
"doc_count": 9
},
...
]
}
}
},
{
"key": "networking",
"doc_count": 6739,
"to-answers": {
"doc_count": 2076,
"top-names": {
"buckets": [
{
"key": "molly7244",
"doc_count": 6
},
{
"key": "alnitak",
"doc_count": 5
},
{
"key": "chris",
"doc_count": 3
},
...
]
}
}
},
{
"key": "mac",
"doc_count": 5590,
"to-answers": {
"doc_count": 999,
"top-names": {
"buckets": [
{
"key": "abrams",
"doc_count": 2
},
{
"key": "ignacio",
"doc_count": 2
},
{
"key": "vazquez",
"doc_count": 2
},
...
]
}
}
},
{
"key": "wireless-networking",
"doc_count": 4409,
"to-answers": {
"doc_count": 6497,
"top-names": {
"buckets": [
{
"key": "molly7244",
"doc_count": 61
},
{
"key": "chris",
"doc_count": 5
},
{
"key": "mike",
"doc_count": 5
},
...
]
}
}
},
{
"key": "windows-8",
"doc_count": 3601,
"to-answers": {
"doc_count": 4263,
"top-names": {
"buckets": [
{
"key": "molly7244",
"doc_count": 3
},
{
"key": "msft",
"doc_count": 2
},
{
"key": "user172132",
"doc_count": 2
},
...
]
}
}
}
]
}
}
}
--------------------------------------------------

<1> The number of question documents with the tag `windows-server-2003`.
<2> The number of answer documents that are related to question documents with the tag `windows-server-2003`.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
* A scorer that wraps a {@link DocIdSetIterator} and emits a constant score.
*/
// Borrowed from ConstantScoreQuery
class ConstantScorer extends Scorer {
public class ConstantScorer extends Scorer {

static ConstantScorer create(DocIdSetIterator iterator, Weight weight, float constantScore) throws IOException {
public static ConstantScorer create(DocIdSetIterator iterator, Weight weight, float constantScore) throws IOException {
return new ConstantScorer(iterator, weight, constantScore);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.search.aggregations;

import org.elasticsearch.search.aggregations.bucket.children.ChildrenBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridBuilder;
Expand Down Expand Up @@ -106,6 +107,10 @@ public static ReverseNestedBuilder reverseNested(String name) {
return new ReverseNestedBuilder(name);
}

public static ChildrenBuilder children(String name) {
return new ChildrenBuilder(name);
}

public static GeoDistanceBuilder geoDistance(String name) {
return new GeoDistanceBuilder(name);
}
Expand Down
Loading