# /

# 集群cluster、节点node、分片shards

1、集群由节点组成。
2、集群的垂直划分,集群由一个或多个分片集合组成。
3、集群的水平扩展,即分片集合的水平扩展,分片集合由一个或多个节点组成。

#elasticsearch.yml
# ---------------------------------- Cluster -----------------------------------
# Use a descriptive name for your cluster:
#cluster.name: my-application
# ------------------------------------ Node ------------------------------------
# Use a descriptive name for the node:
#node.name: node-1
# Add custom attributes to the node:
#node.attr.rack: r1
1
2
3
4
5
6
7
8
9
#GET _cluster/health
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 9,
  "active_shards" : 9,
  "relocating_shards" : 0,//迁移中的分片数
  "initializing_shards" : 0,//初始化中的分片数
  "unassigned_shards" : 0,//未分配的分片数
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#GET kibana_sample_data_ecommerce/_search_shards
{
  "nodes" : {
    "Dw_g-_NuTAa4hh_BBwpZ1g" : {
      "name" : "WIN-HBRBLPJVAQU",
      "ephemeral_id" : "ReHwgfNiSCO7RCcPsGFSfg",
      "transport_address" : "127.0.0.1:9300",
      "attributes" : {
        "ml.machine_memory" : "68625584128",
        "xpack.installed" : "true",
        "transform.node" : "true",
        "ml.max_open_jobs" : "20"
      }
    }
  },
  "indices" : {
    "kibana_sample_data_ecommerce" : { }
  },
  "shards" : [
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "Dw_g-_NuTAa4hh_BBwpZ1g",
        "relocating_node" : null,
        "shard" : 0,
        "index" : "kibana_sample_data_ecommerce",
        "allocation_id" : {
          "id" : "boaDrqhkRJeYBVL2sAzKSg"
        }
      }
    ]
  ]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 索引、文档、Mapping、字段

image.png

#show create table kibana_sample_data_ecommerce;
SQL 错误: line 1:6: no viable alternative at input 'show create'
1
2
-- kibana_sample_data_ecommerce definition

-- Drop table

-- DROP TABLE kibana_sample_data_ecommerce;

