美文网首页
ElasticEearch-mapping

ElasticEearch-mapping

作者: 壹点零 | 来源:发表于2017-11-17 17:23 被阅读0次

    删除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
    

    相关文章

      网友评论

          本文标题:ElasticEearch-mapping

          本文链接:https://www.haomeiwen.com/subject/idwjvxtx.html