准备工作
在本节中,我们将向ElasticSearch添加一些索引、映射和数据。这些数据将在本教程中解释的示例中使用。
我们先启动好ElasticSearch和Kibana,然后访问http://localhost:5601/app/kibana#/dev_tools,进入Kibana的开发工具界面,让我们一步一步做。
创建索引
在Kibana中执行如下命令。
PUT schools
响应内容
命令执行后,如果没有任何问题,会返回如下内容。
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "schools"
}
如果没有提示error,就意味着成功创建了索引 ,如图所示。
创建映射并添加数据
ElasticSearch将根据请求体中提供的数据自动创建映射,我们将使用其批量功能在该索引中添加多个JSON对象。
POST /schools/_bulk
{
"index": {
"_index": "schools",
"_id": "1"
}
}
{
"name": "Central School",
"description": "CBSE Affiliation",
"street": "Nagan",
"city": "paprola",
"state": "HP",
"zip": "176115",
"location": [
31.8955385,
76.8380405
],
"fees": 2000,
"tags": [
"Senior Secondary",
"beautiful campus"
],
"rating": "3.5"
}
{
"index": {
"_index": "schools",
"_id": "2"
}
}
{
"name": "Saint Paul School",
"description": "ICSE Afiliation",
"street": "Dawarka",
"city": "Delhi",
"state": "Delhi",
"zip": "110075",
"location": [
28.5733056,
77.0122136
],
"fees": 5000,
"tags": [
"Good Faculty",
"Great Sports"
],
"rating": "4.5"
}
{
"index": {
"_index": "schools",
"_id": "3"
}
}
{
"name": "Crescent School",
"description": "State Board Affiliation",
"street": "Tonk Road",
"city": "Jaipur",
"state": "RJ",
"zip": "176114",
"location": [
26.8535922,
75.7923988
],
"fees": 2500,
"tags": [
"Well equipped labs"
],
"rating": "4.5"
}
返回响应
{
"took" : 273,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "schools",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "schools",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "schools",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
}
]
}
添加其他索引
POST schools_gov
返回响应
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "schools"
}
(这意味着成功创建了索引)
添加其他数据
POST schools_gov/_bulk
{
"index":{
"_index":"schools_gov", "_id":"1"
}
}
{
"name":"Model School", "description":"CBSE Affiliation", "street":"silk city",
"city":"Hyderabad", "state":"AP", "zip":"500030", "location":[17.3903703, 78.4752129],
"fees":200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
}
{
"index":{
"_index":"schools_gov", "_id":"2"
}
}
{
"name":"Government School", "description":"State Board Affiliation",
"street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
"location": [18.599752, 73.6821995], "fees":500, "tags":["Great Sports"], "rating":"4"
}
返回响应
{
"took" : 293,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "schools_gov",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "schools_gov",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}