title: How to access `matched_queries` in a `painless` script for creating buckets
body: <p>I am working with Elasticsearch 5.3.3 on AWS</p>
  
  <p>I have multiple filters in a search query, each with an <code>_name</code>. These names are reflected in the results as <code>matched_queries</code>. I would like to group these results into different buckets where the selection criteria for each bucket is the presence of one or more of the filter names in <code>matched_queries</code>.</p>
  
  <p>Here is an example request:</p>
  
  <pre><code>GET my_index/_search
  {
    "query": {
      "bool": {
        "must": [
          { "terms": { "bed": [0, 1, 2] } },
          { "terms": { "contract": ["none", "good", "tight"] } }
        ],
        "should": [
          { 
            "terms": { 
              "bed": ["0", "1"],
              "_name": "exact_beds"
            }
          },
          {
            "term": {
              "bed": {
                "value": 2,
                "_name": "increased_beds"
              }
            }
          },
          {
            "terms": {
              "contract": ["good", "tight"],
              "_name": "money"
            }
          },
          {
            "term": {
              "contract": {
                "value": "none",
                "_name": "no_money"
              }
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>It will output something like</p>
  
  <pre><code>{
    "took": 19,
    "timed_out": false,
    "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
    },
    "hits": {
      "total": 297256,
      "max_score": 5.8605247,
      "hits": [
        {
          "_index": "my_index",
          "_type": "listing",
          "_id": "49932563",
          "_score": 5.8605247,
          "_source": {
            "bed": 1,
            "contract": "good",
          },
          "matched_queries": [
            "money",
            "exact_beds"
          ]
        }
      ]
    }
  }
  </code></pre>
  
  <p>I would like to add something like this to my request</p>
  
  <pre><code>"aggs": {
    "E1": {
      "filter": {
        "script": {
          "script": {
            "lang": "painless",
            "inline": "matched_queries.contains('exact_beds') &amp;&amp; matched_queries.contains('money')"
          }
        }
      }
    }
  </code></pre>
  
  <p>But I get an error saying <code>"Variable [matched_queries] is not defined."</code>. Is there any way to access <code>matched_queries</code>?</p>