美文网首页
ElasticSearch(ES)索引更新

ElasticSearch(ES)索引更新

作者: 南岩飞雪 | 来源:发表于2019-04-16 21:20 被阅读0次

    方法一,直接更新

    1. Close index


      image.png
    2. 更新_settings,索引(eg. indexusers)增加pinyin_analyzer
    PUT /indexusers/_settings
    {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "pinyin_analyzer" : {
                        "tokenizer" : "my_pinyin"
                        }
                },
                "tokenizer" : {
                    "my_pinyin" : {
                        "type" : "pinyin",
                        "keep_separate_first_letter" : false,
                        "keep_full_pinyin" : true,
                        "keep_original" : true,
                        "limit_first_letter_length" : 16,
                        "lowercase" : true,
                        "remove_duplicated_term" : true
                    }
                }
            }
        }
    }
    
    1. Open index
    2. 更新mapping,给需要的字段(eg. name)指定pinyin_analyzer
    PUT indexusers/_mapping/user
    {
            "properties": {
                "name": {
                    "type": "text",
                    "fields": {
                        "pinyin": {
                            "type": "text",
                            "store": false,
                            "term_vector": "with_offsets",
                            "analyzer": "pinyin_analyzer",
                            "boost": 10
                        }
                    }
                }
            }
        }
    
    1. 测试analyzer
    GET /indexusers/_analyze
    {
      "text": ["南岩飞雪"],
      "analyzer": "pinyin_analyzer"
    }
    

    Response

    {
      "tokens" : [
        {
          "token" : "nan",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "南岩飞雪",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "nyfx",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "yan",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 1
        },
        {
          "token" : "fei",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 2
        },
        {
          "token" : "xue",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 3
        }
      ]
    }
    
    1. 添加数据,使用pinyin查询
    PUT indexusers/user/id000
    {
      "name" : "南岩飞雪"
    }
    
    GET indexusers/_search?q=name.pinyin:nyfx
    
    1. pinyin_analyzer生效后,后续更新的数据,name字段都支持pinyin分词检索;之前的数据,需要自己触发更新

    方法二,重建索引

    1. 新建索引
    PUT /indexusers2/ 
    {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "pinyin_analyzer" : {
                        "tokenizer" : "my_pinyin"
                        }
                },
                "tokenizer" : {
                    "my_pinyin" : {
                        "type" : "pinyin",
                        "keep_separate_first_letter" : false,
                        "keep_full_pinyin" : true,
                        "keep_original" : true,
                        "limit_first_letter_length" : 16,
                        "lowercase" : true,
                        "remove_duplicated_term" : true
                    }
                }
            }
        }
    }
    
    1. _reindex
    POST _reindex
    {
      "source": {
        "index": "indexusers"
      },
      "dest": {
        "index": "indexusers2"
    }}
    
    1. 索引别名
    {
        "actions":[
            {
                "remove":{
                    "alias":"product",
                    "index":"indexusers"
                }
            },
            {
                "add":{
                    "alias":"product",
                    "index":"indexusers2"
                }
            }
        ]
    }
    

    相关文章

      网友评论

          本文标题:ElasticSearch(ES)索引更新

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