CREATE TABLE kibana_sample_data_ecommerce (
  category TEXT,
  "category.keyword" KEYWORD(32766),
  currency KEYWORD(32766),
  customer_birth_date DATETIME,
  customer_first_name TEXT,
  "customer_first_name.keyword" KEYWORD(32766),
  customer_full_name TEXT,
  "customer_full_name.keyword" KEYWORD(32766),
  customer_gender KEYWORD(32766),
  customer_id KEYWORD(32766),
  customer_last_name TEXT,
  "customer_last_name.keyword" KEYWORD(32766),
  customer_phone KEYWORD(32766),
  day_of_week KEYWORD(32766),
  day_of_week_i INTEGER,
  email KEYWORD(32766),
  "event.dataset" KEYWORD(32766),
  "geoip.city_name" KEYWORD(32766),
  "geoip.continent_name" KEYWORD(32766),
  "geoip.country_iso_code" KEYWORD(32766),
  "geoip.location" GEO_POINT,
  "geoip.region_name" KEYWORD(32766),
  manufacturer TEXT,
  "manufacturer.keyword" KEYWORD(32766),
  order_date DATETIME,
  order_id KEYWORD(32766),
  "products._id" TEXT,
  "products._id.keyword" KEYWORD(32766),
  "products.base_price" HALF_FLOAT,
  "products.base_unit_price" HALF_FLOAT,
  "products.category" TEXT,
  "products.category.keyword" KEYWORD(32766),
  "products.created_on" DATETIME,
  "products.discount_amount" HALF_FLOAT,
  "products.discount_percentage" HALF_FLOAT,
  "products.manufacturer" TEXT,
  "products.manufacturer.keyword" KEYWORD(32766),
  "products.min_price" HALF_FLOAT,
  "products.price" HALF_FLOAT,
  "products.product_id" LONG,
  "products.product_name" TEXT,
  "products.product_name.keyword" KEYWORD(32766),
  "products.quantity" INTEGER,
  "products.sku" KEYWORD(32766),
  "products.tax_amount" HALF_FLOAT,
  "products.taxful_price" HALF_FLOAT,
  "products.taxless_price" HALF_FLOAT,
  "products.unit_discount_amount" HALF_FLOAT,
  sku KEYWORD(32766),
  taxful_total_price HALF_FLOAT,
  taxless_total_price HALF_FLOAT,
  total_quantity INTEGER,
  total_unique_products INTEGER,
  "type" KEYWORD(32766),
  "user" KEYWORD(32766)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#describe kibana_sample_data_ecommerce;
#show columns from kibana_sample_data_ecommerce;
column                       |type     |mapping   |
-----------------------------+---------+----------+
category                     |VARCHAR  |text      |
category.keyword             |VARCHAR  |keyword   |
currency                     |VARCHAR  |keyword   |
customer_birth_date          |TIMESTAMP|datetime  |
customer_first_name          |VARCHAR  |text      |
customer_first_name.keyword  |VARCHAR  |keyword   |
customer_full_name           |VARCHAR  |text      |
customer_full_name.keyword   |VARCHAR  |keyword   |
customer_gender              |VARCHAR  |keyword   |
customer_id                  |VARCHAR  |keyword   |
customer_last_name           |VARCHAR  |text      |
customer_last_name.keyword   |VARCHAR  |keyword   |
customer_phone               |VARCHAR  |keyword   |
day_of_week                  |VARCHAR  |keyword   |
day_of_week_i                |INTEGER  |integer   |
email                        |VARCHAR  |keyword   |
event                        |STRUCT   |object    |
event.dataset                |VARCHAR  |keyword   |
geoip                        |STRUCT   |object    |
geoip.city_name              |VARCHAR  |keyword   |
geoip.continent_name         |VARCHAR  |keyword   |
geoip.country_iso_code       |VARCHAR  |keyword   |
geoip.location               |GEOMETRY |geo_point |
geoip.region_name            |VARCHAR  |keyword   |
manufacturer                 |VARCHAR  |text      |
manufacturer.keyword         |VARCHAR  |keyword   |
order_date                   |TIMESTAMP|datetime  |
order_id                     |VARCHAR  |keyword   |
products                     |STRUCT   |object    |
products._id                 |VARCHAR  |text      |
products._id.keyword         |VARCHAR  |keyword   |
products.base_price          |FLOAT    |half_float|
products.base_unit_price     |FLOAT    |half_float|
products.category            |VARCHAR  |text      |
products.category.keyword    |VARCHAR  |keyword   |
products.created_on          |TIMESTAMP|datetime  |
products.discount_amount     |FLOAT    |half_float|
products.discount_percentage |FLOAT    |half_float|
products.manufacturer        |VARCHAR  |text      |
products.manufacturer.keyword|VARCHAR  |keyword   |
products.min_price           |FLOAT    |half_float|
products.price               |FLOAT    |half_float|
products.product_id          |BIGINT   |long      |
products.product_name        |VARCHAR  |text      |
products.product_name.keyword|VARCHAR  |keyword   |
products.quantity            |INTEGER  |integer   |
products.sku                 |VARCHAR  |keyword   |
products.tax_amount          |FLOAT    |half_float|
products.taxful_price        |FLOAT    |half_float|
products.taxless_price       |FLOAT    |half_float|
products.unit_discount_amount|FLOAT    |half_float|
sku                          |VARCHAR  |keyword   |
taxful_total_price           |FLOAT    |half_float|
taxless_total_price          |FLOAT    |half_float|
total_quantity               |INTEGER  |integer   |
total_unique_products        |INTEGER  |integer   |
type                         |VARCHAR  |keyword   |
user                         |VARCHAR  |keyword   |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#GET kibana_sample_data_ecommerce/_mapping
{
  "kibana_sample_data_ecommerce" : {
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "currency" : {
          "type" : "keyword"
        },
        "customer_birth_date" : {
          "type" : "date"
        },
        "customer_first_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_full_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_gender" : {
          "type" : "keyword"
        },
        "customer_id" : {
          "type" : "keyword"
        },
        "customer_last_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_phone" : {
          "type" : "keyword"
        },
        "day_of_week" : {
          "type" : "keyword"
        },
        "day_of_week_i" : {
          "type" : "integer"
        },
        "email" : {
          "type" : "keyword"
        },
        "event" : {
          "properties" : {
            "dataset" : {
              "type" : "keyword"
            }
          }
        },
        "geoip" : {
          "properties" : {
            "city_name" : {
              "type" : "keyword"
            },
            "continent_name" : {
              "type" : "keyword"
            },
            "country_iso_code" : {
              "type" : "keyword"
            },
            "location" : {
              "type" : "geo_point"
            },
            "region_name" : {
              "type" : "keyword"
            }
          }
        },
        "manufacturer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "order_date" : {
          "type" : "date"
        },
        "order_id" : {
          "type" : "keyword"
        },
        "products" : {
          "properties" : {
            "_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "base_price" : {
              "type" : "half_float"
            },
            "base_unit_price" : {
              "type" : "half_float"
            },
            "category" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "created_on" : {
              "type" : "date"
            },
            "discount_amount" : {
              "type" : "half_float"
            },
            "discount_percentage" : {
              "type" : "half_float"
            },
            "manufacturer" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "min_price" : {
              "type" : "half_float"
            },
            "price" : {
              "type" : "half_float"
            },
            "product_id" : {
              "type" : "long"
            },
            "product_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              },
              "analyzer" : "english"
            },
            "quantity" : {
              "type" : "integer"
            },
            "sku" : {
              "type" : "keyword"
            },
            "tax_amount" : {
              "type" : "half_float"
            },
            "taxful_price" : {
              "type" : "half_float"
            },
            "taxless_price" : {
              "type" : "half_float"
            },
            "unit_discount_amount" : {
              "type" : "half_float"
            }
          }
        },
        "sku" : {
          "type" : "keyword"
        },
        "taxful_total_price" : {
          "type" : "half_float"
        },
        "taxless_total_price" : {
          "type" : "half_float"
        },
        "total_quantity" : {
          "type" : "integer"
        },
        "total_unique_products" : {
          "type" : "integer"
        },
        "type" : {
          "type" : "keyword"
        },
        "user" : {
          "type" : "keyword"
        }
      }
    }
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#注意:index不仅仅包含mapping
#GET kibana_sample_data_ecommerce
{
  "kibana_sample_data_ecommerce" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "currency" : {
          "type" : "keyword"
        },
        "customer_birth_date" : {
          "type" : "date"
        },
        "customer_first_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_full_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_gender" : {
          "type" : "keyword"
        },
        "customer_id" : {
          "type" : "keyword"
        },
        "customer_last_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer_phone" : {
          "type" : "keyword"
        },
        "day_of_week" : {
          "type" : "keyword"
        },
        "day_of_week_i" : {
          "type" : "integer"
        },
        "email" : {
          "type" : "keyword"
        },
        "event" : {
          "properties" : {
            "dataset" : {
              "type" : "keyword"
            }
          }
        },
        "geoip" : {
          "properties" : {
            "city_name" : {
              "type" : "keyword"
            },
            "continent_name" : {
              "type" : "keyword"
            },
            "country_iso_code" : {
              "type" : "keyword"
            },
            "location" : {
              "type" : "geo_point"
            },
            "region_name" : {
              "type" : "keyword"
            }
          }
        },
        "manufacturer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "order_date" : {
          "type" : "date"
        },
        "order_id" : {
          "type" : "keyword"
        },
        "products" : {
          "properties" : {
            "_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "base_price" : {
              "type" : "half_float"
            },
            "base_unit_price" : {
              "type" : "half_float"
            },
            "category" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "created_on" : {
              "type" : "date"
            },
            "discount_amount" : {
              "type" : "half_float"
            },
            "discount_percentage" : {
              "type" : "half_float"
            },
            "manufacturer" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "min_price" : {
              "type" : "half_float"
            },
            "price" : {
              "type" : "half_float"
            },
            "product_id" : {
              "type" : "long"
            },
            "product_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              },
              "analyzer" : "english"
            },
            "quantity" : {
              "type" : "integer"
            },
            "sku" : {
              "type" : "keyword"
            },
            "tax_amount" : {
              "type" : "half_float"
            },
            "taxful_price" : {
              "type" : "half_float"
            },
            "taxless_price" : {
              "type" : "half_float"
            },
            "unit_discount_amount" : {
              "type" : "half_float"
            }
          }
        },
        "sku" : {
          "type" : "keyword"
        },
        "taxful_total_price" : {
          "type" : "half_float"
        },
        "taxless_total_price" : {
          "type" : "half_float"
        },
        "total_quantity" : {
          "type" : "integer"
        },
        "total_unique_products" : {
          "type" : "integer"
        },
        "type" : {
          "type" : "keyword"
        },
        "user" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "auto_expand_replicas" : "0-1",
        "blocks" : {
          "read_only_allow_delete" : "true"
        },
        "provided_name" : "kibana_sample_data_ecommerce",
        "creation_date" : "1699864076981",
        "number_of_replicas" : "0",
        "uuid" : "gdH9u2JjS0-PswW9cK-Vig",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

