"query must be rewritten first" error when using terms lookup inside dis_max query

During some experimentation I got this error when trying to use the terms lookup query inside a dis_max query as follows :

  "dis_max" : {
    "tie_breaker" : 0.7,
    "queries" : [
        "terms" : {
          "user_id" : {
            "index" : "contacts_read",
            "type" : "contact",
            "id" : "2846ff2d-7571-449d-84b5-d7f8b65d9c33",
            "path" : "knows"
          "boost" : 1.0
    "boost" : 1.2

when executing this query I obtain the following stack trace

Caused by: java.lang.UnsupportedOperationException: query must be rewritten first
	at org.elasticsearch.index.query.TermsQueryBuilder.doToQuery(TermsQueryBuilder.java:414) ~[elasticsearch-6.0.0-rc2.jar:6.0.0-rc2]
	at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:98) ~[elasticsearch-6.0.0-rc2.jar:6.0.0-rc2]
	at org.elasticsearch.index.query.AbstractQueryBuilder.toQueries(AbstractQueryBuilder.java:233) ~[elasticsearch-6.0.0-rc2.jar:6.0.0-rc2]
	at org.elasticsearch.index.query.DisMaxQueryBuilder.doToQuery(DisMaxQueryBuilder.java:185) ~[elasticsearch-6.0.0-rc2.jar:6.0.0-rc2]

The same errors occurs when wrapping the filter query inside a bool query (either in a filter context or a query context)

It happens in both elasticsearch 5 and 6

Not sure if it's related but found some other user experiencing similar issue with the dis_max here : https://stackoverflow.com/questions/43741543/cannot-access-indexed-shapes-in-dis-max-query-elasticsearch

Hope some folk has a clue :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.