title: Field [] used in expression does not exist in mappings
body: <p>The feature I try to fullfit is to create a metric in kibana that display the number of users "unvalidated".
  I send a log sent when a user registers, then a log when a user is validated.</p>
  
  <p>So the count I want is the difference between the number of registered and the number of validated.</p>
  
  <p>In kibana I cannot do such a math operation, so I found a workaround:
  I added a "scripted field" named "unvalidated" which is equal to 1 when a user registers and -1 when a user validates his account.
  The sum of the "unvalidated" field should be the number of unvalidated users.</p>
  
  <p>This is the script I defined in my scripted field:
  <code>
  doc['ctxt_code'].value == 1 ? 1 : doc['ctxt_code'].value == 2 ? -1 : 0
  </code></p>
  
  <p>with:</p>
  
  <ul>
  <li><p>ctxt_code 1 as the register log</p></li>
  <li><p>ctxt_code 2 as the validated log</p></li>
  </ul>
  
  <p>This setup works well when all my logs have a "ctxt_code", but when a log without this field is pushed kibana throws the following error:</p>
  
  <p><code>
  Field [ctxt_code] used in expression does not exist in mappings
  </code>
  <a href="https://i.stack.imgur.com/4B6px.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4B6px.jpg" alt="kibana error"></a></p>
  
  <p>I can't understand this error because kibana says:</p>
  
  <p><code>
  If a field is sparse (only some documents contain a value), documents missing the field will have a value of 0
  </code></p>
  
  <p>which is the case.</p>
  
  <p>Anyone has a clue ?</p>