# 数据建模

Elasticsearch是文档数据库。(不是关系型数据库)
数据建模是指将现实世界中的概念和关系转化为数据库中的表、列和约束的过程。(对应Elasticsearch的索引、字段)
数据建模可以帮助我们更好地组织和管理数据。
在关系型数据库中,我们使用实体关系模型(ERM)进行数据建模。
通过ERM描述现实世界中的实体和实体之间的关系。实体表示现实世界中的一个对象,属性表示实体的特征,关系表示实体之间的联系,约束表示数据的限制条件。
数据建模的基本过程:
1、概念模型,比如一个订单包含多个商品。
2、逻辑模型,比如ER图。
3、数据模型,比如数据库表中的记录。
4、优化迭代。

# 字段数据类型(Field data types)

Field data types 字段数据类型
每个字段都有一个字段数据类型或字段类型。此类型指示字段包含的数据类型,如字符串或布尔值,以及其预期用途。例如,可以将字符串索引到文本字段和关键字字段。但是,将分析文本字段值以进行全文搜索,而保留关键字字符串以进行筛选和排序。

Common types 常见类型
binary
boolean
Keywords  //关键字
Numbers
Dates
alias

Objects and relational types 对象和关系类型
object
flattened
nested
join

Structured data types 结构化数据类型
Range
ip
version
murmur3

