2019-11-26

Elasticsearch - Types

Types are logical containers for documents, similar to how tables are containers for
rows. You’d put documents with different structures (schemas) in different types. For
example, you could have a type that defines get-together groups and another type for
the events when people gather.
 The definition of fields in each type is called a mapping. For example, name would
be mapped as a string, but the geolocation field under location would be mapped
as a special geo_point type. (We explore working with geospatial data in appendix A.)
Each kind of field is handled differently. For example, you search for a word in the
name field and you search for groups by location to find those that are located near
where you live.
TIP Whenever you search in a field that isn’t at the root of your JSON docu-
ment, you must specify its path. For example, the geolocation field under
location is referred to as location.geolocation.
You may ask yourself: if Elasticsearch is schema-free, why does each document belong
to a type, and each type contains a mapping, which is like a schema?
 We say schema-free because documents are not bound to the schema. They aren’t
required to contain all the fields defined in your mapping and may come up with
new fields. How does it work? First, the mapping contains all the fields of all the
documents indexed so far in that type. But not all documents have to have all fields.
Also, if a new document gets indexed with a field that’s not already in the mapping,
Elasticsearch automatically adds that new field to your mapping. To add that field, it
has to decide what type it is, so it guesses it. For example, if the value is 7, it assumes
it’s a long type.
 This autodetection of new fields has its downside because Elasticsearch might not
guess right. For example, after indexing 7, you might want to index hello world,
which will fail because it’s a string and not a long. In production, the safe way to go is
to define your mapping before indexing data.
 Mapping types only divide documents logically. Physically, documents from the
same index are written to disk regardless of the mapping type they belong to.

No comments:

Post a Comment