1.指定字段类型创建数据库
--指定类型创建
PUT myindex14
{
"mappings": {
"properties": {
"id":{"type":"long"},
"name":{"type":"text"},
"city":{"type": "text"},
"course":{"type":"text"},
"teacher":{"type":"text"},
"pxdate":{"type":"date"}
}
}
}
2.按照文档编号插入数据
--插入第一条
PUT myindex14/_doc/1
{
"id": 1,
"name" : "zhangsan",
"city" : "chengdu",
"course" : "语文"
}
--插入第二条
PUT myindex14/_doc/2
{
"id": 2,
"name" : "zhangsan",
"city" : "chengdu",
"course" : "语文",
"teacher":"李四",
"pxdate":"20240628"
}
--插入第三条
PUT myindex14/_doc/3
{
"id": 2,
"pxdate":"20240628"
}
--插入第四条
PUT myindex14/_doc/4
{
"id": 4,
"pxdate":"20240628",
"address":"安居苑"
}
--第五条:插入空。
PUT myindex14/_doc/5
{}
--第6条,插入完全不在我们定义的字段的值
PUT myindex14/_doc/6
{
"id2": 1,
"phdate":"20240628",
"addr":"安居苑"
}
--第7条:插入字段类型不匹配的值
PUT myindex14/_doc/7
{
"id": 2,
"name" : "zhangsan",
"city" : "chengdu",
"course" : "语文",
"teacher":"李四",
"pxdate":"riqi" --这个类型不匹配
}
--输出结果如下。
--原本需要输入date类型的值,但是输入了"riqi"字符串,类型不匹配。
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [pxdate] of type [date] in document with id '7'. Preview of field's value: 'riqi'"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [pxdate] of type [date] in document with id '7'. Preview of field's value: 'riqi'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "failed to parse date field [riqi] with format [strict_date_optional_time||epoch_millis]",
"caused_by" : {
"type" : "date_time_parse_exception",
"reason" : "Failed to parse with all enclosed parsers"
}
}
},
"status" : 400
}
--数据查询
curl -XGET '192.168.1.7:9201/myindex14/_search?pretty' -H 'Content-Type: application/json'
总结:
对于定义了数据结构的数据库,可以插入定义的字段的值。也可以插入,未经定义的值,
也可以插入空值,可以插入字段和定义的字段无关的记录。只要自定义的字段类型匹配,
其他的非自定义的字段都是非常灵活的,默认是text字符串类型。未定义的字段插入后,
默认添加这个字段。
3.添加和删除字段(DDL)
--添加字段email
POST myindex14/_update/1
{
"script" : "ctx._source.email = '1193025604@qq.com'"
}
--映射信息中出现:email字段。
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
--删除字段 :email
POST myindex14/_update/1
{
"script" : "ctx._source.remove('email')"
}
curl -XGET '192.168.1.7:9201/myindex14/_mapping?pretty'
--虽然我们执行了删除字段,但是查询时,字段还在。原因可能是ES不会立即删除
--删除的数据和删除的字段。而是在下次进行合并时才回真实删除。
--查询全部数据
get myindex14/_search?pretty
get /myindex14/_search?pretty
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"name" : "zhangsan",
"city" : "chengdu",
"course" : "语文",
"teacher" : "李四",
"pxdate" : "20240628"
}
},
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"pxdate" : "20240628"
}
},
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"id" : 4,
"pxdate" : "20240628",
"address" : "安居苑"
}
},
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : { }
},
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"id2" : 1,
"phdate" : "20240628",
"addr" : "安居苑"
}
},
{
"_index" : "myindex14",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "zhangsan",
"city" : "chengdu",
"course" : "语文"
}
}
]
}
}
4.总结
POST /index/_doc --可以自动生产文档ID;
PUT /INDEX/_doc/1 --需要手动指定文档ID;
DELETE 删除操作不会立即生效,而是需要下次合并才会生效。
get myindex14/_search?pretty