Aggregate data types 聚合数据类型
aggregate_metric_double
histogram

Text search types 文本搜索类型
text           //文本
annotated-text
completion
search_as_you_type
token_count

Document ranking types 文档排名类型
dense_vector
sparse_vector
rank_feature
rank_features

Spatial data types 空间数据类型
geo_point
geo_shape
point
shape

Other types 其他类型
percolator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Keyword field type(不分词)

如果出现以下情况,请考虑将数字标识符映射为关键字:
您不打算使用范围查询来搜索标识符数据。
快速检索很重要。对关键字字段的术语查询搜索通常比对数字字段的术语搜索快。

# Text field type(分词)

用于索引全文值的字段,例如电子邮件正文或产品描述。对这些字段进行分析,也就是说,在索引之前,它们通过分析器将字符串转换为单个术语的列表。分析过程允许Elasticsearch在每个全文字段中搜索单个单词。文本字段不用于排序,也很少用于聚合(尽管重要的文本聚合是一个显著的例外)。
如果您需要对电子邮件地址、主机名、状态代码或标记等结构化内容进行索引,则可能应该使用关键字字段。

# 对象和关系类型(Objects and relational types)

# 嵌套字段类型(Nested field type)
# 使用Object field type存放对象数组,会出现错误的查询结果。
#Elasticsearch没有内部对象的概念。Object field type存放对象数组的结构为:
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}
1
2
3
4
5
6
#存放两个对象
PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

