Java Code Examples for org.elasticsearch.search.sort.ScriptSortBuilder#ScriptSortType

The following examples show how to use org.elasticsearch.search.sort.ScriptSortBuilder#ScriptSortType . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items, String alias) throws SqlParseException {
    for (SQLSelectOrderByItem sqlSelectOrderByItem : items) {
        SQLExpr expr = sqlSelectOrderByItem.getExpr();
        Field f = FieldMaker.makeField(expr, null, null);
        String orderByName = f.toString();

        if (sqlSelectOrderByItem.getType() == null) {
            sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); //zhongshu-comment 默认是升序排序
        }
        String type = sqlSelectOrderByItem.getType().toString();

        orderByName = orderByName.replace("`", "");
        if (alias != null) orderByName = orderByName.replaceFirst(alias + "\\.", "");

        ScriptSortBuilder.ScriptSortType scriptSortType = judgeIsStringSort(expr);
        select.addOrderBy(f.getNestedPath(), orderByName, type, scriptSortType);
    }
}
 
Example 2
Source File: ElasticsearchLengthOfStringSortingStrategy.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    SearchRequestBuilder q,
    QueryParameters parameters,
    SortDirection direction
) {
    PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName());

    SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
    Map<String, Object> scriptParams = new HashMap<>();
    String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations());
    List<String> fieldNames = Arrays.stream(propertyNames)
        .map(propertyName -> {
            String suffix = propertyDefinition.getDataType() == String.class
                ? Elasticsearch7SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX
                : "";
            return propertyName + suffix;
        })
        .collect(Collectors.toList());
    scriptParams.put("fieldNames", fieldNames);
    scriptParams.put("direction", esOrder.name());
    Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams);
    ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER;
    q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC));
}
 
Example 3
Source File: ElasticsearchLengthOfStringSortingStrategy.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    SearchRequestBuilder q,
    QueryParameters parameters,
    SortDirection direction
) {
    PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName());

    SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
    Map<String, Object> scriptParams = new HashMap<>();
    String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations());
    List<String> fieldNames = Arrays.stream(propertyNames)
        .map(propertyName -> {
            String suffix = propertyDefinition.getDataType() == String.class
                ? Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX
                : "";
            return propertyName + suffix;
        })
        .collect(Collectors.toList());
    scriptParams.put("fieldNames", fieldNames);
    scriptParams.put("direction", esOrder.name());
    Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams);
    ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER;
    q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC));
}
 
Example 4
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private ScriptSortBuilder.ScriptSortType judgeIsStringSort(SQLExpr expr) {
    if (expr instanceof SQLCaseExpr) {
        List<SQLCaseExpr.Item> itemList = ((SQLCaseExpr) expr).getItems();
        for (SQLCaseExpr.Item item : itemList) {
            if (item.getValueExpr() instanceof SQLCharExpr) {
                return ScriptSortBuilder.ScriptSortType.STRING;
            }
        }
    }
    return ScriptSortBuilder.ScriptSortType.NUMBER;
}
 
Example 5
Source File: Select.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public void addOrderBy(String nestedPath, String name, String type, ScriptSortBuilder.ScriptSortType scriptSortType) {
	if ("_score".equals(name)) { //zhongshu-comment 可以直接在order by子句中写_score,根据该字段排序 select * from tbl order by _score asc
		isQuery = true;
	}
	Order order = new Order(nestedPath, name, type);

	order.setScriptSortType(scriptSortType);
	this.orderBys.add(order);
}
 
Example 6
Source File: Order.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public ScriptSortBuilder.ScriptSortType getScriptSortType() {
	return scriptSortType;
}
 
Example 7
Source File: Order.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public void setScriptSortType(ScriptSortBuilder.ScriptSortType scriptSortType) {
	this.scriptSortType = scriptSortType;
}