# Error with Term Frequency Script combined with Aggregation script

**URL:** <https://discuss.elastic.co/t/error-with-term-frequency-script-combined-with-aggregation-script/32321>\
**Category:** Elasticsearch\
**Created:** [October 16, 2015, 12:07am UTC](https://discuss.elastic.co/t/error-with-term-frequency-script-combined-with-aggregation-script/32321 "2015-10-16T00:07:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sethbb](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sethbb](https://discuss.elastic.co/u/sethbb)\
**Post date:** [October 16, 2015, 12:07am UTC](https://discuss.elastic.co/t/error-with-term-frequency-script-combined-with-aggregation-script/32321/1 "2015-10-16T00:07:28Z")

</div>

I have two scripts in my query, one that gets a term frequency and one that calculates an aggregate. If I run this query with just one of the scripts present, it runs find. However , if both are present, then I get an error that looks like this:

```
+org.elasticsearch.index.search.nested.NonNestedDocsFilter@b2a8151b)],from[0],size[0]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[ElasticsearchParseException[failed to parse / load source]; nested: IndexOutOfBoundsException[docID must be >= 0 and < maxDoc=39 (got docID=7623)]; ]; "

```

Here's my query:

```
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "has_parent": {
                "type": "article",
                "filter": {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "profile_id": 12
                        }
                      },
                      {
                        "script": {
                          "script": "_index['title'][term].tf() >= frequency",
                          "params": {
                            "term": "foobar",
                            "frequency": 1
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "revenue": {
      "sum": {
        "script": "Map map = new HashMap(); for(field in ['total_goals','total_events']){if(_source.containsKey(field)){for(e in _source[field]){ map.put(field+e.name, e.value.intValue());}}}; doc['revenue'].value "
      }
    }
  },
  "size": 0
}

```

Anyone have any idea what's going on? Why should these scripts affect each other?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:44pm UTC](https://discuss.elastic.co/t/error-with-term-frequency-script-combined-with-aggregation-script/32321/2 "2017-07-05T23:44:29Z")

</div>

Closed on July 5, 2017, 11:44pm UTC