#查询一个不存在的对象
GET my-index-000001/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "user.first": "Alice" }},
        { "match": { "user.last":  "Smith" }}
      ]
    }
  }
}

#却返回一个命中
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first" : "John",
              "last" : "Smith"
            },
            {
              "first" : "Alice",
              "last" : "White"
            }
          ]
        }
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 使用Nested field type存放对象数组
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}

#存放两个对象
PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

#查询一个不存在的对象
GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "Smith" }} 
          ]
        }
      }
    }
  }
}

#返回0个命中
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#查询一个存在的对象
GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "White" }} 
          ]
        }
      },
      "inner_hits": { 
        "highlight": {
          "fields": {
            "user.first": {}
          }
        }
      }
    }
  }
}

#返回存在对象
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first" : "John",
              "last" : "Smith"
            },
            {
              "first" : "Alice",
              "last" : "White"
            }
          ]
        },
        "inner_hits" : {
          "user" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.3862942,
              "hits" : [
                {
                  "_index" : "my-index-000001",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "user",
                    "offset" : 1
                  },
                  "_score" : 1.3862942,
                  "_source" : {
                    "last" : "White",
                    "first" : "Alice"
                  },
                  "highlight" : {
                    "user.first" : [
                      "<em>Alice</em>"
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 联接字段类型(Join field type)
# 通过join type定义父子文档关系
#定义父子文档关联关系
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_id": {
        "type": "keyword"
      },
      "my_join_field": { //字段的名称
        "type": "join",
        "relations": {
          "question": "answer" //定义一个单一关系,其中question是answer的父级。
        }
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#添加两个父文档
PUT my-index-000001/_doc/1?refresh
{
  "my_id": "1",
  "text": "This is a question",
  "my_join_field": {
    "name": "question" 
  }
}

PUT my-index-000001/_doc/2?refresh
{
  "my_id": "2",
  "text": "This is another question",
  "my_join_field": {
    "name": "question"
  }
}

#添加两个子文档
PUT my-index-000001/_doc/3?routing=1&refresh 
{
  "my_id": "3",
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" //此子文档的父id
  }
}

PUT my-index-000001/_doc/4?routing=1&refresh
{
  "my_id": "4",
  "text": "This is another answer",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#查询全index数据
GET my-index-000001/_search
{
  "query": {
    "match_all": {}
  },
  "sort": ["my_id"]
}
1
2
3
4
5
6
7
8
# 通过parent_id查询关联的answer子文档
#通过parent_id查询关联的answer子文档
GET my-index-000001/_search
{
  "query": {
    "parent_id": { 
      "type": "answer",
      "id": "1"
    }
  }
}

#返回关联的子文档
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.35667494,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.35667494,
        "_routing" : "1",
        "_source" : {
          "my_id" : "3",
          "text" : "This is an answer",
          "my_join_field" : {
            "name" : "answer",
            "parent" : "1"
          }
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.35667494,
        "_routing" : "1",
        "_source" : {
          "my_id" : "4",
          "text" : "This is another answer",
          "my_join_field" : {
            "name" : "answer",
            "parent" : "1"
          }
        }
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 通过has_parent查询关联的answer子文档
#通过has_parent查询关联的answer子文档
GET /my-index-000001/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question", //parent
      "query": {
        "match_all": {}
      }
    }
  }
}

#返回关联的子文档
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "my_id" : "3",
          "text" : "This is an answer",
          "my_join_field" : {
            "name" : "answer",
            "parent" : "1"
          }
        }
      },
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "my_id" : "4",
          "text" : "This is another answer",
          "my_join_field" : {
            "name" : "answer",
            "parent" : "1"
          }
        }
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 通过has_child查询关联的父文档
#通过has_child查询关联的父文档
GET /my-index-000001/_search
{
  "query": {
    "has_child": {
      "type": "answer", //child
      "query": {
        "match_all": {}
      }
    }
  }
}

#返回关联的父文档
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "my_id" : "1",
          "text" : "This is a question",
          "my_join_field" : {
            "name" : "question"
          }
        }
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47