# Scripted nested facet query cannot access \_source.doc

**URL:** <https://discuss.elastic.co/t/scripted-nested-facet-query-cannot-access--source-doc/8147>\
**Category:** Elasticsearch\
**Created:** [June 18, 2012, 10:10pm UTC](https://discuss.elastic.co/t/scripted-nested-facet-query-cannot-access--source-doc/8147 "2012-06-18T22:10:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andym](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@andym](https://discuss.elastic.co/u/andym)\
**Post date:** [June 18, 2012, 10:10pm UTC](https://discuss.elastic.co/t/scripted-nested-facet-query-cannot-access--source-doc/8147/1 "2012-06-18T22:10:50Z")

</div>

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](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](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](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](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](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

---

<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, 3:23am UTC](https://discuss.elastic.co/t/scripted-nested-facet-query-cannot-access--source-doc/8147/2 "2017-07-06T03:23:33Z")

</div>


