Object field type
Object field type
JSON documents are hierarchical in nature: the document may contain inner objects which, in turn, may contain inner objects themselves:
resp = client.index(
index="my-index-000001",
id="1",
document={
"region": "US",
"manager": {
"age": 30,
"name": {
"first": "John",
"last": "Smith"
}
}
},
)
print(resp)
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
region: 'US',
manager: {
age: 30,
name: {
first: 'John',
last: 'Smith'
}
}
}
)
puts response
const response = await client.index({
index: "my-index-000001",
id: 1,
document: {
region: "US",
manager: {
age: 30,
name: {
first: "John",
last: "Smith",
},
},
},
});
console.log(response);
PUT my-index-000001/_doc/1
{
"region": "US",
"manager": {
"age": 30,
"name": {
"first": "John",
"last": "Smith"
}
}
}
The outer document is also a JSON object. | |
It contains an inner object called | |
Which in turn contains an inner object called |
Internally, this document is indexed as a simple, flat list of key-value pairs, something like this:
{
"region": "US",
"manager.age": 30,
"manager.name.first": "John",
"manager.name.last": "Smith"
}
An explicit mapping for the above document could look like this:
resp = client.indices.create(
index="my-index-000001",
mappings={
"properties": {
"region": {
"type": "keyword"
},
"manager": {
"properties": {
"age": {
"type": "integer"
},
"name": {
"properties": {
"first": {
"type": "text"
},
"last": {
"type": "text"
}
}
}
}
}
}
},
)
print(resp)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
properties: {
region: {
type: 'keyword'
},
manager: {
properties: {
age: {
type: 'integer'
},
name: {
properties: {
first: {
type: 'text'
},
last: {
type: 'text'
}
}
}
}
}
}
}
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
properties: {
region: {
type: "keyword",
},
manager: {
properties: {
age: {
type: "integer",
},
name: {
properties: {
first: {
type: "text",
},
last: {
type: "text",
},
},
},
},
},
},
},
});
console.log(response);
PUT my-index-000001
{
"mappings": {
"properties": {
"region": {
"type": "keyword"
},
"manager": {
"properties": {
"age": { "type": "integer" },
"name": {
"properties": {
"first": { "type": "text" },
"last": { "type": "text" }
}
}
}
}
}
}
}
Properties in the top-level mappings definition. | |
The | |
The |
You are not required to set the field type
to object
explicitly, as this is the default value.
Parameters for object
fields
The following parameters are accepted by object
fields:
Whether or not new | |
Whether the JSON value given for the object field should be parsed and indexed ( | |
Whether the object can hold subobjects ( | |
The fields within the object, which can be of any data type, including |
If you need to index arrays of objects instead of single objects, read Nested first.