Script sorting issue 0.90.9

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.
To view this discussion on the web visit 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.

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.
To view this discussion on the web visit 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.

Bihn,

That worked, thanks. I had been going by the fields outlined
in Elasticsearch Platform — Find real-time answers at scale | Elastic

  • 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.
To view this discussion on the web visit 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.