ES(Elasticsearch)更改字段索引

原有商品搜索,要对原有字段索引进行更改(目前无法直接在原索引上进行更改),那么就要新建新的索引,然后复制数据

环境说明

es 索引goods-2020-01-01 绑定别名goods

如何绑定别名

创建索引

kibana 里执行

PUT goods-2020-01-01
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik_syno_max": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [
            "local_synonym_filter",
            "jt_tfr"
          ],
          "char_filter": [
            "jt_cfr"
          ]
        },
        "ik_syno_smart": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "local_synonym_filter"
          ]
        },
        "english": {
          "tokenizer": "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        }
      },
      "filter": {
        "jt_tfr": {
          "type": "stop",
          "stopwords": [
            " "
          ]
        },
        "local_synonym_filter": {
          "type": "synonym",
          "synonyms_path": "analysis/synonyms.txt"
        },
        "english_stop": {
          "type": "stop",
          "stopwords": "_english_"
        },
        "english_keywords": {
          "type": "keyword_marker",
          "keywords": [
            "example"
          ]
        },
        "english_stemmer": {
          "type": "stemmer",
          "language": "english"
        },
        "english_possessive_stemmer": {
          "type": "stemmer",
          "language": "possessive_english"
        }
      },
      "char_filter": {
        "jt_cfr": {
          "type": "mapping",
          "mappings": [
            "| => \\|"
          ]
        }
      },
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "dynamic": true,
    "properties": {
      "sku": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      },
      "name": {
        "type": "text"
      },
      "gmtCreate":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      },
      "gmtModified":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      },
      "makeTime":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      }
    }
  }
}
关联别名 (创建别名)

kibana 里执行

POST /_aliases
{
  "actions": [
    {
      "add": {
        "alias": "goods",
        "index": "goods-2020-01-01"
      }
    }
  ]
}
删除别名

kibana 里执行

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "goods-2020-01-01", "alias" : "goods" } }
    ]
}
查看别名下所有索引

kibana 里执行

GET goods/_alias
查看索引是否有别名

kibana 里执行

GET goods-2020-01-01/_alias
查看词库分词情况

kibana 里执行

POST goods-2020-01-01/_analyze
{
  "analyzer": "ik_syno_smart",
  "text": "牛栏"
}

查看原索引

kibana 里执行

GET goods-2020-01-01/_mapping
结果
{
  "goods-2020-01-01" : {
    "mappings" : {
      "properties" : {
        "sku" : {
          "type": "keyword",
          "normalizer": "my_normalizer"
        },
        "title" : {
          "type" : "text"
        },
        "gmtCreate":{
	        "type":"date",
	        "format":"yyyy-MM-dd HH:mm:ss",
	        "ignore_malformed":true
	      },
	      "gmtModified":{
	        "type":"date",
	        "format":"yyyy-MM-dd HH:mm:ss",
	        "ignore_malformed":true
	      },
	      "makeTime":{
	        "type":"date",
	        "format":"yyyy-MM-dd HH:mm:ss",
	        "ignore_malformed":true
	      }
      }
    }
  }
}

更改步骤

1.创建新索引

kibana 里执行

PUT goods-2020-12-21
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik_syno_max": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [
            "local_synonym_filter",
            "jt_tfr"
          ],
          "char_filter": [
            "jt_cfr"
          ]
        },
        "ik_syno_smart": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "local_synonym_filter"
          ]
        },
        "english": {
          "tokenizer": "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        }
      },
      "filter": {
        "jt_tfr": {
          "type": "stop",
          "stopwords": [
            " "
          ]
        },
        "local_synonym_filter": {
          "type": "synonym",
          "synonyms_path": "analysis/synonyms.txt"
        },
        "english_stop": {
          "type": "stop",
          "stopwords": "_english_"
        },
        "english_keywords": {
          "type": "keyword_marker",
          "keywords": [
            "example"
          ]
        },
        "english_stemmer": {
          "type": "stemmer",
          "language": "english"
        },
        "english_possessive_stemmer": {
          "type": "stemmer",
          "language": "possessive_english"
        }
      },
      "char_filter": {
        "jt_cfr": {
          "type": "mapping",
          "mappings": [
            "| => \\|"
          ]
        }
      },
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "dynamic": true,
    "properties": {
      "sku": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_syno_max",
        "search_analyzer": "ik_syno_smart",
        "fields": {
          "cn": {
            "type": "text",
            "analyzer": "ik_syno_smart"
          },
          "en": {
            "type": "text",
            "analyzer": "english"
          }
        }
      },
      "gmtCreate":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      },
      "gmtModified":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      },
      "makeTime":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss",
        "ignore_malformed":true
      }
    }
  }
}

2.复制数据

kibana 里执行

POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "goods-2020-01-01"
  },
  "dest": {
    "index": "goods-2020-12-21"
  }
}

3. 查看复制进度

kibana 里执行

GET _tasks?detailed=true&actions=goods-2020-12-21

如果数据量很少,那么结果是如下,表示 复制完成

{
  "nodes" : { }
}

4. 把别名与新索引关联

先删除原别名与就索引的关系,然后建立新索引和别名的关系

kibana 里执行

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "goods-2020-01-01", "alias" : "goods" } },
        { "add" : { "index" : "goods-2020-12-21", "alias" : "goods" } }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风.foxwho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值