# Sorting based on a dynamic field array?

**URL:** <https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573>\
**Category:** Elasticsearch\
**Created:** [January 31, 2013, 4:58pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573 "2013-01-31T16:58:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [January 31, 2013, 4:58pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573/1 "2013-01-31T16:58:28Z")

</div>

Hi all,

I'm trying to sort based on a dynamic field (it's an array of values).

The field is present in the mapping:

Request to :9200/items/item/\_mapping:

{

- item:  
{
  - properties:  
{
    - ...,  
_..._,
    - selects:  
{
      - dynamic: "true",
      - properties:  
{
        - community\_id:  
{
          - type: "string"  
},

        - num\_selects:  
{
          - type: "long"  
}  
}  
},

I then go to create a search query which sorts by the number of values in  
this field (e.g. size of selects) and thought i could do:

customScoreQuery(matchAllQuery())).script("doc['selects'].values.length"));  
//not sure if length should be size?

Whether i use length or size, i get:

[Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No  
field found for [selects] in mapping with types [item]];  
}{[2Qze4mSDQiuf16o3GxVP1Q][items][0]:  
QueryPhaseExecutionException[[items][0]: query[filtered(custom score  
(application\_id:1,function=script[doc['selects'].values.size], params  
[null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested: CompileException[[Error: No field found for  
[selects] in mapping with types [item]]  
[Near : {... doc['selects'].values.size ....}]

I thought the issue may be because not all the 'selects' may have values?  
But the error seems to suggest that the mapping has an issue?

I'm running ES 0.20.4; there is currently ~630,000 docs that the  
matchAllQuery() returns.

Any help greatly appreciated.

Derry

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [January 31, 2013, 5:07pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573/2 "2013-01-31T17:07:40Z")

</div>

I've tested this on another field (string array) which is not dynamic and  
it works fine....

On Thursday, 31 January 2013 16:58:28 UTC, Derry O' Sullivan wrote:

> Hi all,
> 
> I'm trying to sort based on a dynamic field (it's an array of values).
> 
> The field is present in the mapping:
> 
> Request to :9200/items/item/\_mapping:
> 
> {
> 
> - item:  
> {
> - properties:  
> {
> - ...,  
> _..._,
> - selects:  
> {
> - dynamic: "true",
> - properties:  
> {
> - community\_id:  
> {
> - type: "string"  
> },
> 
> - num\_selects:  
> {
> - type: "long"  
> }  
> }  
> },
> 
> I then go to create a search query which sorts by the number of values in  
> this field (e.g. size of selects) and thought i could do:
> 
> customScoreQuery(matchAllQuery())).script("doc['selects'].values.length"));  
> //not sure if length should be size?
> 
> Whether i use length or size, i get:
> 
> [Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No  
> field found for [selects] in mapping with types [item]];  
> }{[2Qze4mSDQiuf16o3GxVP1Q][items][0]:  
> QueryPhaseExecutionException[[items][0]: query[filtered(custom score  
> (application\_id:1,function=script[doc['selects'].values.size], params  
> [null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested: CompileException[[Error: No field found for  
> [selects] in mapping with types [item]]  
> [Near : {... doc['selects'].values.size ....}]
> 
> I thought the issue may be because not all the 'selects' may have values?  
> But the error seems to suggest that the mapping has an issue?
> 
> I'm running ES 0.20.4; there is currently ~630,000 docs that the  
> matchAllQuery() returns.
> 
> Any help greatly appreciated.
> 
> Derry

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [February 1, 2013, 12:39pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573/3 "2013-02-01T12:39:46Z")

</div>

I've also tried to do this using a custom scoring script:

$ curl -XGET '[http://localhost:9200/items/item/\_search](http://localhost:9200/items/item/_search)' -d '{  
"query" :{  
"custom\_score": {  
"query" : { "match\_all": {}},  
"script" : "selectsscorescript",  
"lang": "native"  
}  
}  
}'  
% Total % Received % Xferd Average Speed Time Time Time  
Current  
Dload Upload Total Spent Left  
Speed  
100 1033 100 882 100 151 55125 9437 --:--:-- --:--:-- --:--:--  
861k{"error":"SearchPhaseExecutionException[Failed to execute phase  
[query], total failure; shardFailures {[4OYazvnmR7CZXJVNET\_ncA][items][4]:  
QueryPhaseExecutionException[[items][4]: query[filtered(custom score  
(ConstantScore(NotDeleted(_:_)),function=script[selectsscorescript], params  
[null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested: ElasticSearchIllegalArgumentException[No  
field found for [selects] in mapping with types [item]];  
}{[4OYazvnmR7CZXJVNET\_ncA][items][3]:  
QueryPhaseExecutionException[[items][3]: query[filtered(custom score  
(ConstantScore(NotDeleted(_:_)),function=script[selectsscorescript], params  
[null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested: ElasticSearchIllegalArgumentException[No  
field found for [selects] in mapping with types [item]]; }]","status":500}

Could someone please comment on this? I'm really confused by the fact that  
is is saying there is no field found when it is clearly there....

On Thursday, 31 January 2013 17:07:40 UTC, Derry O' Sullivan wrote:

> I've tested this on another field (string array) which is not dynamic and  
> it works fine....
> 
> On Thursday, 31 January 2013 16:58:28 UTC, Derry O' Sullivan wrote:
> 
> > Hi all,
> > 
> > I'm trying to sort based on a dynamic field (it's an array of values).
> > 
> > The field is present in the mapping:
> > 
> > Request to :9200/items/item/\_mapping:
> > 
> > {
> > 
> > - item:  
> > {
> > - properties:  
> > {
> > - ...,  
> > _..._,
> > - selects:  
> > {
> > - dynamic: "true",
> > - properties:  
> > {
> > - community\_id:  
> > {
> > - type: "string"  
> > },
> > 
> > - num\_selects:  
> > {
> > - type: "long"  
> > }  
> > }  
> > },
> > 
> > I then go to create a search query which sorts by the number of values in  
> > this field (e.g. size of selects) and thought i could do:
> > 
> > customScoreQuery(matchAllQuery())).script("doc['selects'].values.length"));  
> > //not sure if length should be size?
> > 
> > Whether i use length or size, i get:
> > 
> > [Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No  
> > field found for [selects] in mapping with types [item]];  
> > }{[2Qze4mSDQiuf16o3GxVP1Q][items][0]:  
> > QueryPhaseExecutionException[[items][0]: query[filtered(custom score  
> > (application\_id:1,function=script[doc['selects'].values.size], params  
> > [null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
> > execute main query]]; nested: CompileException[[Error: No field found for  
> > [selects] in mapping with types [item]]  
> > [Near : {... doc['selects'].values.size ....}]
> > 
> > I thought the issue may be because not all the 'selects' may have values?  
> > But the error seems to suggest that the mapping has an issue?
> > 
> > I'm running ES 0.20.4; there is currently ~630,000 docs that the  
> > matchAllQuery() returns.
> > 
> > Any help greatly appreciated.
> > 
> > Derry

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [February 1, 2013, 12:58pm UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573/4 "2013-02-01T12:58:14Z")

</div>

Discovered the answer for anyone wondering

You have to do a .containsKey() check if you have a potential  
null field in a doc.

Also from the script expression doc (linked  
here): [https://groups.google.com/d/msg/elasticsearch/P0DG9j6hWKY/wGLpg5d9bswJ](https://groups.google.com/d/msg/elasticsearch/P0DG9j6hWKY/wGLpg5d9bswJ)  
you can only use doc() on simple fields!

On Friday, 1 February 2013 12:39:46 UTC, Derry O' Sullivan wrote:

> I've also tried to do this using a custom scoring script:
> 
> $ curl -XGET '[http://localhost:9200/items/item/\_search](http://localhost:9200/items/item/_search)' -d '{  
> "query" :{  
> "custom\_score": {  
> "query" : { "match\_all": {}},  
> "script" : "selectsscorescript",  
> "lang": "native"  
> }  
> }  
> }'  
> % Total % Received % Xferd Average Speed Time Time Time  
> Current  
> Dload Upload Total Spent Left  
> Speed  
> 100 1033 100 882 100 151 55125 9437 --:--:-- --:--:-- --:--:--  
> 861k{"error":"SearchPhaseExecutionException[Failed to execute phase  
> [query], total failure; shardFailures {[4OYazvnmR7CZXJVNET\_ncA][items][4]:  
> QueryPhaseExecutionException[[items][4]: query[filtered(custom score  
> (ConstantScore(NotDeleted(_:_)),function=script[selectsscorescript], params  
> [null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested: ElasticSearchIllegalArgumentException[No  
> field found for [selects] in mapping with types [item]];  
> }{[4OYazvnmR7CZXJVNET\_ncA][items][3]:  
> QueryPhaseExecutionException[[items][3]: query[filtered(custom score  
> (ConstantScore(NotDeleted(_:_)),function=script[selectsscorescript], params  
> [null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested: ElasticSearchIllegalArgumentException[No  
> field found for [selects] in mapping with types [item]]; }]","status":500}
> 
> Could someone please comment on this? I'm really confused by the fact that  
> is is saying there is no field found when it is clearly there....
> 
> On Thursday, 31 January 2013 17:07:40 UTC, Derry O' Sullivan wrote:
> 
> > I've tested this on another field (string array) which is not dynamic and  
> > it works fine....
> > 
> > On Thursday, 31 January 2013 16:58:28 UTC, Derry O' Sullivan wrote:
> > 
> > > Hi all,
> > > 
> > > I'm trying to sort based on a dynamic field (it's an array of values).
> > > 
> > > The field is present in the mapping:
> > > 
> > > Request to :9200/items/item/\_mapping:
> > > 
> > > {
> > > 
> > > - item:  
> > > {
> > > - properties:  
> > > {
> > > - ...,  
> > > _..._,
> > > - selects:  
> > > {
> > > - dynamic: "true",
> > > - properties:  
> > > {
> > > - community\_id:  
> > > {
> > > - type: "string"  
> > > },
> > > 
> > > - num\_selects:  
> > > {
> > > - type: "long"  
> > > }  
> > > }  
> > > },
> > > 
> > > I then go to create a search query which sorts by the number of values  
> > > in this field (e.g. size of selects) and thought i could do:
> > > 
> > > customScoreQuery(matchAllQuery())).script("doc['selects'].values.length"));  
> > > //not sure if length should be size?
> > > 
> > > Whether i use length or size, i get:
> > > 
> > > [Line: 1, Column: 1]]; nested: ElasticSearchIllegalArgumentException[No  
> > > field found for [selects] in mapping with types [item]];  
> > > }{[2Qze4mSDQiuf16o3GxVP1Q][items][0]:  
> > > QueryPhaseExecutionException[[items][0]: query[filtered(custom score  
> > > (application\_id:1,function=script[doc['selects'].values.size], params  
> > > [null]))-\>cache(\_type:item)],from[0],size[10]: Query Failed [Failed to  
> > > execute main query]]; nested: CompileException[[Error: No field found for  
> > > [selects] in mapping with types [item]]  
> > > [Near : {... doc['selects'].values.size ....}]
> > > 
> > > I thought the issue may be because not all the 'selects' may have  
> > > values? But the error seems to suggest that the mapping has an issue?
> > > 
> > > I'm running ES 0.20.4; there is currently ~630,000 docs that the  
> > > matchAllQuery() returns.
> > > 
> > > Any help greatly appreciated.
> > > 
> > > Derry

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:53am UTC](https://discuss.elastic.co/t/sorting-based-on-a-dynamic-field-array/10573/5 "2017-07-06T02:53:35Z")

</div>


