# Top Hits aggregator from example not working for me

**URL:** <https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869>\
**Category:** Elasticsearch\
**Created:** [July 24, 2014, 3:17pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869 "2014-07-24T15:17:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Yim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_yim/32/1367_2.png) [@Daniel\_Yim](https://discuss.elastic.co/u/Daniel_Yim)\
**Post date:** [July 24, 2014, 3:17pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/1 "2014-07-24T15:17:26Z")

</div>

Hello, I am trying to follow the _field collapse example_ on this page  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example),  
which uses the new 1.3.0 _top\_hits_ aggregator to return the top scoring  
document for a given criteria. To my knowledge, I set up the settings and  
mappings correctly, but I am getting a strange error when I try to run the  
query that the example provided.

curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
{  
"settings": {  
"index": {  
"analysis": {  
"analyzer": {  
"idx\_analyzer": {  
"tokenizer": "whitespace",  
"filter": [  
"lowercase",  
"snowball",  
"XYZSynFilter"  
]  
},  
"sch\_analyzer": {  
"tokenizer": "standard",  
"filter": [  
"standard",  
"lowercase",  
"stop"  
]  
},  
"sch\_comma\_analyzer": {  
"tokenizer": "CommaTokenizer",  
"filter": [  
"standard",  
"lowercase",  
"stop"  
]  
}  
},  
"filter": {  
"XYZSynFilter": {  
"type": "synonym",  
"synonyms": [  
"aids virus, aids, retrovirology, hiv"  
],  
"expand": true,  
"ignore\_case": true  
}  
},  
"tokenizer": {  
"CommaTokenizer": {  
"type": "pattern",  
"pattern": ","  
}  
}  
}  
}  
},  
"mappings": {  
"employees": {  
"properties": {  
"fullName": {  
"type": "string",  
"search\_analyzer": "sch\_analyzer"  
},  
"specialty": {  
"type": "string",  
"search\_analyzer": "sch\_comma\_analyzer"  
}  
}  
}  
}  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
{  
"fullName": "Don White",  
"specialty": "Adult Retrovirology, aids, hiv"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
{  
"fullName": "Don White",  
"specialty": "general practitioner, physician, general, primary care"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
{  
"fullName": "Don White",  
"specialty": "icu, er"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
{  
"fullName": "Terrance Gartner",  
"specialty": "oncology, cancer, research, tumor, polyp"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
{  
"fullName": "Terrance Gartner",  
"specialty": "physician, general, GP, primary care, aids"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
{  
"fullName": "Terrance Gartner",  
"specialty": "emergency care, icu, ambulance, er, urgent"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
{  
"fullName": "Carter Taylor",  
"specialty": "neurosurgery, brain surgery, brain tumor"  
}'  
curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
{  
"fullName": "Carter Taylor",  
"specialty": "trauma, icu, emergency care, ER, urgent care"  
}'

Executing this search (per the example) gives me an error  
curl -XGET  
"[http://localhost:9200/personsearch/employees/\_search?pretty=true](http://localhost:9200/personsearch/employees/_search?pretty=true)" -d'  
{  
"query": {  
"query\_string": {  
"query": "icu"  
}  
},  
"aggs": {  
"most-rel-profile": {  
"terms": {  
"field": "profileName",  
"order": {  
"top\_hit": "desc"  
}  
},  
"aggs": {  
"top\_tags\_hits": {  
"top\_hits": {}  
},  
"top\_hit": {  
"max": {  
"script": "\_doc.score"  
}  
}  
}  
}  
}  
}'

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
all shards failed; shardFailures  
{[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]:  
SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n  
"query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
{\n "most-rel-profile": {\n "terms": {\n "field":  
"profileName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "top\_tags\_hits": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "script": "\_doc.score"\n }\n }\n  
}\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
[mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]:  
SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n  
"query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
{\n "most-rel-profile": {\n "terms": {\n "field":  
"profileName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "top\_tags\_hits": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "script": "\_doc.score"\n }\n }\n  
}\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
[mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]:  
SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n  
"query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
{\n "most-rel-profile": {\n "terms": {\n "field":  
"profileName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "top\_tags\_hits": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "script": "\_doc.score"\n }\n }\n  
}\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
[mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]:  
SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n  
"query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
{\n "most-rel-profile": {\n "terms": {\n "field":  
"profileName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "top\_tags\_hits": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "script": "\_doc.score"\n }\n }\n  
}\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
[mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]:  
SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n  
"query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
{\n "most-rel-profile": {\n "terms": {\n "field":  
"profileName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "top\_tags\_hits": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "script": "\_doc.score"\n }\n }\n  
}\n }\n }\n}\n]]]; nested:\* ScriptException[dynamic scripting for  
[mvel] disabled];\* }]",  
"status": 400  
}

Does the _dynamic scripting for [mvel] disabled_ have something to do with  
it?

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rafal\_Kuc\_3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rafal_kuc_3/32/799_2.png) [@Rafal\_Kuc\_3](https://discuss.elastic.co/u/Rafal_Kuc_3)\
**Post date:** [July 24, 2014, 3:27pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/2 "2014-07-24T15:27:54Z")

</div>

Hello!

The error is about scripting and dynamic scripting being disabled for MVEL. The simplest way to make it work is turning on dynamic scripting by adding script.disable\_dynamic: false to your elasticsearch.yml file. However for production it is not recommended for security reasons. You can also try using scripting language that is sandboxed and allows for dynamic scripting, like groovy.

_--_

Regards,

Rafał Kuć

Performance Monitoring \* Log Analytics \* Search Analytics

Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

Hello, I am trying to follow the [field collapse example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)[on this page](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example), which uses the new 1.3.0 **top\_hits** aggregator to return the top scoring document for a given criteria. To my knowledge, I set up the settings and mappings correctly, but I am getting a strange error when I try to run the query that the example provided.

curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"

curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'

{

"settings": {

```
"index": {

  "analysis": {

    "analyzer": {

      "idx_analyzer": {

        "tokenizer": "whitespace",

        "filter": [

          "lowercase",

          "snowball",

          "XYZSynFilter"

        ]

      },

      "sch_analyzer": {

        "tokenizer": "standard",

        "filter": [

          "standard",

          "lowercase",

          "stop"

        ]

      },

      "sch_comma_analyzer": {

        "tokenizer": "CommaTokenizer",

        "filter": [

          "standard",

          "lowercase",

          "stop"

        ]

      }

    },

    "filter": {

      "XYZSynFilter": {

        "type": "synonym",

        "synonyms": [

          "aids virus, aids, retrovirology, hiv"

        ],

        "expand": true,

        "ignore_case": true

      }

    },

    "tokenizer": {

      "CommaTokenizer": {

        "type": "pattern",

        "pattern": ","

      }

    }

  }

}

```

},

"mappings": {

```
"employees": {

  "properties": {

    "fullName": {

      "type": "string",

      "search_analyzer": "sch_analyzer"

    },

    "specialty": {

      "type": "string",

      "search_analyzer": "sch_comma_analyzer"

    }

  }

}

```

}

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'

{

"fullName": "Don White",

"specialty": "Adult Retrovirology, aids, hiv"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'

{

"fullName": "Don White",

"specialty": "general practitioner, physician, general, primary care"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'

{

"fullName": "Don White",

"specialty": "icu, er"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "oncology, cancer, research, tumor, polyp"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "physician, general, GP, primary care, aids"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "emergency care, icu, ambulance, er, urgent"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'

{

"fullName": "Carter Taylor",

"specialty": "neurosurgery, brain surgery, brain tumor"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'

{

"fullName": "Carter Taylor",

"specialty": "trauma, icu, emergency care, ER, urgent care"

}'

Executing this search (per the example) gives me an error

curl -XGET "[http://localhost:9200/personsearch/employees/\_search?pretty=true](http://localhost:9200/personsearch/employees/_search?pretty=true)" -d'

{

"query": {

```
"query_string": {

  "query": "icu"

}

```

},

"aggs": {

```
"most-rel-profile": {

  "terms": {

    "field": "profileName",

    "order": {

      "top_hit": "desc"

    }

  },

  "aggs": {

    "top_tags_hits": {

      "top_hits": {}

    },

    "top_hit": {

      "max": {

        "script": "_doc.score"

      }

    }

  }

}

```

}

}'

{

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]: SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]: SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]: SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]: SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]: SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: **ScriptException[dynamic scripting for [mvel] disabled];** }]",

"status": 400

}

Does the **dynamic scripting for [mvel] disabled** have something to do with it?

Thanks.

--

You received this message because you are subscribed to the Google Groups "elasticsearch" group.

To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).

To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer).

For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Daniel\_Yim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_yim/32/1367_2.png) [@Daniel\_Yim](https://discuss.elastic.co/u/Daniel_Yim)\
**Post date:** [July 24, 2014, 4:22pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/3 "2014-07-24T16:22:41Z")

</div>

Thanks, Rafal. The \*top\_hits \*aggregator is working now, but could you  
explain why this is a security risk? We will need to use this feature in  
production and now I am feeling uneasy about it.

On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:

> Hello!
> 
> The error is about scripting and dynamic scripting being disabled for  
> MVEL. The simplest way to make it work is turning on dynamic scripting by  
> adding script.disable\_dynamic: false to your elasticsearch.yml file.  
> However for production it is not recommended for security reasons. You can  
> also try using scripting language that is sandboxed and allows for dynamic  
> scripting, like groovy.
> 
> \*-- Regards, Rafał Kuć Performance Monitoring \* Log Analytics \* Search  
> Analytics Solr & Elasticsearch Support \* \*[http://sematext.com/](http://sematext.com/)
> 
> Hello, I am trying to follow the field collapse example  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> on this page  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example),  
> which uses the new 1.3.0 _top\_hits_ aggregator to return the top scoring  
> document for a given criteria. To my knowledge, I set up the settings and  
> mappings correctly, but I am getting a strange error when I try to run the  
> query that the example provided.
> 
> curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
> curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
> {  
> "settings": {  
> "index": {  
> "analysis": {  
> "analyzer": {  
> "idx\_analyzer": {  
> "tokenizer": "whitespace",  
> "filter": [  
> "lowercase",  
> "snowball",  
> "XYZSynFilter"  
> ]  
> },  
> "sch\_analyzer": {  
> "tokenizer": "standard",  
> "filter": [  
> "standard",  
> "lowercase",  
> "stop"  
> ]  
> },  
> "sch\_comma\_analyzer": {  
> "tokenizer": "CommaTokenizer",  
> "filter": [  
> "standard",  
> "lowercase",  
> "stop"  
> ]  
> }  
> },  
> "filter": {  
> "XYZSynFilter": {  
> "type": "synonym",  
> "synonyms": [  
> "aids virus, aids, retrovirology, hiv"  
> ],  
> "expand": true,  
> "ignore\_case": true  
> }  
> },  
> "tokenizer": {  
> "CommaTokenizer": {  
> "type": "pattern",  
> "pattern": ","  
> }  
> }  
> }  
> }  
> },  
> "mappings": {  
> "employees": {  
> "properties": {  
> "fullName": {  
> "type": "string",  
> "search\_analyzer": "sch\_analyzer"  
> },  
> "specialty": {  
> "type": "string",  
> "search\_analyzer": "sch\_comma\_analyzer"  
> }  
> }  
> }  
> }  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
> {  
> "fullName": "Don White",  
> "specialty": "Adult Retrovirology, aids, hiv"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
> {  
> "fullName": "Don White",  
> "specialty": "general practitioner, physician, general, primary care"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
> {  
> "fullName": "Don White",  
> "specialty": "icu, er"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
> {  
> "fullName": "Terrance Gartner",  
> "specialty": "oncology, cancer, research, tumor, polyp"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
> {  
> "fullName": "Terrance Gartner",  
> "specialty": "physician, general, GP, primary care, aids"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
> {  
> "fullName": "Terrance Gartner",  
> "specialty": "emergency care, icu, ambulance, er, urgent"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
> {  
> "fullName": "Carter Taylor",  
> "specialty": "neurosurgery, brain surgery, brain tumor"  
> }'  
> curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
> {  
> "fullName": "Carter Taylor",  
> "specialty": "trauma, icu, emergency care, ER, urgent care"  
> }'
> 
> Executing this search (per the example) gives me an error  
> curl -XGET "  
> [http://localhost:9200/personsearch/employees/\_search?pretty=true](http://localhost:9200/personsearch/employees/_search?pretty=true)" -d'  
> {  
> "query": {  
> "query\_string": {  
> "query": "icu"  
> }  
> },  
> "aggs": {  
> "most-rel-profile": {  
> "terms": {  
> "field": "profileName",  
> "order": {  
> "top\_hit": "desc"  
> }  
> },  
> "aggs": {  
> "top\_tags\_hits": {  
> "top\_hits": {}  
> },  
> "top\_hit": {  
> "max": {  
> "script": "\_doc.score"  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase [query],  
> all shards failed; shardFailures  
> {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]:  
> SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
> Parse Failure [Failed to parse source [{\n "query": {\n  
> "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> {\n "most-rel-profile": {\n "terms": {\n "field":  
> "profileName",\n "order": {\n "top\_hit": "desc"\n  
> }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> {\n "script": "\_doc.score"\n }\n }\n  
> }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]:  
> SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
> Parse Failure [Failed to parse source [{\n "query": {\n  
> "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> {\n "most-rel-profile": {\n "terms": {\n "field":  
> "profileName",\n "order": {\n "top\_hit": "desc"\n  
> }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> {\n "script": "\_doc.score"\n }\n }\n  
> }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]:  
> SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
> Parse Failure [Failed to parse source [{\n "query": {\n  
> "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> {\n "most-rel-profile": {\n "terms": {\n "field":  
> "profileName",\n "order": {\n "top\_hit": "desc"\n  
> }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> {\n "script": "\_doc.score"\n }\n }\n  
> }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]:  
> SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
> Parse Failure [Failed to parse source [{\n "query": {\n  
> "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> {\n "most-rel-profile": {\n "terms": {\n "field":  
> "profileName",\n "order": {\n "top\_hit": "desc"\n  
> }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> {\n "script": "\_doc.score"\n }\n }\n  
> }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]:  
> SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
> Parse Failure [Failed to parse source [{\n "query": {\n  
> "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> {\n "most-rel-profile": {\n "terms": {\n "field":  
> "profileName",\n "order": {\n "top\_hit": "desc"\n  
> }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> {\n "script": "\_doc.score"\n }\n }\n  
> }\n }\n }\n}\n]]]; nested:\* ScriptException[dynamic scripting for  
> [mvel] disabled];\* }]",  
> "status": 400  
> }
> 
> Does the _dynamic scripting for [mvel] disabled_ have something to do  
> with it?
> 
> ## Thanks.
> 
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rafal\_Kuc\_3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rafal_kuc_3/32/799_2.png) [@Rafal\_Kuc\_3](https://discuss.elastic.co/u/Rafal_Kuc_3)\
**Post date:** [July 24, 2014, 4:30pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/4 "2014-07-24T16:30:12Z")

</div>

Hello Daniel!

MVEL scripts can't be sandboxed and they provide a lot of features, however when running Elasticsearch with too much user rights one can exploit that situation, i.e. [http://bouk.co/blog/elasticsearch-rce/.](http://bouk.co/blog/elasticsearch-rce/.) This resulted in dynamic scripting being disabled by default in Elasticsearch 1.2.0 ([http://www.elasticsearch.org/blog/elasticsearch-1-2-0-released/)](http://www.elasticsearch.org/blog/elasticsearch-1-2-0-released/)) and enabled again in 1.3.0 ([http://www.elasticsearch.org/blog/elasticsearch-1-3-0-released/)](http://www.elasticsearch.org/blog/elasticsearch-1-3-0-released/)) for languages like Groovy which can be sandboxed.

_--_

Regards,

Rafał Kuć

Performance Monitoring \* Log Analytics \* Search Analytics

Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

Thanks, Rafal. The **top\_hits** aggregator is working now, but could you explain why this is a security risk? We will need to use this feature in production and now I am feeling uneasy about it.

On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:

Hello!

The error is about scripting and dynamic scripting being disabled for MVEL. The simplest way to make it work is turning on dynamic scripting by adding script.disable\_dynamic: false to your elasticsearch.yml file. However for production it is not recommended for security reasons. You can also try using scripting language that is sandboxed and allows for dynamic scripting, like groovy.

_--_

Regards,

Rafał Kuć

Performance Monitoring \* Log Analytics \* Search Analytics

Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

Hello, I am trying to follow the [field collapse example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)[on this page](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example), which uses the new 1.3.0 **top\_hits** aggregator to return the top scoring document for a given criteria. To my knowledge, I set up the settings and mappings correctly, but I am getting a strange error when I try to run the query that the example provided.

curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"

curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'

{

"settings": {

"index": {

```
 "analysis": {

   "analyzer": {

     "idx_analyzer": {

       "tokenizer": "whitespace",

       "filter": [

         "lowercase",

         "snowball",

         "XYZSynFilter"

       ]

     },

     "sch_analyzer": {

       "tokenizer": "standard",

       "filter": [

         "standard",

         "lowercase",

         "stop"

       ]

     },

     "sch_comma_analyzer": {

       "tokenizer": "CommaTokenizer",

       "filter": [

         "standard",

         "lowercase",

         "stop"

       ]

     }

   },

   "filter": {

     "XYZSynFilter": {

       "type": "synonym",

       "synonyms": [

         "aids virus, aids, retrovirology, hiv"

       ],

       "expand": true,

       "ignore_case": true

     }

   },

   "tokenizer": {

     "CommaTokenizer": {

       "type": "pattern",

       "pattern": ","

     }

   }

 }

```

}

},

"mappings": {

"employees": {

```
 "properties": {

   "fullName": {

     "type": "string",

     "search_analyzer": "sch_analyzer"

   },

   "specialty": {

     "type": "string",

     "search_analyzer": "sch_comma_analyzer"

   }

 }

```

}

}

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'

{

"fullName": "Don White",

"specialty": "Adult Retrovirology, aids, hiv"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'

{

"fullName": "Don White",

"specialty": "general practitioner, physician, general, primary care"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'

{

"fullName": "Don White",

"specialty": "icu, er"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "oncology, cancer, research, tumor, polyp"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "physician, general, GP, primary care, aids"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'

{

"fullName": "Terrance Gartner",

"specialty": "emergency care, icu, ambulance, er, urgent"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'

{

"fullName": "Carter Taylor",

"specialty": "neurosurgery, brain surgery, brain tumor"

}'

curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'

{

"fullName": "Carter Taylor",

"specialty": "trauma, icu, emergency care, ER, urgent care"

}'

Executing this search (per the example) gives me an error

curl -XGET "[http://localhost:9200/personsearch/employees/\_search?pretty=true](http://localhost:9200/personsearch/employees/_search?pretty=true)" -d'

{

"query": {

"query\_string": {

```
 "query": "icu"

```

}

},

"aggs": {

"most-rel-profile": {

```
 "terms": {

   "field": "profileName",

   "order": {

     "top_hit": "desc"

   }

 },

 "aggs": {

   "top_tags_hits": {

     "top_hits": {}

   },

   "top_hit": {

     "max": {

       "script": "_doc.score"

     }

   }

 }

```

}

}

}'

{

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]: SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]: SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]: SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]: SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]: SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n "terms": {\n "field": "profileName",\n "order": {\n "top\_hit": "desc"\n }\n },\n "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n },\n "top\_hit": {\n "max": {\n "script": "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]]; nested: **ScriptException[dynamic scripting for [mvel] disabled];** }]",

"status": 400

}

Does the **dynamic scripting for [mvel] disabled** have something to do with it?

Thanks.

--

You received this message because you are subscribed to the Google Groups "elasticsearch" group.

To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearc...@googlegroups.com.

To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer).

For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).--

You received this message because you are subscribed to the Google Groups "elasticsearch" group.

To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).

To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm_medium=email&utm_source=footer).

For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [July 24, 2014, 5:05pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/5 "2014-07-24T17:05:10Z")

</div>

You can switch to Lucene "expression" language to access score.

The example in the docs is a bit unfortunate, as it uses MVEL, which has to  
be enabled in ES 1.3 explicitly.

Here is a modified example I made using the "expression" language:

> <https://gist.github.com/jprante/7a1763c7073f2b8084a6>

Jörg

On Thu, Jul 24, 2014 at 6:22 PM, Daniel Yim [danielyim@gmail.com](mailto:danielyim@gmail.com) wrote:

> Thanks, Rafal. The \*top\_hits \*aggregator is working now, but could you  
> explain why this is a security risk? We will need to use this feature in  
> production and now I am feeling uneasy about it.
> 
> On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:
> 
> > Hello!
> > 
> > The error is about scripting and dynamic scripting being disabled for  
> > MVEL. The simplest way to make it work is turning on dynamic scripting by  
> > adding script.disable\_dynamic: false to your elasticsearch.yml file.  
> > However for production it is not recommended for security reasons. You can  
> > also try using scripting language that is sandboxed and allows for dynamic  
> > scripting, like groovy.
> > 
> > \*-- Regards, Rafał Kuć Performance Monitoring \* Log Analytics \* Search  
> > Analytics Solr & Elasticsearch Support \* \*[http://sematext.com/](http://sematext.com/)
> > 
> > Hello, I am trying to follow the field collapse example  
> > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > on this page  
> > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > , which uses the new 1.3.0 _top\_hits_ aggregator to return the top  
> > scoring document for a given criteria. To my knowledge, I set up the  
> > settings and mappings correctly, but I am getting a strange error when I  
> > try to run the query that the example provided.
> > 
> > curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
> > curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
> > {  
> > "settings": {  
> > "index": {  
> > "analysis": {  
> > "analyzer": {  
> > "idx\_analyzer": {  
> > "tokenizer": "whitespace",  
> > "filter": [  
> > "lowercase",  
> > "snowball",  
> > "XYZSynFilter"  
> > ]  
> > },  
> > "sch\_analyzer": {  
> > "tokenizer": "standard",  
> > "filter": [  
> > "standard",  
> > "lowercase",  
> > "stop"  
> > ]  
> > },  
> > "sch\_comma\_analyzer": {  
> > "tokenizer": "CommaTokenizer",  
> > "filter": [  
> > "standard",  
> > "lowercase",  
> > "stop"  
> > ]  
> > }  
> > },  
> > "filter": {  
> > "XYZSynFilter": {  
> > "type": "synonym",  
> > "synonyms": [  
> > "aids virus, aids, retrovirology, hiv"  
> > ],  
> > "expand": true,  
> > "ignore\_case": true  
> > }  
> > },  
> > "tokenizer": {  
> > "CommaTokenizer": {  
> > "type": "pattern",  
> > "pattern": ","  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "mappings": {  
> > "employees": {  
> > "properties": {  
> > "fullName": {  
> > "type": "string",  
> > "search\_analyzer": "sch\_analyzer"  
> > },  
> > "specialty": {  
> > "type": "string",  
> > "search\_analyzer": "sch\_comma\_analyzer"  
> > }  
> > }  
> > }  
> > }  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
> > {  
> > "fullName": "Don White",  
> > "specialty": "Adult Retrovirology, aids, hiv"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
> > {  
> > "fullName": "Don White",  
> > "specialty": "general practitioner, physician, general, primary care"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
> > {  
> > "fullName": "Don White",  
> > "specialty": "icu, er"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
> > {  
> > "fullName": "Terrance Gartner",  
> > "specialty": "oncology, cancer, research, tumor, polyp"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
> > {  
> > "fullName": "Terrance Gartner",  
> > "specialty": "physician, general, GP, primary care, aids"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
> > {  
> > "fullName": "Terrance Gartner",  
> > "specialty": "emergency care, icu, ambulance, er, urgent"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
> > {  
> > "fullName": "Carter Taylor",  
> > "specialty": "neurosurgery, brain surgery, brain tumor"  
> > }'  
> > curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
> > {  
> > "fullName": "Carter Taylor",  
> > "specialty": "trauma, icu, emergency care, ER, urgent care"  
> > }'
> > 
> > Executing this search (per the example) gives me an error  
> > curl -XGET "[http://localhost:9200/personsearch/employees/\_](http://localhost:9200/personsearch/employees/_)  
> > search?pretty=true" -d'  
> > {  
> > "query": {  
> > "query\_string": {  
> > "query": "icu"  
> > }  
> > },  
> > "aggs": {  
> > "most-rel-profile": {  
> > "terms": {  
> > "field": "profileName",  
> > "order": {  
> > "top\_hit": "desc"  
> > }  
> > },  
> > "aggs": {  
> > "top\_tags\_hits": {  
> > "top\_hits": {}  
> > },  
> > "top\_hit": {  
> > "max": {  
> > "script": "\_doc.score"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }'
> > 
> > {  
> > "error": "SearchPhaseExecutionException[Failed to execute phase  
> > [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]:  
> > SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{\n "query": {\n  
> > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > {\n "script": "\_doc.score"\n }\n }\n  
> > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]:  
> > SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{\n "query": {\n  
> > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > {\n "script": "\_doc.score"\n }\n }\n  
> > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]:  
> > SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{\n "query": {\n  
> > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > {\n "script": "\_doc.score"\n }\n }\n  
> > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]:  
> > SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{\n "query": {\n  
> > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > {\n "script": "\_doc.score"\n }\n }\n  
> > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]:  
> > SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{\n "query": {\n  
> > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > {\n "script": "\_doc.score"\n }\n }\n  
> > }\n }\n }\n}\n]]]; nested:\* ScriptException[dynamic scripting for  
> > [mvel] disabled];\* }]",  
> > "status": 400  
> > }
> > 
> > Does the _dynamic scripting for [mvel] disabled_ have something to do  
> > with it?
> > 
> > ## Thanks.
> > 
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > 
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-)  
> > 78a4-4419-a87d-e39ec3148fcf%[40googlegroups.com](http://40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFhAzKExHojqtOD%3D9R-vZCOjuY8UfQdu0FJ8Z1rUjwN\_A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFhAzKExHojqtOD%3D9R-vZCOjuY8UfQdu0FJ8Z1rUjwN_A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Daniel\_Yim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_yim/32/1367_2.png) [@Daniel\_Yim](https://discuss.elastic.co/u/Daniel_Yim)\
**Post date:** [July 24, 2014, 6:44pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/6 "2014-07-24T18:44:30Z")

</div>

That gives me the following error:

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
all shards failed; shardFailures  
{[mzcfN2MzQjSKlWPHndRROA][personsearch][0]:  
SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n "match":  
{\n "\_all": "icu"\n }\n },\n "aggs": {\n  
"most-rel-agg": {\n "terms": {\n "field":  
"fullName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "most-rel-profile": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "lang" : "expression",\n "script":  
"\_score"\n }\n }\n }\n }\n }\n}\n]]]; nested:  
NoClassDefFoundError[org/antlr/runtime/RecognitionException]; nested:  
ClassNotFoundException[org.antlr.runtime.RecognitionException];  
}{[mzcfN2MzQjSKlWPHndRROA][personsearch][1]:  
SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n "match":  
{\n "\_all": "icu"\n }\n },\n "aggs": {\n  
"most-rel-agg": {\n "terms": {\n "field":  
"fullName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "most-rel-profile": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "lang" : "expression",\n "script":  
"\_score"\n }\n }\n }\n }\n }\n}\n]]]; nested:  
NoClassDefFoundError[org/antlr/runtime/RecognitionException]; nested:  
ClassNotFoundException[org.antlr.runtime.RecognitionException];  
}{[mzcfN2MzQjSKlWPHndRROA][personsearch][2]:  
SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n "match":  
{\n "\_all": "icu"\n }\n },\n "aggs": {\n  
"most-rel-agg": {\n "terms": {\n "field":  
"fullName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "most-rel-profile": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "lang" : "expression",\n "script":  
"\_score"\n }\n }\n }\n }\n }\n}\n]]]; nested:  
NoClassDefFoundError[org/antlr/runtime/RecognitionException]; nested:  
ClassNotFoundException[org.antlr.runtime.RecognitionException];  
}{[mzcfN2MzQjSKlWPHndRROA][personsearch][3]:  
SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n "match":  
{\n "\_all": "icu"\n }\n },\n "aggs": {\n  
"most-rel-agg": {\n "terms": {\n "field":  
"fullName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "most-rel-profile": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "lang" : "expression",\n "script":  
"\_score"\n }\n }\n }\n }\n }\n}\n]]]; nested:  
NoClassDefFoundError[org/antlr/runtime/RecognitionException]; nested:  
ClassNotFoundException[org.antlr.runtime.RecognitionException];  
}{[mzcfN2MzQjSKlWPHndRROA][personsearch][4]:  
SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
Parse Failure [Failed to parse source [{\n "query": {\n "match":  
{\n "\_all": "icu"\n }\n },\n "aggs": {\n  
"most-rel-agg": {\n "terms": {\n "field":  
"fullName",\n "order": {\n "top\_hit": "desc"\n  
}\n },\n "aggs": {\n "most-rel-profile": {\n  
"top\_hits": {}\n },\n "top\_hit": {\n "max":  
{\n "lang" : "expression",\n "script":  
"\_score"\n }\n }\n }\n }\n }\n}\n]]]; nested:  
NoClassDefFoundError[org/antlr/runtime/RecognitionException]; nested:  
ClassNotFoundException[org.antlr.runtime.RecognitionException]; }]",  
"status": 400  
}

