title: How to subtract two values in Elasticsearch
body: <p>I'm trying to get the difference between two fields. I'm using Elasticsearch 5.5.</p>
  
  <p>I have already tried</p>
  
  <pre><code>{
  "query": {
  "bool": {
  "filter": {
  "script": {
  "script": "doc['students.total_fee'].value - doc['students.paid_fee'].value"
  }
  }
  }
  }
  }
  </code></pre>
  
  <p>but it is returning empty "hits".</p>
  
  <p>I have also tried</p>
  
  <pre><code>{
  "query": {
  "bool": {
  "must": [
  {
  "nested": {
  "path": "students",
  "query": {
  "script": {
  "script": {
  "inline": "doc['students.total_fee'].value - doc['students.paid_fee'].value",
  "lang": "expression"
  }
  }
  }
  }
  }
  ]
  }
  }
  </code></pre>
  
  <p>and it is returning "0".</p>
  
  <p>also the "script_fields" did not worked.</p>
  
  <pre><code>{ "script_fields" :
   { "difference" :
   { "script" : "doc['students.total_fee'].value - doc['students.paid_fee'].value"
   } } }
  </code></pre>
  
  <p>suppose I have data in following format.</p>
  
  <pre><code>"_source" : {
  "students" : [
  {
  "name" : "A",
  "total_fee" : 12345,
  "paid_fee" : 12344.8
  },
  {
  "name" : "B",
  "total_fee" : 23456,
  "paid_fee" : 23455.6
  }
  ]
  }
  </code></pre>
  
  <p>Now I want to get the difference between "total_fee" and "paid_fee" for each student.</p>
  
  <p>I expect to get an array of differences for all students.</p>
  
  <p>Thanks in advance :D</p>