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

i have tihs mapping. The custom_score is calculcated with this script

if(score >= 0)
    custom_score = score
else
    custom_score = score-100

Is it possible elasticsearch auto index this field? I want to use this value to make some sortings to some queries. Thanks

1 Answer 1

1

You can use a transform but be careful that this feature is deprecated in 2.x and will be removed in ES 5. The only options remaining for ES 5 is to do the transformation in your own client code and index the value already changed accordingly.

But, for now, using transforms:

{
  "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"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.