On Thursday, July 24, 2014 12:05:17 PM UTC-5, Jörg Prante wrote:

> You can switch to Lucene "expression" language to access score.
> 
> The example in the docs is a bit unfortunate, as it uses MVEL, which has  
> to be enabled in ES 1.3 explicitly.
> 
> Here is a modified example I made using the "expression" language:
> 
> [Top-hits aggregation demo for Elasticsearch 1.3 · GitHub](https://gist.github.com/jprante/7a1763c7073f2b8084a6)
> 
> Jörg
> 
> On Thu, Jul 24, 2014 at 6:22 PM, Daniel Yim \<[dani...@gmail.com](mailto:dani...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Thanks, Rafal. The \*top\_hits \*aggregator is working now, but could you  
> > explain why this is a security risk? We will need to use this feature in  
> > production and now I am feeling uneasy about it.
> > 
> > On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:
> > 
> > > Hello!
> > > 
> > > The error is about scripting and dynamic scripting being disabled for  
> > > MVEL. The simplest way to make it work is turning on dynamic scripting by  
> > > adding script.disable\_dynamic: false to your elasticsearch.yml file.  
> > > However for production it is not recommended for security reasons. You can  
> > > also try using scripting language that is sandboxed and allows for dynamic  
> > > scripting, like groovy.
> > > 
> > > \*-- Regards, Rafał Kuć Performance Monitoring \* Log Analytics \* Search  
> > > Analytics Solr & Elasticsearch Support \* \*[http://sematext.com/](http://sematext.com/)
> > > 
> > > Hello, I am trying to follow the field collapse example  
> > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > on this page  
> > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > , which uses the new 1.3.0 _top\_hits_ aggregator to return the top  
> > > scoring document for a given criteria. To my knowledge, I set up the  
> > > settings and mappings correctly, but I am getting a strange error when I  
> > > try to run the query that the example provided.
> > > 
> > > curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
> > > curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
> > > {  
> > > "settings": {  
> > > "index": {  
> > > "analysis": {  
> > > "analyzer": {  
> > > "idx\_analyzer": {  
> > > "tokenizer": "whitespace",  
> > > "filter": [  
> > > "lowercase",  
> > > "snowball",  
> > > "XYZSynFilter"  
> > > ]  
> > > },  
> > > "sch\_analyzer": {  
> > > "tokenizer": "standard",  
> > > "filter": [  
> > > "standard",  
> > > "lowercase",  
> > > "stop"  
> > > ]  
> > > },  
> > > "sch\_comma\_analyzer": {  
> > > "tokenizer": "CommaTokenizer",  
> > > "filter": [  
> > > "standard",  
> > > "lowercase",  
> > > "stop"  
> > > ]  
> > > }  
> > > },  
> > > "filter": {  
> > > "XYZSynFilter": {  
> > > "type": "synonym",  
> > > "synonyms": [  
> > > "aids virus, aids, retrovirology, hiv"  
> > > ],  
> > > "expand": true,  
> > > "ignore\_case": true  
> > > }  
> > > },  
> > > "tokenizer": {  
> > > "CommaTokenizer": {  
> > > "type": "pattern",  
> > > "pattern": ","  
> > > }  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "mappings": {  
> > > "employees": {  
> > > "properties": {  
> > > "fullName": {  
> > > "type": "string",  
> > > "search\_analyzer": "sch\_analyzer"  
> > > },  
> > > "specialty": {  
> > > "type": "string",  
> > > "search\_analyzer": "sch\_comma\_analyzer"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "Adult Retrovirology, aids, hiv"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "general practitioner, physician, general, primary care"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "icu, er"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "oncology, cancer, research, tumor, polyp"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "physician, general, GP, primary care, aids"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "emergency care, icu, ambulance, er, urgent"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
> > > {  
> > > "fullName": "Carter Taylor",  
> > > "specialty": "neurosurgery, brain surgery, brain tumor"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
> > > {  
> > > "fullName": "Carter Taylor",  
> > > "specialty": "trauma, icu, emergency care, ER, urgent care"  
> > > }'
> > > 
> > > Executing this search (per the example) gives me an error  
> > > curl -XGET "[http://localhost:9200/personsearch/employees/\_](http://localhost:9200/personsearch/employees/_)  
> > > search?pretty=true" -d'  
> > > {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "icu"  
> > > }  
> > > },  
> > > "aggs": {  
> > > "most-rel-profile": {  
> > > "terms": {  
> > > "field": "profileName",  
> > > "order": {  
> > > "top\_hit": "desc"  
> > > }  
> > > },  
> > > "aggs": {  
> > > "top\_tags\_hits": {  
> > > "top\_hits": {}  
> > > },  
> > > "top\_hit": {  
> > > "max": {  
> > > "script": "\_doc.score"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'
> > > 
> > > {  
> > > "error": "SearchPhaseExecutionException[Failed to execute phase  
> > > [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]:  
> > > SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]:  
> > > SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]:  
> > > SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]:  
> > > SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]:  
> > > SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested:\* ScriptException[dynamic scripting for  
> > > [mvel] disabled];\* }]",  
> > > "status": 400  
> > > }
> > > 
> > > Does the _dynamic scripting for [mvel] disabled_ have something to do  
> > > with it?
> > > 
> > > ## Thanks.
> > > 
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-)  
> > > 78a4-4419-a87d-e39ec3148fcf%[40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/acf4f717-359d-461c-8bb8-a65e7f4cfb08%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/acf4f717-359d-461c-8bb8-a65e7f4cfb08%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Daniel\_Yim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_yim/32/1367_2.png) [@Daniel\_Yim](https://discuss.elastic.co/u/Daniel_Yim)\
**Post date:** [July 24, 2014, 6:52pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/7 "2014-07-24T18:52:46Z")

</div>

Also, I was hoping that this example will give me the entire contents of  
the top scoring document, not just its score.

Here is the result I get when running this query:  
[https://gist.github.com/384d88ee0015009033d7.git](https://gist.github.com/384d88ee0015009033d7.git)

{  
"took": 6,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 5,  
"max\_score": 0.3125,  
"hits": [  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "6",  
"\_score": 0.3125,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "emergency care, icu, ambulance, er, urgent"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "3",  
"\_score": 0.2972674,  
"\_source": {  
"fullName": "Don White",  
"specialty": "icu, er"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "8",  
"\_score": 0.18579213,  
"\_source": {  
"fullName": "Carter Taylor",  
"specialty": "trauma, icu, emergency care, ER, urgent care"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "4",  
"\_score": 0.095891505,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "oncology, cancer, research, tumor, polyp, icu"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "5",  
"\_score": 0.095891505,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "physician, general, GP, primary care, aids,  
icu"  
}  
}  
]  
},  
"aggregations": {  
"most-rel-agg": {  
"buckets": [  
{  
"key": "terrance gartner",  
"doc\_count": 3,  
"most-rel-profile": {  
"hits": {  
"total": 3,  
"max\_score": 0.3125,  
"hits": [  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "6",  
"\_score": 0.3125,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "emergency care, icu, ambulance,  
er, urgent"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "4",  
"\_score": 0.095891505,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "oncology, cancer, research,  
tumor, polyp, icu"  
}  
},  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "5",  
"\_score": 0.095891505,  
"\_source": {  
"fullName": "Terrance Gartner",  
"specialty": "physician, general, GP, primary  
care, aids, icu"  
}  
}  
]  
}  
},  
"top\_hit": {  
"value": 0.3125  
}  
},  
{  
"key": "don white",  
"doc\_count": 1,  
"most-rel-profile": {  
"hits": {  
"total": 1,  
"max\_score": 0.2972674,  
"hits": [  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "3",  
"\_score": 0.2972674,  
"\_source": {  
"fullName": "Don White",  
"specialty": "icu, er"  
}  
}  
]  
}  
},  
"top\_hit": {  
"value": 0.2972674071788788  
}  
},  
{  
"key": "carter taylor",  
"doc\_count": 1,  
"most-rel-profile": {  
"hits": {  
"total": 1,  
"max\_score": 0.18579213,  
"hits": [  
{  
"\_index": "personsearch",  
"\_type": "employees",  
"\_id": "8",  
"\_score": 0.18579213,  
"\_source": {  
"fullName": "Carter Taylor",  
"specialty": "trauma, icu, emergency care,  
ER, urgent care"  
}  
}  
]  
}  
},  
"top\_hit": {  
"value": 0.18579213321208954  
}  
}  
]  
}  
}  
}

On Thursday, July 24, 2014 12:05:17 PM UTC-5, Jörg Prante wrote:

> You can switch to Lucene "expression" language to access score.
> 
> The example in the docs is a bit unfortunate, as it uses MVEL, which has  
> to be enabled in ES 1.3 explicitly.
> 
> Here is a modified example I made using the "expression" language:
> 
> [Top-hits aggregation demo for Elasticsearch 1.3 · GitHub](https://gist.github.com/jprante/7a1763c7073f2b8084a6)
> 
> Jörg
> 
> On Thu, Jul 24, 2014 at 6:22 PM, Daniel Yim \<[dani...@gmail.com](mailto:dani...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Thanks, Rafal. The \*top\_hits \*aggregator is working now, but could you  
> > explain why this is a security risk? We will need to use this feature in  
> > production and now I am feeling uneasy about it.
> > 
> > On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:
> > 
> > > Hello!
> > > 
> > > The error is about scripting and dynamic scripting being disabled for  
> > > MVEL. The simplest way to make it work is turning on dynamic scripting by  
> > > adding script.disable\_dynamic: false to your elasticsearch.yml file.  
> > > However for production it is not recommended for security reasons. You can  
> > > also try using scripting language that is sandboxed and allows for dynamic  
> > > scripting, like groovy.
> > > 
> > > \*-- Regards, Rafał Kuć Performance Monitoring \* Log Analytics \* Search  
> > > Analytics Solr & Elasticsearch Support \* \*[http://sematext.com/](http://sematext.com/)
> > > 
> > > Hello, I am trying to follow the field collapse example  
> > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > on this page  
> > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > , which uses the new 1.3.0 _top\_hits_ aggregator to return the top  
> > > scoring document for a given criteria. To my knowledge, I set up the  
> > > settings and mappings correctly, but I am getting a strange error when I  
> > > try to run the query that the example provided.
> > > 
> > > curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
> > > curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
> > > {  
> > > "settings": {  
> > > "index": {  
> > > "analysis": {  
> > > "analyzer": {  
> > > "idx\_analyzer": {  
> > > "tokenizer": "whitespace",  
> > > "filter": [  
> > > "lowercase",  
> > > "snowball",  
> > > "XYZSynFilter"  
> > > ]  
> > > },  
> > > "sch\_analyzer": {  
> > > "tokenizer": "standard",  
> > > "filter": [  
> > > "standard",  
> > > "lowercase",  
> > > "stop"  
> > > ]  
> > > },  
> > > "sch\_comma\_analyzer": {  
> > > "tokenizer": "CommaTokenizer",  
> > > "filter": [  
> > > "standard",  
> > > "lowercase",  
> > > "stop"  
> > > ]  
> > > }  
> > > },  
> > > "filter": {  
> > > "XYZSynFilter": {  
> > > "type": "synonym",  
> > > "synonyms": [  
> > > "aids virus, aids, retrovirology, hiv"  
> > > ],  
> > > "expand": true,  
> > > "ignore\_case": true  
> > > }  
> > > },  
> > > "tokenizer": {  
> > > "CommaTokenizer": {  
> > > "type": "pattern",  
> > > "pattern": ","  
> > > }  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "mappings": {  
> > > "employees": {  
> > > "properties": {  
> > > "fullName": {  
> > > "type": "string",  
> > > "search\_analyzer": "sch\_analyzer"  
> > > },  
> > > "specialty": {  
> > > "type": "string",  
> > > "search\_analyzer": "sch\_comma\_analyzer"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "Adult Retrovirology, aids, hiv"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "general practitioner, physician, general, primary care"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
> > > {  
> > > "fullName": "Don White",  
> > > "specialty": "icu, er"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "oncology, cancer, research, tumor, polyp"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "physician, general, GP, primary care, aids"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
> > > {  
> > > "fullName": "Terrance Gartner",  
> > > "specialty": "emergency care, icu, ambulance, er, urgent"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
> > > {  
> > > "fullName": "Carter Taylor",  
> > > "specialty": "neurosurgery, brain surgery, brain tumor"  
> > > }'  
> > > curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
> > > {  
> > > "fullName": "Carter Taylor",  
> > > "specialty": "trauma, icu, emergency care, ER, urgent care"  
> > > }'
> > > 
> > > Executing this search (per the example) gives me an error  
> > > curl -XGET "[http://localhost:9200/personsearch/employees/\_](http://localhost:9200/personsearch/employees/_)  
> > > search?pretty=true" -d'  
> > > {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "icu"  
> > > }  
> > > },  
> > > "aggs": {  
> > > "most-rel-profile": {  
> > > "terms": {  
> > > "field": "profileName",  
> > > "order": {  
> > > "top\_hit": "desc"  
> > > }  
> > > },  
> > > "aggs": {  
> > > "top\_tags\_hits": {  
> > > "top\_hits": {}  
> > > },  
> > > "top\_hit": {  
> > > "max": {  
> > > "script": "\_doc.score"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'
> > > 
> > > {  
> > > "error": "SearchPhaseExecutionException[Failed to execute phase  
> > > [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][personsearch][0]:  
> > > SearchParseException[[personsearch][0]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]:  
> > > SearchParseException[[personsearch][1]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]:  
> > > SearchParseException[[personsearch][2]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]:  
> > > SearchParseException[[personsearch][3]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for  
> > > [mvel] disabled]; }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]:  
> > > SearchParseException[[personsearch][4]: query[\_all:icu],from[-1],size[-1]:  
> > > Parse Failure [Failed to parse source [{\n "query": {\n  
> > > "query\_string": {\n "query": "icu"\n }\n },\n "aggs":  
> > > {\n "most-rel-profile": {\n "terms": {\n "field":  
> > > "profileName",\n "order": {\n "top\_hit": "desc"\n  
> > > }\n },\n "aggs": {\n "top\_tags\_hits": {\n  
> > > "top\_hits": {}\n },\n "top\_hit": {\n "max":  
> > > {\n "script": "\_doc.score"\n }\n }\n  
> > > }\n }\n }\n}\n]]]; nested:\* ScriptException[dynamic scripting for  
> > > [mvel] disabled];\* }]",  
> > > "status": 400  
> > > }
> > > 
> > > Does the _dynamic scripting for [mvel] disabled_ have something to do  
> > > with it?
> > > 
> > > ## Thanks.
> > > 
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-)  
> > > 78a4-4419-a87d-e39ec3148fcf%[40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [July 24, 2014, 8:45pm UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/8 "2014-07-24T20:45:48Z")

</div>

The ANTLR class error is not related , I have no ANTLR here, it must be an  
erro at your side.

Not sure where you miss the entire content - I see the aggregated docs with  
all the \_source under "most-rel-profile", ordered by score descending.

Jörg

On Thu, Jul 24, 2014 at 8:52 PM, Daniel Yim [danielyim@gmail.com](mailto:danielyim@gmail.com) wrote:

> Also, I was hoping that this example will give me the entire contents of  
> the top scoring document, not just its score.
> 
> Here is the result I get when running this query:  
> [https://gist.github.com/384d88ee0015009033d7.git](https://gist.github.com/384d88ee0015009033d7.git)
> 
> {  
> "took": 6,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 5,  
> "max\_score": 0.3125,  
> "hits": [  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "6",  
> "\_score": 0.3125,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "emergency care, icu, ambulance, er, urgent"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "3",  
> "\_score": 0.2972674,  
> "\_source": {  
> "fullName": "Don White",  
> "specialty": "icu, er"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "8",  
> "\_score": 0.18579213,  
> "\_source": {  
> "fullName": "Carter Taylor",  
> "specialty": "trauma, icu, emergency care, ER, urgent care"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "4",  
> "\_score": 0.095891505,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "oncology, cancer, research, tumor, polyp, icu"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "5",  
> "\_score": 0.095891505,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "physician, general, GP, primary care, aids,  
> icu"  
> }  
> }  
> ]  
> },  
> "aggregations": {  
> "most-rel-agg": {  
> "buckets": [  
> {  
> "key": "terrance gartner",  
> "doc\_count": 3,  
> "most-rel-profile": {  
> "hits": {  
> "total": 3,  
> "max\_score": 0.3125,  
> "hits": [  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "6",  
> "\_score": 0.3125,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "emergency care, icu,  
> ambulance, er, urgent"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "4",  
> "\_score": 0.095891505,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "oncology, cancer, research,  
> tumor, polyp, icu"  
> }  
> },  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "5",  
> "\_score": 0.095891505,  
> "\_source": {  
> "fullName": "Terrance Gartner",  
> "specialty": "physician, general, GP,  
> primary care, aids, icu"  
> }  
> }  
> ]  
> }  
> },  
> "top\_hit": {  
> "value": 0.3125  
> }  
> },  
> {  
> "key": "don white",  
> "doc\_count": 1,  
> "most-rel-profile": {  
> "hits": {  
> "total": 1,  
> "max\_score": 0.2972674,  
> "hits": [  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "3",  
> "\_score": 0.2972674,  
> "\_source": {  
> "fullName": "Don White",  
> "specialty": "icu, er"  
> }  
> }  
> ]  
> }  
> },  
> "top\_hit": {  
> "value": 0.2972674071788788  
> }  
> },  
> {  
> "key": "carter taylor",  
> "doc\_count": 1,  
> "most-rel-profile": {  
> "hits": {  
> "total": 1,  
> "max\_score": 0.18579213,  
> "hits": [  
> {  
> "\_index": "personsearch",  
> "\_type": "employees",  
> "\_id": "8",  
> "\_score": 0.18579213,  
> "\_source": {  
> "fullName": "Carter Taylor",  
> "specialty": "trauma, icu, emergency care,  
> ER, urgent care"  
> }  
> }  
> ]  
> }  
> },  
> "top\_hit": {  
> "value": 0.18579213321208954  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> On Thursday, July 24, 2014 12:05:17 PM UTC-5, Jörg Prante wrote:
> 
> > You can switch to Lucene "expression" language to access score.
> > 
> > The example in the docs is a bit unfortunate, as it uses MVEL, which has  
> > to be enabled in ES 1.3 explicitly.
> > 
> > Here is a modified example I made using the "expression" language:
> > 
> > [Top-hits aggregation demo for Elasticsearch 1.3 · GitHub](https://gist.github.com/jprante/7a1763c7073f2b8084a6)
> > 
> > Jörg
> > 
> > On Thu, Jul 24, 2014 at 6:22 PM, Daniel Yim [dani...@gmail.com](mailto:dani...@gmail.com) wrote:
> > 
> > > Thanks, Rafal. The \*top\_hits \*aggregator is working now, but could you  
> > > explain why this is a security risk? We will need to use this feature in  
> > > production and now I am feeling uneasy about it.
> > > 
> > > On Thursday, July 24, 2014 10:28:32 AM UTC-5, Rafał Kuć wrote:
> > > 
> > > > Hello!
> > > > 
> > > > The error is about scripting and dynamic scripting being disabled for  
> > > > MVEL. The simplest way to make it work is turning on dynamic scripting by  
> > > > adding script.disable\_dynamic: false to your elasticsearch.yml file.  
> > > > However for production it is not recommended for security reasons. You can  
> > > > also try using scripting language that is sandboxed and allows for dynamic  
> > > > scripting, like groovy.
> > > > 
> > > > \*-- Regards, Rafał Kuć Performance Monitoring \* Log Analytics \* Search  
> > > > Analytics Solr & Elasticsearch Support \* \*[http://sematext.com/](http://sematext.com/)
> > > > 
> > > > Hello, I am trying to follow the field collapse example  
> > > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > > on this page  
> > > > [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#\_field\_collapse\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#_field_collapse_example)  
> > > > , which uses the new 1.3.0 _top\_hits_ aggregator to return the top  
> > > > scoring document for a given criteria. To my knowledge, I set up the  
> > > > settings and mappings correctly, but I am getting a strange error when I  
> > > > try to run the query that the example provided.
> > > > 
> > > > curl -XDELETE "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)"  
> > > > curl -XPUT "[http://localhost:9200/personsearch](http://localhost:9200/personsearch)" -d'  
> > > > {  
> > > > "settings": {  
> > > > "index": {  
> > > > "analysis": {  
> > > > "analyzer": {  
> > > > "idx\_analyzer": {  
> > > > "tokenizer": "whitespace",  
> > > > "filter": [  
> > > > "lowercase",  
> > > > "snowball",  
> > > > "XYZSynFilter"  
> > > > ]  
> > > > },  
> > > > "sch\_analyzer": {  
> > > > "tokenizer": "standard",  
> > > > "filter": [  
> > > > "standard",  
> > > > "lowercase",  
> > > > "stop"  
> > > > ]  
> > > > },  
> > > > "sch\_comma\_analyzer": {  
> > > > "tokenizer": "CommaTokenizer",  
> > > > "filter": [  
> > > > "standard",  
> > > > "lowercase",  
> > > > "stop"  
> > > > ]  
> > > > }  
> > > > },  
> > > > "filter": {  
> > > > "XYZSynFilter": {  
> > > > "type": "synonym",  
> > > > "synonyms": [  
> > > > "aids virus, aids, retrovirology, hiv"  
> > > > ],  
> > > > "expand": true,  
> > > > "ignore\_case": true  
> > > > }  
> > > > },  
> > > > "tokenizer": {  
> > > > "CommaTokenizer": {  
> > > > "type": "pattern",  
> > > > "pattern": ","  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "mappings": {  
> > > > "employees": {  
> > > > "properties": {  
> > > > "fullName": {  
> > > > "type": "string",  
> > > > "search\_analyzer": "sch\_analyzer"  
> > > > },  
> > > > "specialty": {  
> > > > "type": "string",  
> > > > "search\_analyzer": "sch\_comma\_analyzer"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/1](http://localhost:9200/personsearch/employees/1)" -d'  
> > > > {  
> > > > "fullName": "Don White",  
> > > > "specialty": "Adult Retrovirology, aids, hiv"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/2](http://localhost:9200/personsearch/employees/2)" -d'  
> > > > {  
> > > > "fullName": "Don White",  
> > > > "specialty": "general practitioner, physician, general, primary care"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/3](http://localhost:9200/personsearch/employees/3)" -d'  
> > > > {  
> > > > "fullName": "Don White",  
> > > > "specialty": "icu, er"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/4](http://localhost:9200/personsearch/employees/4)" -d'  
> > > > {  
> > > > "fullName": "Terrance Gartner",  
> > > > "specialty": "oncology, cancer, research, tumor, polyp"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/5](http://localhost:9200/personsearch/employees/5)" -d'  
> > > > {  
> > > > "fullName": "Terrance Gartner",  
> > > > "specialty": "physician, general, GP, primary care, aids"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/6](http://localhost:9200/personsearch/employees/6)" -d'  
> > > > {  
> > > > "fullName": "Terrance Gartner",  
> > > > "specialty": "emergency care, icu, ambulance, er, urgent"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/7](http://localhost:9200/personsearch/employees/7)" -d'  
> > > > {  
> > > > "fullName": "Carter Taylor",  
> > > > "specialty": "neurosurgery, brain surgery, brain tumor"  
> > > > }'  
> > > > curl -XPUT "[http://localhost:9200/personsearch/employees/8](http://localhost:9200/personsearch/employees/8)" -d'  
> > > > {  
> > > > "fullName": "Carter Taylor",  
> > > > "specialty": "trauma, icu, emergency care, ER, urgent care"  
> > > > }'
> > > > 
> > > > Executing this search (per the example) gives me an error  
> > > > curl -XGET "[http://localhost:9200/personsearch/employees/\_search](http://localhost:9200/personsearch/employees/_search)?  
> > > > pretty=true" -d'  
> > > > {  
> > > > "query": {  
> > > > "query\_string": {  
> > > > "query": "icu"  
> > > > }  
> > > > },  
> > > > "aggs": {  
> > > > "most-rel-profile": {  
> > > > "terms": {  
> > > > "field": "profileName",  
> > > > "order": {  
> > > > "top\_hit": "desc"  
> > > > }  
> > > > },  
> > > > "aggs": {  
> > > > "top\_tags\_hits": {  
> > > > "top\_hits": {}  
> > > > },  
> > > > "top\_hit": {  
> > > > "max": {  
> > > > "script": "\_doc.score"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }'
> > > > 
> > > > {  
> > > > "error": "SearchPhaseExecutionException[Failed to execute phase  
> > > > [query], all shards failed; shardFailures {[4gtV\_OJESWWSgMnbTruKyA][  
> > > > personsearch][0]: SearchParseException[[personsearch][0]:  
> > > > query[\_all:icu],from[-1],size[-1]: Parse Failure [Failed to parse  
> > > > source [{\n "query": {\n "query\_string": {\n "query":  
> > > > "icu"\n }\n },\n "aggs": {\n "most-rel-profile": {\n  
> > > > "terms": {\n "field": "profileName",\n "order": {\n  
> > > > "top\_hit": "desc"\n }\n },\n "aggs": {\n  
> > > > "top\_tags\_hits": {\n "top\_hits": {}\n },\n  
> > > > "top\_hit": {\n "max": {\n "script":  
> > > > "\_doc.score"\n }\n }\n }\n }\n }\n}\n]]];  
> > > > nested: ScriptException[dynamic scripting for [mvel] disabled];  
> > > > }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][1]: SearchParseException[[  
> > > > personsearch][1]: query[\_all:icu],from[-1],size[-1]: Parse Failure  
> > > > [Failed to parse source [{\n "query": {\n "query\_string": {\n  
> > > > "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile":  
> > > > {\n "terms": {\n "field": "profileName",\n  
> > > > "order": {\n "top\_hit": "desc"\n }\n },\n  
> > > > "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n  
> > > > },\n "top\_hit": {\n "max": {\n  
> > > > "script": "\_doc.score"\n }\n }\n }\n }\n  
> > > > }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled];  
> > > > }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][2]: SearchParseException[[  
> > > > personsearch][2]: query[\_all:icu],from[-1],size[-1]: Parse Failure  
> > > > [Failed to parse source [{\n "query": {\n "query\_string": {\n  
> > > > "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile":  
> > > > {\n "terms": {\n "field": "profileName",\n  
> > > > "order": {\n "top\_hit": "desc"\n }\n },\n  
> > > > "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n  
> > > > },\n "top\_hit": {\n "max": {\n  
> > > > "script": "\_doc.score"\n }\n }\n }\n }\n  
> > > > }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled];  
> > > > }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][3]: SearchParseException[[  
> > > > personsearch][3]: query[\_all:icu],from[-1],size[-1]: Parse Failure  
> > > > [Failed to parse source [{\n "query": {\n "query\_string": {\n  
> > > > "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile":  
> > > > {\n "terms": {\n "field": "profileName",\n  
> > > > "order": {\n "top\_hit": "desc"\n }\n },\n  
> > > > "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n  
> > > > },\n "top\_hit": {\n "max": {\n  
> > > > "script": "\_doc.score"\n }\n }\n }\n }\n  
> > > > }\n}\n]]]; nested: ScriptException[dynamic scripting for [mvel] disabled];  
> > > > }{[4gtV\_OJESWWSgMnbTruKyA][personsearch][4]: SearchParseException[[  
> > > > personsearch][4]: query[\_all:icu],from[-1],size[-1]: Parse Failure  
> > > > [Failed to parse source [{\n "query": {\n "query\_string": {\n  
> > > > "query": "icu"\n }\n },\n "aggs": {\n "most-rel-profile":  
> > > > {\n "terms": {\n "field": "profileName",\n  
> > > > "order": {\n "top\_hit": "desc"\n }\n },\n  
> > > > "aggs": {\n "top\_tags\_hits": {\n "top\_hits": {}\n  
> > > > },\n "top\_hit": {\n "max": {\n  
> > > > "script": "\_doc.score"\n }\n }\n }\n }\n  
> > > > }\n}\n]]]; nested:\* ScriptException[dynamic scripting for [mvel]  
> > > > disabled];\* }]",  
> > > > "status": 400  
> > > > }
> > > > 
> > > > Does the _dynamic scripting for [mvel] disabled_ have something to do  
> > > > with it?
> > > > 
> > > > ## Thanks.
> > > > 
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > > 
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a)  
> > > > 4-4419-a87d-e39ec3148fcf%[40googlegroups.com](http://40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c1fff8-78a4-4419-a87d-e39ec3148fcf%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/43346d99-6385-4a24-bd8e-8e7482f39a36%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7675dfb7-66e4-4626-9fa3-01ec4a5494bb%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoG86y0Hm0kJHdH-8qxHKjWF%2B0p%3DP7R554t5ZN98giBJGg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoG86y0Hm0kJHdH-8qxHKjWF%2B0p%3DP7R554t5ZN98giBJGg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:13am UTC](https://discuss.elastic.co/t/top-hits-aggregator-from-example-not-working-for-me/18869/9 "2017-07-06T01:13:20Z")

</div>


