索引映射
习题1
创建一个索引,符合如下的要求:
- 索引名称为
movies
- 同时为索引创建一个相应的alias,通过此别名查询的数据,
name
字段都是以movie_
开头的。 - 索引的主分片数量为3,为每个主分片分配一个副本。
- 使用bulk API,对索引
movies
写入多条电影数据。
答案:
# 新增索引直接指定别名
PUT movies
{
"mappings": {
"properties": {
"name":{
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"aliases": {
"movies_a": {
"filter": {
"prefix": {
"name": "movie_"
}
}
}
}
}
POST movies_a/_bulk
{"index": {"_id": 1}}
{"name": "123123"}
{"index": {"_id": 2}}
{"name": "movie_1"}
{"index": {"_id": 3}}
{"name": "movie_2"}
{"index": {"_id": 4}}
{"name": "xxxxx"}
GET movies/_search
GET movies_a/_search
习题2
查询满足条件的省级行政单位信息,并且满足以下条件之一:
- 城市信息中包含北京市
- 行政区信息中包含淇滨区
以下为习题所需数据:
PUT /area/_doc/1
{
"province": {
"name": "北京",
"cities": [
{
"name": "北京市",
"district": [
{"name":"丰台区"},
{"name":"海淀区"},
{"name":"朝阳区"},
{"name":"东城区"},
{"name":"西城区"},
{"name":"昌平区"}
]
}
]
}
}
PUT /area/_doc/2
{
"province": {
"name": "河南省",
"cities": [
{
"name": "郑州市",
"district": [
{
"name": "金水区"
},
{
"name": "高新区"
},
{
"name": "郑东新区"
},
{
"name": "二七区"
},
{
"name": "中原区"
},
{
"name": "惠济区"
}
]
},
{
"name": "鹤壁市",
"district": [
{
"name": "山城区"
},
{
"name": "淇滨区"
},
{
"name": "鹤山区"
},
{
"name": "朝歌"
},
{
"name": "浚县"
}
]
}
]
}
}
PUT /area/_doc/3
{
"province": {
"name": "台湾省",
"cities": [
{
"name": "台北市",
"district": [
{
"name": "中正区"
},
{
"name": "大同区"
},
{
"name": "中山区"
},
{
"name": "万华区"
},
{
"name": "信义区"
},
{
"name": "松山区"
}
]
},
{
"name": "高雄",
"district": [
{
"name": "小港区"
},
{
"name": "鼓山区"
},
{
"name": "三民区"
}
]
}
]
}
}
答案:
1. 先指定mapping nested对象类型,针对查询的name字段将text类型换成keyword不分词
PUT area
{
"mappings": {
"properties": {
"province": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
},
"cities": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
},
"district": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
}
2. 执行查询语句 这里按照两个对象nested类型 进行查询,采用的是 bool should,只要一下两个条件满足任意就直接查询出来
GET area/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "province.cities",
"query": {
"term": {
"province.cities.name": "北京市"
}
}
}
},
{
"nested": {
"path": "province.cities.district",
"query": {
"term": {
"province.cities.district.name": "淇滨区"
}
}
}
}
]
}
}
}
习题3
为索引task3,设置dynamic mapping,具体的要求如下:
一切text类型的字段,类型全部映射为keyword
一切以double_开头命名的字段,类型都设置成double
bulk导入如下的数据进行验证。
导入数据
POST task3/_bulk
{"index":{"_id":1}}
{"cont":"你好马士兵教育", "int_value":35}
{"index":{"_id":2}}
{"cont":"马士兵教育", "int_value":35}
{"index":{"_id":3}}
{"cont":"马士兵", "int_value":35}
答案:
PUT task3
{
"mappings": {
"dynamic_templates": [
{
"text_2_keyword": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
},
{
"int_prefix_2_integer": {
"match": "double_*",
"mapping": {
"type": "double"
}
}
}
]
}
}
#PUT _template/day3_task3_dynamic_template
#{
# "index_patterns": ["task3"],
# "mappings": {
# "dynamic_templates": [
# {
# "text_2_keyword": {
# "match_mapping_type": "string",
# "mapping": {
# "type": "keyword"
# }
# }
# },
# {
# "int_prefix_2_integer": {
# "match": "double_*",
# "mapping": {
# "type": "double"
# }
# }
# }
# ]
# }
#}
验证:
GET task3/_mapping
{
"task3" : {
"mappings" : {
"dynamic_templates" : [
{
"strings_as_keyword" : {
"match_mapping_type" : "string",
"runtime" : {
"type" : "keyword"
}
}
},
{
"strings_as_int" : {
"match" : "double_*",
"runtime" : {
"type" : "double"
}
}
}
],
"runtime" : {
"cont" : {
"type" : "keyword"
},
"int_value" : {
"type" : "double"
}
}
}
}
}
习题4
设置一个index template,符合如下的要求:
为msb和msb-开头的索引,创建3个主分片,1个副本分片。
对索引msb-tech写入测试数据
答案:
PUT _index_template/day3_task4_index_template
{
"index_patterns": ["msb", "msb-*"],
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
}
PUT msb-tech/_doc/1
{
"title": "测试数据1"
}
GET _cat/indices?v
结果 pri=3 rep=1