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

标签 elasticsearch mapping

{
  "mappings": {
    "exam": {
        "properties": {
            "id": {
                "type": "long"
            },
            "score": {
                "type": "integer"
            },
            "custom_score": {
                "type": "integer"
            }
        }
    }
  }
}

我有 map 。使用此脚本计算custom_score
if(score >= 0)
    custom_score = score
else
    custom_score = score-100

Elasticsearch可以自动索引此字段吗?我想使用此值对某些查询进行一些排序。谢谢

最佳答案

您可以使用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"
        }
      }
    }
  }
}

关于elasticsearch - Elasticsearch使用脚本映射自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327669/

相关文章:

search - ElasticSearch中 token 过滤器的控制顺序

elasticsearch - 从 SonarQube 6.5 升级到 6.6 后出现 elasticsearch "can not run as root"错误。没有其他改变

python - 如何在 cython 中制作从 C struct 到 int 的 C++ 映射?

python - 如何在 Python 中创建映射(关联子数组)的列表(数组)

python - 将列从一个数据帧映射到另一个数据帧以创建新列

elasticsearch - ElasticSearch结果的变化

java - 如何设置日期格式以插入 Elasticsearch

java - 无法在 Elasticsearch 中对大文件进行批量索引

silverlight - 字体粗细和样式在 silverlight 中未正确映射

asp.net - .NET ODP 托管驱动程序用户映射不起作用