Core Types Mapping - gnuhub/elasticsearch GitHub Wiki
Each JSON field can be mapped to a specific core type. JSON itself already provides us with some typing, with its support for string, integer/long, float/double, boolean, and null.
The following sample tweet JSON document will be used to explain the core types:
{
"tweet" {
"user" : "kimchy"
"message" : "This is a tweet!",
"postDate" : "2009-11-15T14:12:12",
"priority" : 4,
"rank" : 12.3
}
}Explicit mapping for the above JSON tweet can be:
{
"tweet" : {
"properties" : {
"user" : {"type" : "string", "index" : "not_analyzed"},
"message" : {"type" : "string", "null_value" : "na"},
"postDate" : {"type" : "date"},
"priority" : {"type" : "integer"},
"rank" : {"type" : "float"}
}
}
}The text based string type is the most basic type, and contains one or more characters. An example mapping can be:
{
"tweet" : {
"properties" : {
"message" : {
"type" : "string",
"store" : "yes",
"index" : "analyzed",
"null_value" : "na"
}
}
}
}The above mapping defines a string message property/field within the tweet type. The field is stored in the index (so it can later be retrieved using selective loading when searching), and it gets analyzed (broken down into searchable terms). If the message has a null value, then then value that will be stored is na.
The following table lists all the attributes that can be used with the string type:
-
index_name: The name of the field that will be stored in the index. Defaults to the property/field name.| -
store|Set toyesthe store actual field in the index,noto not store it. Defaults tono(note, the JSON document itself is stored, and it can be retrieved from it). -
index: Set toanalyzedfor the field to be indexed and searchable after being broken down into token using an analyzer.not_analyzedmeans that its still searchable, but does not go through any analysis process or broken down into tokens.nomeans that it won't be searchable at all. Defaults toanalyzed. -
term_vector: Possible values areno,yes,with_offsets,with_positions,with_positions_offsets. Defaults tono. -
boost: The boost value. Defaults to1.0. -
null_value: When there is a (JSON) null value for the field, use thenull_valueas the field value. Defaults to not adding the field at all. -
omit_norms: Boolean value if norms should be omitted or not. Defaults tofalse. -
omit_term_freq_and_positions: Boolean value if term freq and positions should be omitted. Defaults tofalse. -
analyzer: The analyzer used to analyze the text contents whenanalyzedduring indexing and when searching using a query string. Defaults to the globally configured analyzer. -
index_analyzer: The analyzer used to analyze the text contents whenanalyzedduring indexing. -
search_analyzer: The analyzer used to analyze the field when part of a query string. -
include_in_all: Should the field be included in the_allfield (if enabled). Defaults totrueor to the parentobjecttype setting.
A number based type supporting float, double, byte, short, integer, and long. It uses specific constructs within Lucene is order to support numeric values. An example mapping can be:
{
"tweet" : {
"properties" : {
"rank" : {
"type" : "float",
"null_value" : 1.0
}
}
}
}The following table lists all the attributes that can be used with a numbered type:
-
type: The type of the number. Can befloat,double,byte,short,integer,long. Required. -
index_name: The name of the field that will be stored in the index. Defaults to the property/field name. -
store: Set toyesthe store actual field in the index,noto not store it. Defaults tono(note, the JSON document itself is stored, and it can be retrieved from it). -
index: Set tonoif the value should not be indexed. In this case,storeshould be set toyes, since if its not indexed and not stored, there is nothing to do with it. -
precision_step: The precision step (number of terms generated for each number value). Defaults to4.| -
boost: The boost value. Defaults to1.0. -
null_value: When there is a (JSON) null value for the field, use thenull_valueas the field value. Defaults to not adding the field at all. -
include_in_all: Should the field be included in the_allfield (if enabled). Defaults totrueor to the parentobjecttype setting.
The date type is a special type which maps to JSON string type. It follows a specific format that can be explicitly set. All dates are UTC. Internally, a date maps to a number type long, with the added parsing stage from string to long and from long to string. An example mapping:
{
"tweet" : {
"properties" : {
"postDate" : {
"type" : "date",
"format" : "YYYY-MM-dd"
}
}
}
}The following table lists all the attributes that can be used with a date type:
-
index_name: The name of the field that will be stored in the index. Defaults to the property/field name. -
format: The "date format":../date_format. Defaults todateOptionalTime. -
store: Set toyesthe store actual field in the index,noto not store it. Defaults tono(note, the JSON document itself is stored, and it can be retrieved from it). -
index: Set tonoif the value should not be indexed. In this case,storeshould be set toyes, since if its not indexed and not stored, there is nothing to do with it. -
precision_step: The precision step (number of terms generated for each number value). Defaults to4. -
boost: The boost value. Defaults to1.0. -
null_value: When there is a (JSON) null value for the field, use thenull_valueas the field value. Defaults to not adding the field at all. -
include_in_all: Should the field be included in the_allfield (if enabled). Defaults totrueor to the parentobjecttype setting.
The boolean type Maps to the JSON boolean type. It ends up storing within the index either T or F, with automatic translation to true and false respectively.
{
"tweet" : {
"properties" : {
"hes_my_special_tweet" : {
"type" : "boolean",
}
}
}
}The boolean type also supports passing the value as a number (in this case 0 is false, all other values are true).
The following table lists all the attributes that can be used with the boolean type:
-
index_name: The name of the field that will be stored in the index. Defaults to the property/field name. -
store: Set toyesthe store actual field in the index,noto not store it. Defaults tono(note, the JSON document itself is stored, and it can be retrieved from it). -
index: Set tonoif the value should not be indexed. In this case,storeshould be set toyes, since if its not indexed and not stored, there is nothing to do with it. -
boost: The boost value. Defaults to1.0. -
null_value: When there is a (JSON) null value for the field, use thenull_valueas the field value. Defaults to not adding the field at all. -
include_in_all: Should the field be included in the_allfield (if enabled). Defaults totrueor to the parentobjecttype setting.
The binary type is a base64 representation of binary data that can be stored in the index. The field is always stored and not indexed at all.
{
"tweet" : {
"properties" : {
"image" : {
"type" : "binary",
}
}
}
}The following table lists all the attributes that can be used with the binary type:
-
index_name: The name of the field that will be stored in the index. Defaults to the property/field name.