前言:

为了实际一下ElasticSearch的自定义相关度评分,应用了Expression脚本。然而在执行过程中却报错了,最初通过批改索引映射,增加别名和同步旧索引数据得以解决,所以以下也围绕这几项问题开展。 波及常识:索引映射批改索引别名应用重建索引相关度评分 

问题:

  1. 评分脚本执行报错。   
    (以下都是用Kibana的图形化开发工具测试ES的API),以后索引是一个一般的产品数据,包含产品ID,产品名称,产品别名,产品品类,价格,排序编号。上面只是想搜寻某产品名,依照排序编号的相关度相乘的排序形式,然而却报错。 (注:_search下取文档的值是doc, 其余状况下ctx)
GET products_new/_search{  "query": {    "function_score": {      "query": {        "match": {          "goods_name": 12        }      },      "script_score": {        "script": {          "lang": "expression",          "source": "_score * doc['goods_sort']"        }        }    }  }}

  1. 批改索引映射报错。        
    依据报错提醒是因为脚本中执行的字段非数据,非日期和非地理位置类型,所以就须要对之前索引的字段类型进行批改。然而索引映射对字段的批改只能增加属性不能批改,mapper [goods_sort] of different type, current_type [keyword], merged_type [long],解决这个就须要重建索引。

索引重建

  1. 给旧索引增加别名。

    POST /_aliases{  "actions": [ {   "add": {     "index": "products",     "alias": "products_alias"   } }  ]}
  2. 依据旧索引创立新字段类型的映射。
PUT products_new {  "mappings": {    "properties": {      "class_id": {        "type": "long"      },      "goods_name": {        "type": "text",        "analyzer": "ik_smart"      },      "goods_sort": {        "type": "long"      },      "id": {        "type": "long"      },      "price": {        "type": "text",        "fields": {          "keyword": {            "type": "keyword",            "ignore_above": 256            }        }      },      "single_goods_name": {        "type": "text",        "analyzer": "ik_max_word"      },      "state": {        "type": "long"      }    }  }}
  1. 同步旧索引数据到新索引。
POST /_reindex{  "source": {    "index": "products"  },  "dest": {    "index": "products_new"  }}
  1. 给新索引增加和旧索引雷同的别名并移除旧索引别名。

    POST /_aliases{  "actions": [ {   "add": {     "index": "products_new",     "alias": "products_alias"   },   "remove": {     "index": "products",     "alias": "products_alias"   } }  ]}
  2. 删除旧索引。DELETE products
DELETE products{}
  1. 通过别名应用脚本评分。
GET products_alias/_search{  "query": {    "function_score": {      "query": {        "match": {          "goods_name": 12        }      },      "script_score": {        "script": {          "lang": "expression",          "source": "_score * doc['goods_sort']"        }        }    }  }}

为什么要给旧索引创立了别名,又前面移除再删除。那是因为批改了索引映射相当于进行了业务调整,为了不影响程序增加别名的状况下。等数据全副同步后再移除和删除就不会影响到业务了。