# Script sorting issue 0.90.9

**URL:** <https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117>\
**Category:** Elasticsearch\
**Created:** [March 3, 2014, 6:50pm UTC](https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117 "2014-03-03T18:50:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Ritchie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_ritchie/32/9370_2.png) [@Bruce\_Ritchie](https://discuss.elastic.co/u/Bruce_Ritchie)\
**Post date:** [March 3, 2014, 6:50pm UTC](https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117/1 "2014-03-03T18:50:18Z")

</div>

Hi,

I've been attempting to get custom sorting working for a scenario I have  
without success. What I'm trying to do is sort by fieldA if it exists and  
if not then use fieldB for the sort. The first thing I tried was :

requestBuilder.addSort(SortBuilders.fieldSort(fieldA)).order(sortOrder).ignoreUnmapped(true));  
requestBuilder.addSort(SortBuilders.fieldSort(fieldB)).order(sortOrder).ignoreUnmapped(true));

However this doesn't seem to work as expected - rather it sorts results by  
fieldA (A-Z) _then_ by fieldB (A-Z). The two fields are mutually exclusive

- either one exists or the other, never both in the same doc.

The next thing I tried was to use a sort script and this is where I've hit  
an issue that I can't seem to overcome.

I'm using the following script:

if (!(doc["formatted\_name\_\_v.plain"].empty)) {  
doc["formatted\_name\_\_v.plain"].value; } else if  
(!(doc["corporate\_name\_\_v.plain"].empty)) {  
doc["corporate\_name\_\_v.plain"].value; } else { ""; }

being built with the following code:

```
            ScriptSortBuilder sortBuilder = 

```

SortBuilders.scriptSort(script.toString(),  
"string").lang("mvel").order(sortOrder);  
requestBuilder.addSort(sortBuilder);

and ES is complaining about a compile error

Caused by: [Error: unexpected token in constructor]  
[Near : {... tted\_name\_\_v.plain"].empty)) { doc["formatted\_name ....}]  
^  
[Line: 1, Column: 37]  
at  
org.elasticsearch.common.mvel2.ast.TypeDescriptor.updateClassName(TypeDescriptor.java:72)  
at  
org.elasticsearch.common.mvel2.ast.TypeDescriptor.(TypeDescriptor.java:50)  
at  
org.elasticsearch.common.mvel2.compiler.AbstractParser.nextToken(AbstractParser.java:1042)  
at  
org.elasticsearch.common.mvel2.compiler.ExpressionCompiler.\_compile(ExpressionCompiler.java:128)  
at  
org.elasticsearch.common.mvel2.util.ParseTools.subCompileExpression(ParseTools.java:2115)  
at  
org.elasticsearch.common.mvel2.ast.Negation.(Negation.java:40)  
^

The arrow in the output seems to point to the 'o' in the { doc[.. part.

Does anyone see anything obviously wrong with my expression?

Bruce

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/02262056-e7c1-4cc7-81dc-19f0a5bb8514%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/02262056-e7c1-4cc7-81dc-19f0a5bb8514%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 3, 2014, 7:12pm UTC](https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117/2 "2014-03-03T19:12:22Z")

</div>

Try:

doc["formatted\_name\_\_v.plain"].isEmpty()

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/43b85750-0b9c-4a90-87cb-fa342e076229%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43b85750-0b9c-4a90-87cb-fa342e076229%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Bruce\_Ritchie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_ritchie/32/9370_2.png) [@Bruce\_Ritchie](https://discuss.elastic.co/u/Bruce_Ritchie)\
**Post date:** [March 3, 2014, 7:19pm UTC](https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117/3 "2014-03-03T19:19:15Z")

</div>

Bihn,

That worked, thanks. I had been going by the fields outlined  
in [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/modules-scripting.html#_document_fields)

- I guess mvel doesn't quite handle the 'isXYZ' javabean convention  
correctly.

Bruce

On Monday, March 3, 2014 2:12:22 PM UTC-5, Binh Ly wrote:

> Try:
> 
> doc["formatted\_name\_\_v.plain"].isEmpty()

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/47ef9357-4961-4b5b-b701-9be3eb8bbdff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/47ef9357-4961-4b5b-b701-9be3eb8bbdff%40googlegroups.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, 1:46am UTC](https://discuss.elastic.co/t/script-sorting-issue-0-90-9/16117/4 "2017-07-06T01:46:07Z")

</div>


