ElasticEearch-mapping

删除school索引

DELETE /school

静态映射

format日期格式默认:strict_date_optional_time||epoch_millis

PUT /school
{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "student": {
            "properties": {
                "age": { "type": "long"},
                "course": { "type": "text"},
                "name": {"type": "keyword"},
                "study_date": {"type": "date", "format": "yyyy-MM-dd"}
            }
        }
    }
}

日期格式不对无法写入

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15 23:00:00"
}

可以写入

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15"
}

动态映射

DELETE /school
PUT /school
{
    "mappings": {
        "student": {
            "dynamic":  "strict",
            "properties": {
                "age": { "type": "long"},
                "course": { "type": "text"},
                "name": {"type": "keyword"},
                "study_date": {"type": "date", "format": "yyyy-MM-dd"},
                "other": {
                  "type":   "object",
                  "properties": {
                     "field01":{"type":"text"}
                  },
                  "dynamic":  true
                }
            }
        }
    }
}

不能动态增加字段,无法写入

POST /school/student
{
  "name":"zhangsan",
  "sex":"male"
}

POST /school/student/1
{
  "name":"zhangsan",
  "other":{
    "field01":"value1",
    "field02":"value2"
  }
}

GET /school/_mapping

mapping是不允许修改,但是可以新增字段类型

在已建立的索引下,添加字段mapping

PUT /school/_mapping/student
{
  "properties": {
    "a_new_filed": {
      "type": "keyword"
    }
  }
}

在已建立的索引下,新增type的mapping

注意,不同type下,相同的字段名的类型要保持一致

PUT /school/_mapping/new_type
{
  "properties": {
    "a_new_filed": {
      "type": "keyword"
    }
  }
}

在已建立的索引下,添加一个object类型字段的mapping

PUT /school/_mapping/student
{
  "properties": {
    "name_all": {
        "properties":{
            "first":{"type":"keyword"},
            "last":{"type":"keyword"}
        }
    }
  }
}

在已建立的索引下,在object类型字段下添加子字段的mapping

PUT /school/_mapping/student
{
  "properties": {
    "name_all": {
        "properties":{
            "all":{"type":"keyword"}
        }
    }
  }
}

修改当前索引的字段mapping,增加ignore_above属性(注意:只有keyword类型有ignore_above)

PUT /school/_mapping/student
{
  "properties": {
    "name": {
        "type":"keyword",
        "ignore_above":100
    }
  }
}

获取某个索引的映射信息

GET /school/_mapping

获取某个索引下某个type的映射信息

GET /school/_mapping/student

获取某个索引下指定type的某个字段的mapping

GET /school/_mapping/student/field/name

获取某个索引下指定type,多个字段的mapping

GET /school/_mapping/student/field/name,course

获取某个索引下所有type,多个字段的mapping

GET /school/_mapping/field/name,course

获取多个索引内的字段的mapping

GET /school,school2/_mapping/field/name,course

获取多个索引下的mapping(通配符)

GET /sc*/_mapping/

获取多个索引下多个type的mapping(通配符)

GET /sc*/_mapping/stud*

获取多个索引下多个type的,多个字段的mapping(通配符)

GET /sc*/_mapping/stud*/field/na*

获取集群内所有的映射信息

GET /_all/_mapping

获取集群内多个type的映射信息

GET /_all/_mapping/student,student2

获取集群内多个type的字段映射信息

GET /_all/_mapping/student,student2/field/name

创建别名

PUT /school/_alias/school_info

查看别名

GET /_alias

查看某个索引下的别名

GET /school/_alias/

添加移除别名

PUT /school_new
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "school",
        "alias": "school_info"
      }
    },
    {
      "add": {
        "index": "school_new",
        "alias": "school_info"
      }
    }
  ]
}

-----------------nested类型-----------------------

DELETE school
PUT school/class/1
{
  "name": "xxx-class",
  "users": [
    {
      "age": 25,
      "name": "zhangsan"
    },
    {
      "age": 26,
      "name": "lisi"
    }
  ]
}

存储在ES中,是这个样子的

{
    "users.age":[25,26],
    "users.name":["zhangsan","lisi"]
}
GET school/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "users.age": 25
          }
        },
        {
          "match": {
            "user.name": "zhangsan"
          }
        }
      ]
    }
  }
}

使用nested类型

DELETE school
PUT school
{
  "mappings": {
    "class": {
      "properties": {
        "users": {
          "type":"nested"
        }
      }
    }
  }
}

PUT school/class/1
{
  "name": "xxx-class",
  "users": [
    {
      "age": 25,
      "name": "zhangsan"
    },
    {
      "age": 26,
      "name": "lisi"
    }
  ]
}

GET school/_search
{
  "query": {
    "nested": {
      "path": "users",
      "query":{
        "bool": {
          "must": [
            { "match": { "users.age": 25 }},
            { "match": { "users.name":"zhangsan" }}
          ]
        }
      }
    }
  }
}

-----------------Geo-point类型-----------------------

DELETE my_index
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

指定经纬度

PUT my_index/my_type/1
{
  "text": "Geo-point as an object",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

格式lat纬度,lon经度

PUT my_index/my_type/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" 
}

PUT my_index/my_type/3
{
  "text": "Geo-point as a geohash",
  "location": "drm3btev3e86" 
}

格式[lon经度,lat纬度]

PUT my_index/my_type/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] 
}

GET my_index/_search
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -71.4
        },
        "bottom_right": {
          "lat": 40,
          "lon": -71.3
        }
      }
    }
  }
}

距离搜索

