zoukankan      html  css  js  c++  java
  • Elasticsearch中Mapping

    映射(Mapping)

    概念:创建索引时,可以预先定义字段的类型以及相关属性。从而使得索引建立得更加细致和完善。如果不预先设置映射,会自动识别输入的字段类型。

    官方文档(字段数据类型):https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

    官方文档(映射参数):https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html

    映射分为静态映射动态映射

    动态映射:文档中碰到一个以前没见过的字段时,动态映射可以自动决定改字段的类型,并对改字段添加映射。通过dynamic属性进行控制,默认值为true,动态添加;false忽略新字段;strict碰到陌生字段抛出异常。

    建立静态映射:

    curl -XPOST 'http://localhost:9200/library' -d '
    {
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      },
      "mappings": {
        "books": {
          "properties": {
            "title": {
              "type": "string"
            },
            "name": {
              "type": "string",
              "index": "not_analyzed"
            },
            "publish_date": {
              "type": "date",
              "index": "not_analyzed"
            },
            "price": {
              "type": "double"
            },
            "number": {
              "type": "integer"
            }
          }
        }
      }
    }
    '

    动态映射:

    必须为object类型才能添加dynamic动态映射

    {
        "mappings": {
            "books": {
                "dynamic": "strict",
                "properties": {
                    "title": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "publish_date": {
                        "type": "date",
                        "index": "not_analyzed"
                    },
                    "price": {
                        "type": "double"
                    },
                    "number": {
                        "type": "object",
                        "dynamic": true
                    }
                }
            }
        }
    }

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

    curl -XGET localhost:9200/library/_mapping

    更新修改映射

    mapping一旦建立,就不能修改现有的字段映射。如果要推倒现有的映射,必须得重新建立一个索引,然后重新定义映射,将之前索引里的数据导入到新建立得索引中。

    1. 给现有的索引定义一个别名,并且把现有的索引指向这个别名
    2. 运行 curl -XPUT localhost:9200/现有索引/_alias/别名A
    3. 新创建一个索引,定义好最新的映射
    4. 将别名指向新的索引,并且删除之前索引的执行
    5. 运行
    curl -XPOST 'localhost:9200/_aliases' -d '
    {
        "actions": [
            {
                "remove": {
                    "index": "现有索引名",
                    "alias": "别名A"
                }
            },
            {
                "add": {
                    "index": "新建索引名",
                    "alias": "别名A"
                }
            }
        ]
    }
    '

    以上无需停机即可完成。

  • 相关阅读:
    【8.12测试】辉夜的见面礼
    全国标准信息公共服务平台
    win10易升更新
    WPF 中的父子窗口
    C#通过事件跨类调用WPF主窗口中的控件
    Apache Hudi 介绍与应用
    【总结】Spark任务的core,executor,memory资源配置方法
    springboot基于spark-launcher构建rest api远程提交spark任务
    spark 输出到hdfs小文件过多
    大数据
  • 原文地址:https://www.cnblogs.com/zqwby-0708/p/7872973.html
Copyright © 2011-2022 走看看