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" } }
    ]
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页