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.