GET /my_index/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

特殊区域

GET /my_index/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "location" : {
                        "points" : [
                        {"lat" : 40, "lon" : -80},
                        {"lat" : 50, "lon" : -75},
                        {"lat" : 40, "lon" : -70}
                        ]
                    }
                }
            }
        }
    }
}

-----------------默认mapping属性-----------------------

DELETE school

PUT school
{
  "mappings": {
    "_default_": { 
      "_source": {
        "enabled": true
      }
    },
    "student": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "text",
          "store": false,
          "index": true
        },
        "course": {
          "type": "text"
        },
        "study_date":{
          "type":"date",
          "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
GET school/_mapping

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15 23:00:00"
}

GET /school/student/1

默认动态mapping映射

DELETE /school
PUT /school
{
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "message_field": {
            "mapping": {
              "store": false,
              "type": "text"
            },
            "match": "*msg",
            "match_mapping_type": "string"
          }
        },
        {
          "string_fields": {
            "mapping": {
              "ignore_above": 256,
              "store": false,
              "type": "keyword"
            },
            "match": "*",
            "match_mapping_type": "string"
          }
        }
      ],
      "properties":{}
    }
  }
}

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15 23:00:00"
}
GET /school/_mapping

-----------------模板----------------

DELETE /school

PUT _template/student_template
{
  "template": "sc*",
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas":2
  },
  "mappings": {
    "student": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "text",
          "store": false,
          "index": true
        },
        "course": {
          "type": "text"
        },
        "study_date":{
          "type":"date",
          "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}
GET /school/_mapping,_settings

删除模板

DELETE /_template/student_template

获取模板

GET /_template/student_template

模板的优先级,order数字越大,优先级越高

DELETE /school
PUT /_template/template_1
{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "student" : {
            "_source" : { "enabled" : false }
        }
    }
}

PUT /_template/template_2
{
    "template" : "sc*",
    "order" : 1,
    "settings" : {
        "number_of_shards" : 2
    },
    "mappings" : {
        "student" : {
            "_source" : { "enabled" : true }
        }
    }
}
PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

GET /school/_mapping,_settings

动态模板

PUT /_template/template_3
{
  "template": "my_*",
  "order": 1,
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "message_field": {
            "mapping": {
              "store": false,
              "type": "text"
            },
            "match": "*msg",
            "match_mapping_type": "string"
          }
        },
        {
          "string_fields": {
            "mapping": {
              "ignore_above": 256,
              "store": false,
              "type": "keyword"
            },
            "match": "*",
            "match_mapping_type": "string"
          }
        }
      ],
      "properties": {}
    }
  }
}

DELETE /my_index
PUT /my_index/doc/1
{
  "name":"zhangsan",
  "msg":"this is a message!"
}

GET /my_index/_mapping

-----------------分词器------------------

内置标准分词器

POST _analyze
{
  "analyzer": "standard",
  "text": "I'am a Teacher 666."
}

内置简单分词器

POST _analyze
{
  "analyzer": "simple",
  "text": "I'am a Teacher 666."
}

内置停止词分词器

POST _analyze
{
  "analyzer": "stop",
  "text": "I'am a Teacher 666."
}

测试自定义组合分词器

DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type":      "custom",
          "tokenizer": "standard",
          "char_filter": ["html_strip"],
          "filter": ["lowercase","stop"]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "I'am a <b>Teacher</b> 666."
}

设置mapping时为字段指定分词器

DELETE my_index

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "std_english": {  
           "type":      "standard",
           "stopwords": "_english_"
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "my_text": {
           "type":     "text",
           "analyzer": "standard",  
           "fields": {
             "stop": {
               "type":     "text",
               "analyzer": "std_english"  
            }
          }
        }
      }
    }
  }
}

PUT my_index/my_type/1
{
    "my_text":"today is the good"
}

标准分析器查询

GET my_index/my_type/_search
{
 "query": {
    "match": {
      "my_text": "the"
    }
  }
}

测试用停止词分析器查询(查询不出来数据)

GET my_index/my_type/_search
{
 "query": {
    "match": {
      "my_text.stop": "the"
    }
  }
}

测试用了停止词分析器查询(可以查询出来数据)

GET my_index/my_type/_search
{
 "query": {
    "match": {
      "my_text.stop": "good"
    }
  }
}

查看自定义的分词器

POST my_index/_analyze
{
  "analyzer": "std_english",
  "text": "today is the good"
}

动态更新分词器

POST /school/_close
PUT /school/_settings
{
  "analysis" : {
    "analyzer":{
      "content":{
        "type":"custom",
        "tokenizer":"whitespace"
      }
    }
  }
}
POST /school/_open
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,100评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,308评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,718评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,275评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,376评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,454评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,464评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,248评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,686评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,974评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,150评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,817评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,484评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,140评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,374评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,012评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,041评论 2 351

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,644评论 18 139
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,485评论 0 4
  • 获取索引的mapping 实例: 测试分析器 创建一个索引 到目前为止, 我们已经通过索引一篇文档创建了一个新的索...
    M醉逍遥阅读 1,055评论 0 1
  • 我们遥相观望 天空很灰 你把我看得无限迷惘 我分不出其中因果 烈日不来 街道被浓密的梧桐覆盖 为何我爱你的雪白 那...
    王尔德wonder阅读 197评论 0 0
  • 我决定以后经常写写,让荒废已久的笔动起来,让心的缝隙里透点阳光进来,晒晒布满灰尘的角落。 我不入流,这...
    浪漫的凤凰阅读 215评论 0 0