Scripted nested facet query cannot access _source.doc

Hello,

I am running into a problem with a scripted nested facet query
accessing document through _source. The query used to work just fine
with non-nested mapping, but with nested mapping it is throwing an
error. I suspect this could be an error how I construct the scripted
nested facet query (in step 6 below).

Sorry for the long post, but just in case it is an ES issue I am
attaching full repro steps:

  1. Install ES 0.19.2

  2. Create index named h

  3. Put nested mapping (as h/doc/_mapping )
    {
    "doc": {
    "properties": {
    "title": {
    "include_in_all": "false",
    "index": "not_analyzed",
    "type": "string"
    },
    "chapter": {
    "properties": {
    "tags": {
    "include_in_all": "false",
    "index": "not_analyzed",
    "type": "string"
    },
    "paragraph": {
    "properties": {
    "keywords": {
    "include_in_all": "false",
    "index": "not_analyzed",
    "type": "string"
    }
    },
    "type": "nested"
    }
    }
    }
    }
    }
    }
    (gist here: https://gist.github.com/2950831)

  4. Insert document 1: (PUT into h/doc/1)
    {
    "doc": {
    "title": "title1",
    "chapter": {
    "tags": [
    "tag1",
    "tag3"
    ],
    "paragraph": [
    {
    "keywords": "keyword1"
    },
    {
    "keywords": "keyword2"
    },
    {
    "keywords": "keyword1"
    }
    ]
    }
    }
    }
    (gist here https://gist.github.com/2950843)

  5. Insert document 2: (PUT into h/doc/2)
    {
    "doc": {
    "title": "title1",
    "chapter": {
    "tags": [
    "tag1",
    "tag3"
    ],
    "paragraph": [
    {
    "keywords": "keyword3"
    },
    {
    "keywords": "keyword2"
    },
    {
    "keywords": "keyword2"
    }
    ]
    }
    }
    }

(gist here: https://gist.github.com/2950854)

  1. Issue regular nested facet query (POST into h/_search) , observe
    correct results returned:
    {
    "query": {
    "match_all": {}
    },
    "facets": {
    "chapter.paragraph.keywords": {
    "terms": {
    "field": "chapter.paragraph.keywords",
    "size": 50
    },
    "nested": "chapter.paragraph"
    }
    },
    "size": 0
    }
    (gist here: https://gist.github.com/2950880)

  2. Issue scripted nested facet query (POST into h/_search)
    {
    "query": {
    "match_all": {}
    },
    "facets": {
    "chapter.paragraph.keywords": {
    "terms": {
    "script_field": "_source.doc.chapter.paragraph.keywords",
    "size": 50
    },
    "nested": "chapter.paragraph"
    }
    },
    "size": 0
    }
    (gist here: https://gist.github.com/2950955)

  3. BUG => Observe the following error returned :

QueryPhaseExecutionException[[h][2]:
query[ConstantScore(NotDeleted(cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@a2a5984b)))],from[0],size[0]:
Query Failed [Failed to execute main query]]; nested:
PropertyAccessException[[Error: could not access: doc; in class:
org.elasticsearch.search.lookup.SourceLookup] [Near : {...
_source.doc.chapter.paragraph. ....}] ^ [Line: 1, Column: 1]];

Stack trace from ES log:

Caused by: [Error: could not access: doc; in class:
org.elasticsearch.search.lookup.SourceLookup]
[Near : {... _source.doc.chapter.paragraph. ....}]
^
[Line: 1, Column: 1]
at
org.elasticsearch.common.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getBeanProperty(ReflectiveAccessorOptimizer.java:
694)
at
org.elasticsearch.common.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:
340)
at
org.elasticsearch.common.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:
142)
at
org.elasticsearch.common.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:
67)
at
org.elasticsearch.common.mvel2.ast.ASTNode.optimize(ASTNode.java:157)
at
org.elasticsearch.common.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:
113)
at
org.elasticsearch.common.mvel2.MVELRuntime.execute(MVELRuntime.java:
85)
at
org.elasticsearch.common.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:
123)
at org.elasticsearch.script.mvel.MvelScriptEngineService
$MvelSearchScript.run(MvelScriptEngineService.java:192)
at
org.elasticsearch.search.facet.terms.strings.ScriptTermsStringFieldFacetCollector.doCollect(ScriptTermsStringFieldFacetCollector.java:
91)
at
org.elasticsearch.search.facet.AbstractFacetCollector.collect(AbstractFacetCollector.java:
83)
at
org.elasticsearch.index.search.nested.NestedChildrenCollector.collect(NestedChildrenCollector.java:
94)
at
org.elasticsearch.common.lucene.MultiCollector.collect(MultiCollector.java:
59)
at org.apache.lucene.search.Scorer.score(Scorer.java:90)
at org.apache.lucene.search.ConstantScoreQuery
$ConstantScorer.score(ConstantScoreQuery.java:241)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:581)
at
org.elasticsearch.search.internal.ContextIndexSearcher.search(ContextIndexSearcher.java:
195)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:445)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:426)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:342)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:330)
at
org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:194)
... 9 more

Thanks,

-- Andy