elasticsearch mapping 添加 编辑 删除字段

张映 发表于 2018-05-08

分类目录: 服务器相关

标签:, , , ,

elasticsearch mapping添加字段比较方便。暂不支持编辑字段,删除字段,但是可以采用其他方式来实现编辑字段和删除字段。

1,添加mapping字段

# curl -XPOST "http://127.0.0.1:9200/ik/chinese/_mapping?pretty" -H "Content-Type: application/json" -d '
{
    "chinese": {
            "properties": {
                "sex": {
                    "type": "integer"
                }
            }
        }
  }
'

$ curl -XGET "http://127.0.0.1:9200/ik/_mapping?pretty"
{
  "ik" : {
    "mappings" : {
      "chinese" : {
        "properties" : {
          "description" : {
            "type" : "text"
          },
          "id" : {
            "type" : "integer"
          },
          "sex" : {
            "type" : "integer"   //刚加的字段
          },
          "username" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

2,编辑字段

2.1,创建一个新的索引

//创建索引
$ curl -XPUT 'http://127.0.0.1:9200/ik_v2?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "ik_v2"
}

//创建mapping
# curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/_mapping?pretty" -H "Content-Type: application/json" -d '
{
    "chinese": {
            "_all": {
              "analyzer": "ik_max_word"
            },
            "properties": {
                "id": {
                    "type": "integer"
                },
                "username": {
                    "type": "text"      //类型由keyword变成了text
                },
                "description": {
                    "type": "text"
                }
            }
        }
  }
'

2.2,把老的IK的数据导出,如果不改变字段的顺序,可以把导出的文件,改一下索引名,就可以直接导入到ik_v2。elasticdump 导出 导入 elasticsearch

2.3,创建_aliases

# curl -XPOST "localhost:9200/_aliases?pretty" -H "Content-Type: application/json" -d '
{
    "actions": [
        { "add": {
            "alias": "testik",
            "index": "ik"
        }}
    ]
}
'

2.4可以通过设置的别名,查找数据

# curl -XPOST "http://127.0.0.1:9200/testik/chinese/_search?pretty"  -H "Content-Type: application/json"  -d '
{
    "query": {
        "match_all": {
        }
    }
}
'

2.5,将别名指向到新的ik_v2

# curl -XPOST "http://localhost:9200/_aliases?pretty" -H "Content-Type: application/json" -d '
{
    "actions": [
        { "remove": {
            "alias": "testik",
            "index": "ik"
        }},
        { "add": {
            "alias": "testik",
            "index": "ik_v2"
        }}
    ]
}
'

在这里要注意remove,其实可以不用remove。如果不remove的话,在读取testik会有数据重复,分别来自ik,ik_v2。其实这个testik好比数据库连接的中间件。这样操作,可以实现es服务不宕机的情况,实现平滑过度。

# curl -XPOST "http://127.0.0.1:9200/testik/chinese/_search?pretty"  -H "Content-Type: application/json"  -d '
> {
>     "query": {
>         "match_all": {
>         }
>     }
> }
> '
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ik",
        "_type" : "chinese",
        "_id" : "hlmlPmMB0NIfMbbA0ap6",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "username" : "中国高铁速度很快",
          "description" : "如果要修改一个字段的类型"
        }
      },
      {
        "_index" : "ik",
        "_type" : "chinese",
        "_id" : "h1mlPmMB0NIfMbbA06qP",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "username" : "中国高铁测试",
          "description" : "基基本面震荡"
        }
      },
      {
        "_index" : "ik_v2",
        "_type" : "chinese",
        "_id" : "tFmtPmMB0NIfMbbAQa-9",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "username" : "中国高铁测试",
          "description" : "基基本面震荡"
        }
      },
      {
        "_index" : "ik_v2",
        "_type" : "chinese",
        "_id" : "mFmtPmMB0NIfMbbAOK8w",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "username" : "中国高铁速度很快",
          "description" : "如果要修改一个字段的类型"
        }
      }
    ]
  }
}

3,删除字段

删除字段根编辑字段操作差不多,只不过在导入数据的时候,处理会麻烦点。要把导出的json数据中,删除掉字段对应的数据去掉,才能导入。



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1915.html

留下评论

留下评论
  • (必需)
  • (必需) (will not be published)
  • (必需)   8X2=?