/** * Add sorts to the elasticsearch query based on the 'ORDER BY' clause. * * @param orderBys * list of Order object */ private void setSorts(List<Order> orderBys) { for (Order order : orderBys) { if (order.getNestedPath() != null) { request.addSort(SortBuilders.fieldSort(order.getName()).order(SortOrder.valueOf(order.getType())).setNestedSort(new NestedSortBuilder(order.getNestedPath()))); } else if (order.getName().contains("script(")) { //zhongshu-comment 该分支是我后来加的,用于兼容order by case when那种情况 String scriptStr = order.getName().substring("script(".length(), order.getName().length() - 1); Script script = new Script(scriptStr); ScriptSortBuilder scriptSortBuilder = SortBuilders.scriptSort(script, order.getScriptSortType()); scriptSortBuilder = scriptSortBuilder.order(SortOrder.valueOf(order.getType())); request.addSort(scriptSortBuilder); } else { request.addSort( order.getName(), SortOrder.valueOf(order.getType())); } } }
@Override public FieldSortBuilder rewrite(QueryRewriteContext ctx) throws IOException { if (nestedFilter == null && nestedSort == null) { return this; } if (nestedFilter != null) { QueryBuilder rewrite = nestedFilter.rewrite(ctx); if (nestedFilter == rewrite) { return this; } return new FieldSortBuilder(this).setNestedFilter(rewrite); } else { NestedSortBuilder rewrite = nestedSort.rewrite(ctx); if (nestedSort == rewrite) { return this; } return new FieldSortBuilder(this).setNestedSort(rewrite); } } }
/** Copy constructor. */ public FieldSortBuilder(FieldSortBuilder template) { this(template.fieldName); this.order(template.order()); this.missing(template.missing()); this.unmappedType(template.unmappedType()); if (template.sortMode != null) { this.sortMode(template.sortMode()); } this.setNestedFilter(template.getNestedFilter()); this.setNestedPath(template.getNestedPath()); if (template.getNestedSort() != null) { this.setNestedSort(template.getNestedSort()); }; }
@Override public FieldSortBuilder rewrite(QueryRewriteContext ctx) throws IOException { if (nestedFilter == null && nestedSort == null) { return this; } if (nestedFilter != null) { QueryBuilder rewrite = nestedFilter.rewrite(ctx); if (nestedFilter == rewrite) { return this; } return new FieldSortBuilder(this).setNestedFilter(rewrite); } else { NestedSortBuilder rewrite = nestedSort.rewrite(ctx); if (nestedSort == rewrite) { return this; } return new FieldSortBuilder(this).setNestedSort(rewrite); } } }
/** * Add sorts to the elasticsearch query based on the 'ORDER BY' clause. * * @param orderBys * list of Order object */ private void setSorts(List<Order> orderBys) { for (Order order : orderBys) { if (order.getNestedPath() != null) { request.addSort(SortBuilders.fieldSort(order.getName()).order(SortOrder.valueOf(order.getType())).setNestedSort(new NestedSortBuilder(order.getNestedPath()))); } else { request.addSort(order.getName(), SortOrder.valueOf(order.getType())); } } }
/** Copy constructor. */ public FieldSortBuilder(FieldSortBuilder template) { this(template.fieldName); this.order(template.order()); this.missing(template.missing()); this.unmappedType(template.unmappedType()); if (template.sortMode != null) { this.sortMode(template.sortMode()); } this.setNestedFilter(template.getNestedFilter()); this.setNestedPath(template.getNestedPath()); if (template.getNestedSort() != null) { this.setNestedSort(template.getNestedSort()); }; }
/** Copy constructor. */ public FieldSortBuilder(FieldSortBuilder template) { this(template.fieldName); this.order(template.order()); this.missing(template.missing()); this.unmappedType(template.unmappedType()); if (template.sortMode != null) { this.sortMode(template.sortMode()); } this.setNestedFilter(template.getNestedFilter()); this.setNestedPath(template.getNestedPath()); if (template.getNestedSort() != null) { this.setNestedSort(template.getNestedSort()); }; }
return this; return new FieldSortBuilder(this).setNestedSort(rewrite);