title: painless: check if an individual document contains a key
body: <p>I'm using <a href="https://www.elastic.co/guide/en/elasticsearch/painless/5.5/index.html" rel="noreferrer">painless</a> to filter documents with Elastic 5.5</p>
  
  <h2>Problem</h2>
  
  <p>Using "painless", find documents with <code>strings</code> field.</p>
  
  <h3>Expected Results</h3>
  
  <p>Only documents with <code>strings</code> field are returned</p>
  
  <h3>Actual Results</h3>
  
  <p>All documents are returned.</p>
  
  <h3>Observation</h3>
  
  <p>All documents are returned, as long as there's a document with <code>strings</code> field. This could be a caching issue of some sort.</p>
  
  <h2>TestCase</h2>
  
  <h3>Fixtures</h3>
  
  <pre><code>PUT /test_idx
  
  POST /test_idx/t/1
  {
        "strings": ["hello", "world"]
  }
  
  POST /test_idx/t/2
  {
        "numbers": [1, 2, 3]
  }
  </code></pre>
  
  <h3>Query</h3>
  
  <pre><code>GET /test_idx/_search
  {
     "query": {
        "bool": {
           "filter": [
              {
                 "script": {
                    "script": {
                       "lang": "painless",
                       "inline": "return doc.containsKey(params.keypath)",
                       "params": {"keypath": "strings"}
                    }
                 }
              }
           ]
        }
     }
  }
  </code></pre>
  
  <h3>Actual Response</h3>
  
  <pre><code>{
     "took": 5,
     "timed_out": false,
     "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
     },
     "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
           {
              "_index": "test_idx",
              "_type": "t",
              "_id": "2",
              "_score": 0,
              "_source": {
                 "numbers": [
                    1,
                    2,
                    3
                 ]
              }
           },
           {
              "_index": "test_idx",
              "_type": "t",
              "_id": "1",
              "_score": 0,
              "_source": {
                 "strings": [
                    "hello",
                    "world"
                 ]
              }
           }
        ]
     }
  }
  </code></pre>
  
  <h2>Expected Response</h2>
  
  <pre><code>{
     "took": 5,
     "timed_out": false,
     "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
     },
     "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
           {
              "_index": "test_idx",
              "_type": "t",
              "_id": "1",
              "_score": 0,
              "_source": {
                 "strings": [
                    "hello",
                    "world"
                 ]
              }
           }
        ]
     }
  }
  </code></pre>