一、mapping介绍
"""
mapping,类似于mysql的建表
"""
1、查看表结构
GET goods/fruit/_mapping
2、建立表结构
PUT my_index1
{
"mappings": {
# 类型
"doc": {
# 是否为动态
"dynamic": false,
# 属性
"properties": {
"name": {"type": text},
"age": {"type": long}
}
}
}
}
二、dynamic关键字
"""
若dynamic=false,不用指定,可以随时插入动态属性;但是不会通过动态的属性进行查询
若dynamic=true,不用指定,可以随时插入动态属性;会为所有的属性创建索引
若dynamic=strict,必须添加指定的属性;添加动态的属性时,会报错
"""
PUT my_Index1/doc/1
{
"name": "zqx",
"age": 18
}
PUT my_Index1/doc/1
{
"name": "zzz",
"age": 21,
"school": "qinghua"
}
GET my_Index1/doc/1
{
"query": {
"match": {
"school": "qinghua"
}
}
}
三、copy_to关键字
"""
copy_to参数:把当前的值复制给指定的字段;所有的copy_to的值和对copy_to属性赋的值都可以保留
"""
PUT my_index2
{
"mappings": {
"doc": {
"dynamic": false,
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
}
PUT my_index2/doc/1
{
"first_name": "kitty",
"last_name": "tomcat"
}
PUT my_index2/doc/2
{
"first_name": "kitty",
"last_name": "python"
}
PUT my_index2/doc/3
{
"first_name": "kitty",
"last_name": "php",
"full_name": "haha"
}
GET my_index2/doc/3
{
"full_name": "kitty" # 有Kitty的3组/代替了first_name
或"full_name": "python" # 有python的1组/代替了last_name
或"full_name": "haha" # 有haha的1组/haha只有full_name有,无法代替
}
三、index关键字
"""
index属性,默认为true;如果设置为false,则当前属性不能被创建索引
"""
PUT my_index3/doc/4
{
"mappings": {
"doc": {
"dynamic": false
"properties": {
"name": {
"type": "text",
"index": true
},
"add": {
"type": "text",
"index": false
}
}
}
}
}
PUT my_index3/doc/4
{
"name": "kitty",
"add": "beijing"
}
GET my_index3/doc/4
{
"query": {
"match": {
"add": "beijing" //报错
}
}
}
四、对象型属性
"""
对象型属性访问方式为:.进入下一级对象属性
"""
PUT my_Index4/doc/1
{
"name": "tom",
"age": 18,
"add": {
"address": "beijing",
"tel": "18088888888"
}
}
PUT my_Index4/doc/1
{
"name": "zqx",
"age": 21,
"add": {
"address": "beijing",
"tel": "18066666666"
}
}
GET my_Index4/_search
{
"query": {
"match": {
"add.address": "beijing"
}
}
}