Elasticsearch使用脚本映射自定义属性

时间:2016-09-05 09:34:09

标签: elasticsearch mapping

copy()

我有映射。使用此脚本

对custom_score进行计算
{
  "mappings": {
    "exam": {
        "properties": {
            "id": {
                "type": "long"
            },
            "score": {
                "type": "integer"
            },
            "custom_score": {
                "type": "integer"
            }
        }
    }
  }
}

有弹性搜索自动索引此字段吗?我想使用此值对某些查询进行一些排序。感谢

1 个答案:

答案 0 :(得分:1)

您可以使用transform但请注意,此功能在2.x中已弃用,并且将在ES 5中删除.ES 5的唯一选项是在您自己的客户端代码中进行转换,索引已相应更改的值。

但是,现在,使用变换:

{
  "mappings": {
    "exam": {
      "transform": {
        "script": "if (ctx._source['score'].toInteger()>=0) ctx._source['custom_score'] = ctx._source['score'].toInteger(); else ctx._source['custom_score'] = ctx._source['score'].toInteger()-100"
      },
      "properties": {
        "id": {
          "type": "long"
        },
        "score": {
          "type": "integer"
        },
        "custom_score": {
          "type": "integer"
        }
      }
    }
  }
}