Mapping Builder
Learn how to define an Elasticsearch index mapping using the fluent MappingBuilder syntax in CBElasticsearch
Introduced in v1.0.0
, the MappingBuilder model provides a fluent closure-based sytax for defining and mapping indexes. This builder can be accessed by injecting it into your components:
The new
method of the IndexBuilder
also accepts a closure as the second (properties
) argument. If a closure is passed, a MappingBuilder
instance is passed as an argument to the closure:
The MappingBuilder
has one primary method: create
. create
takes a callback with a MappingBlueprint
object, usually aliased as mapping
.
Mapping Blueprint
The MappingBlueprint
gives a fluent api to defining a mapping. It has methods for all the ElasticSearch mapping types:
As seen above, object
expects a closure which will be provided another MappingBlueprint
. The results will be set as the properties
of the object
call.
Parameters
Parameters can be chained on to a mapping type. Parameters are set using onMissingMethod
and will use the method name (as snake case) as the parameter name and the first argument passed as the parameter value.
You can also add parameters using the
addParameter( string name, any value )
orsetParameters( struct map )
methods.
The only exception to the parameters functions is fields
which expects a closure argument and allows you to create multiple field definitions for a mapping.
Reuse Mapping bits with "Partials"
The Mapping Blueprint also has a way to reuse mappings. Say for instance you have a user
mapping that gets repeated for managers as well.
The partial method accepts three different kinds of arguments:
A closure
A component with a
getPartial
methodA WireBox mapping to a component with a
getPartial
method
The first approach is a simple way to reuse a mapping partials in the same index. The second two approaches work better for partials that are reused across multiple indices.
Last updated