/**
     * We can only do this if dynamic scripting is enabled. In elasticsearch.yml:
     * script.disable_dynamic: false
     * @see vendor/ruffin/elastica/test/bin/run_elasticsearch.sh
     *
     * @param array $terms
     * @return \Elastica\Aggregation\Sum
     */
    protected function termsAggregation(array $terms)
    {
        $terms = str_replace('"', '\\"', $terms);
        $script = '
keywords = ["' . implode('","', $terms) . '"]
total = 0
for (term in keywords) {
	total += _index["revisions.text"][term].tf()
}
return total';
        $script = new \Elastica\Script($script, null, 'groovy');
        $aggregation = new \Elastica\Aggregation\Sum('ttf');
        // $aggregation->setScript() doesn't seem to properly set 'lang': 'groovy'
        // see https://github.com/ruflin/Elastica/pull/748
        // $aggregation->setScript( $script );
        $aggregation->setParams(array('lang' => 'groovy'));
        $aggregation->setParam('script', $script->getScript());
        return $aggregation;
    }