Skip to content

Commit ccaea8d

Browse files
committed
DATAMONGO-1814 - Update reference documentation for faceted classification.
Original pull request: #426. Original ticket: DATAMONGO-1552.
1 parent 37df6ef commit ccaea8d

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Support for `$caseSensitive` and `$diacriticSensitive` text search.
2828
* Support for GeoJSON Polygon with hole.
2929
* Performance improvements by bulk fetching ``DBRef``s.
30+
* Multi-faceted aggregations using `$facet`, `$bucket` and `$bucketAuto` via `Aggregation`.
3031

3132
[[new-features.1-9-0]]
3233
== What's new in Spring Data MongoDB 1.9

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ At the time of this writing we provide support for the following Aggregation Ope
17911791
[cols="2*"]
17921792
|===
17931793
| Pipeline Aggregation Operators
1794-
| count, geoNear, graphLookup, group, limit, lookup, match, project, replaceRoot, skip, sort, unwind
1794+
| bucket, bucketAuto, count, facet, geoNear, graphLookup, group, limit, lookup, match, project, replaceRoot, skip, sort, unwind
17951795

17961796
| Set Aggregation Operators
17971797
| setEquals, setIntersection, setUnion, setDifference, setIsSubset, anyElementTrue, allElementsTrue
@@ -1870,10 +1870,88 @@ project().and("foo").as("bar"), sort(ASC, "foo")
18701870

18711871
More examples for project operations can be found in the `AggregationTests` class. Note that further details regarding the projection expressions can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/project/#pipe._S_project[corresponding section] of the MongoDB Aggregation Framework reference documentation.
18721872

1873+
[[mongo.aggregation.facet]]
1874+
=== Faceted classification
1875+
1876+
MongoDB supports as of Version 3.4 faceted classification using the Aggregation Framework. A faceted classification uses semantic categories, either general or subject-specific, that are combined to create the full classification entry. Documents flowing through the aggregation pipeline are classificated into buckets. A multi-faceted classification enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.
1877+
1878+
==== Buckets
1879+
1880+
Bucket operations categorize incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. Bucket operations require a grouping field or grouping expression. They can be defined via the `bucket()`/`bucketAuto()` methods of the `Aggregate` class. `BucketOperation` and `BucketAutoOperation` can expose accumulations based on aggregation expressions for input documents. The bucket operation can be extended with additional parameters through a fluent API via the `with…()` methods, the `andOutput(String)` method and aliased via the `as(String)` method. Each bucket is represented as a document in the output.
1881+
1882+
`BucketOperation` takes a defined set of boundaries to group incoming documents into these categories. Boundaries are required to be sorted.
1883+
1884+
.Bucket operation examples
1885+
====
1886+
[source,java]
1887+
----
1888+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100, 400]}}
1889+
bucket("price").withBoundaries(0, 100, 400);
1890+
1891+
// will generate {$bucket: {groupBy: $price, default: "Other" boundaries: [0, 100]}}
1892+
bucket("price").withBoundaries(0, 100).withDefault("Other");
1893+
1894+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100], output: { count: { $sum: 1}}}}
1895+
bucket("price").withBoundaries(0, 100).andOutputCount().as("count");
1896+
1897+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100], 5, output: { titles: { $push: "$title"}}}
1898+
bucket("price").withBoundaries(0, 100).andOutput("title").push().as("titles");
1899+
----
1900+
====
1901+
1902+
`BucketAutoOperation` determines boundaries itself in an attempt to evenly distribute documents into a specified number of buckets. `BucketAutoOperation` optionally takes a granularity specifies the https://en.wikipedia.org/wiki/Preferred_number[preferred number] series to use to ensure that the calculated boundary edges end on preferred round numbers or their powers of 10.
1903+
1904+
.Bucket operation examples
1905+
====
1906+
[source,java]
1907+
----
1908+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5}}
1909+
bucketAuto("price", 5)
1910+
1911+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5, granularity: "E24"}}
1912+
bucketAuto("price", 5).withGranularity(Granularities.E24).withDefault("Other");
1913+
1914+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5, output: { titles: { $push: "$title"}}}
1915+
bucketAuto("price", 5).andOutput("title").push().as("titles");
1916+
----
1917+
====
1918+
1919+
Bucket operations can use `AggregationExpression` via `andOutput()` and <<mongo.aggregation.projection.expressions, SpEL expressions>> via `andOutputExpression()` to create output fields in buckets.
1920+
1921+
Note that further details regarding bucket expressions can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/bucket/[`$bucket` section] and
1922+
http://docs.mongodb.org/manual/reference/operator/aggregation/bucketAuto/[`$bucketAuto` section] of the MongoDB Aggregation Framework reference documentation.
1923+
1924+
==== Multi-faceted aggregation
1925+
1926+
Multiple aggregation pipelines can be used to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. A common implementation of faceting is how many online retailers provide ways to narrow down search results by applying filters on product price, manufacturer, size, etc.
1927+
1928+
A `FacetOperation` can be defined via the `facet()` method of the `Aggregation` class. It can be customized with multiple aggregation pipelines via the `and()` method. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.
1929+
1930+
Sub-pipelines can project and filter input documents prior grouping. Common cases are extraction of date parts or calculations before categorization.
1931+
1932+
.Facet operation examples
1933+
====
1934+
[source,java]
1935+
----
1936+
// will generate {$facet: {categorizedByPrice: [ { $match: { price: {$exists : true}}}, { $bucketAuto: {groupBy: $price, buckets: 5}}]}}
1937+
facet(match(Criteria.where("price").exists(true)), bucketAuto("price", 5)).as("categorizedByPrice"))
1938+
1939+
// will generate {$facet: {categorizedByYear: [
1940+
// { $project: { title: 1, publicationYear: { $year: "publicationDate"}}},
1941+
// { $bucketAuto: {groupBy: $price, buckets: 5, output: { titles: {$push:"$title"}}}
1942+
// ]}}
1943+
facet(project("title").and("publicationDate").extractYear().as("publicationYear"),
1944+
bucketAuto("publicationYear", 5).andOutput("title").push().as("titles"))
1945+
.as("categorizedByYear"))
1946+
----
1947+
====
1948+
1949+
Note that further details regarding facet operation can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/facet/[`$facet` section] of the MongoDB Aggregation Framework reference documentation.
1950+
18731951
[[mongo.aggregation.projection.expressions]]
18741952
==== Spring Expression Support in Projection Expressions
18751953

1876-
As of Version 1.4.0 we support the use of SpEL expression in projection expressions via the `andExpression` method of the `ProjectionOperation` class. This allows you to define the desired expression as a SpEL expression which is translated into a corresponding MongoDB projection expression part on query execution. This makes it much easier to express complex calculations.
1954+
We support the use of SpEL expression in projection expressions via the `andExpression` method of the `ProjectionOperation` and `BucketOperation` classes. This allows you to define the desired expression as a SpEL expression which is translated into a corresponding MongoDB projection expression part on query execution. This makes it much easier to express complex calculations.
18771955

18781956
===== Complex calculations with SpEL expressions
18791957

0 commit comments

Comments
 (0)