本文摘录自:How to manage nested objects in Elasticsearch documents
Preparation(准备)
The document of our index will represent a human
and its nested objects will be cats
(no surprises).
Create the index(创建索引)
Open your Kibana dev console and type the following to create the index.
PUT iridakos_nested_objects
{
"mappings": {
"human": {
"properties": {
"name": {
"type": "text"
},
"cats": {
"type": "nested",
"properties": {
"colors": {
"type": "integer"
},
"name": {
"type": "text"
},
"breed": {
"type": "text"
}
}
}
}
}
}
}
Human has:(人有如下属性)
- a
name
property of typetext
- a
cats
property of typenested
Each cat has:(猫有如下属性)
- a
colors
property of typeinteger
- a
name
property of typetext
- a
breed
property of typetext
Add a human(添加一个“人”的记录)
In the Kibana console, execute the following to add a human with three cats.
# Index a human
POST iridakos_nested_objects/human/1
{
"name": "iridakos",
"cats": [
{
"colors": 1,
"name": "Irida",
"breed": "European Shorthair"
},
{
"colors": 2,
"name": "Phoebe",
"breed": "European"
},
{
"colors": 3,
"name": "Nino",
"breed": "Aegean"
}
]
}
Confirm the insertion with:
GET iridakos_nested_objects/human/1
You should see something like this:
{
"_index": "iridakos_nested_objects",
"_type": "human",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "iridakos",
"cats": [
{
"colors": 1,
"name": "Irida",
"breed": "Euro