# How to script sort on a not\_analyzed field containing an array in ES 0.20.6

**URL:** https://discuss.elastic.co/t/how-to-script-sort-on-a-not-analyzed-field-containing-an-array-in-es-0-20-6/12653
**Category:** Elasticsearch
**Created:** [July 4, 2013, 12:23am UTC](https://discuss.elastic.co/t/how-to-script-sort-on-a-not-analyzed-field-containing-an-array-in-es-0-20-6/12653 "2013-07-04T00:23:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Brian\_Phunk\_Gadoury](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@Brian\_Phunk\_Gadoury](https://discuss.elastic.co/u/Brian_Phunk_Gadoury)
#### Post date: [July 4, 2013, 12:23am UTC](https://discuss.elastic.co/t/how-to-script-sort-on-a-not-analyzed-field-containing-an-array-in-es-0-20-6/12653/1 "2013-07-04T00:23:10Z")

</div>

My attempts to use a script to sort on a field containing an array of  
not\_analyzed strings does not behave as I'd expect. Elasticsearch sorts on  
the highest-sorted value in the array, rather than the array values in  
their original order. Is that expected behavior?

The \_mapping for my title field. I don't think the multi\_field aspect is  
relevant, but I'm leaving it in for completeness.

```
            "title" : {
              "type" : "multi_field",
              "fields" : {
                "title" : {
                  "type" : "string"
                },
                "raw" : {
                  "type" : "string",
                  "index" : "not_analyzed",
                  "omit_norms" : true,
                  "index_options" : "docs",
                  "include_in_all" : false
                }
              }

```

My sort script that just serializes all the array values into one string:

'script' =\> "s='';foreach(val : doc['title.raw'].values) {s += val + ' '} s"  
,

Example source data:

```
{
    "_id": "1",
    "title": ["Y", "B"]
},
{
    "_id": "2",
    "title": ["Z", "A"]
}

```

With that source data and that script sort, I would expect ES to return doc  
1 first because Y comes before Z. However, it returns doc 2 first because A  
comes before B. (I'm guessing ES is storing the title array values already  
sorted in ascending order.)

Is there any way to make this approach work? If not, I'm thinking about  
using the river (which is how ES gets all its data in our architecture)  
script to populate a new field with the first element of the title array.

--  
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: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [July 4, 2013, 4:27pm UTC](https://discuss.elastic.co/t/how-to-script-sort-on-a-not-analyzed-field-containing-an-array-in-es-0-20-6/12653/2 "2013-07-04T16:27:58Z")

</div>

I would use a new field and place the burden of constructing a sortable  
string at index time. Your index will grow bigger, but query time  
performance will be improved.

--  
Ivan

On Wed, Jul 3, 2013 at 5:23 PM, Brian Gadoury [bgadoury@endpoint.com](mailto:bgadoury@endpoint.com) wrote:

> My attempts to use a script to sort on a field containing an array of  
> not\_analyzed strings does not behave as I'd expect. Elasticsearch sorts on  
> the highest-sorted value in the array, rather than the array values in  
> their original order. Is that expected behavior?
> 
> The \_mapping for my title field. I don't think the multi\_field aspect is  
> relevant, but I'm leaving it in for completeness.
> 
> ```
> "title" : {
> "type" : "multi_field",
> "fields" : {
> "title" : {
> "type" : "string"
> },
> "raw" : {
> "type" : "string",
> "index" : "not_analyzed",
> "omit_norms" : true,
> "index_options" : "docs",
> "include_in_all" : false
> }
> }
> 
> ```
> 
> My sort script that just serializes all the array values into one string:
> 
> 'script' =\> "s='';foreach(val : doc['title.raw'].values) {s += val + ' '}  
> s",
> 
> Example source data:
> 
> ```
> {
> "_id": "1",
> "title": ["Y", "B"]
> },
> {
> "_id": "2",
> "title": ["Z", "A"]
> }
> 
> ```
> 
> With that source data and that script sort, I would expect ES to return  
> doc 1 first because Y comes before Z. However, it returns doc 2 first  
> because A comes before B. (I'm guessing ES is storing the title array  
> values already sorted in ascending order.)
> 
> Is there any way to make this approach work? If not, I'm thinking about  
> using the river (which is how ES gets all its data in our architecture)  
> script to populate a new field with the first element of the title array.
> 
> --  
> 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).

--  
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:28am UTC](https://discuss.elastic.co/t/how-to-script-sort-on-a-not-analyzed-field-containing-an-array-in-es-0-20-6/12653/3 "2017-07-06T02:28:10Z")

</div>


