Is there SDK support for addin boosting in elasticsearch-java?

Is there SDK support for addin boosting in elasticsearch-java?

In the 7.x SDK there was a field method on the MultiMatchQueryBuilder to set both the field name as wel as a boost value.

In the following code Field is a DTO containing both a field name and a boost value.

....
    private MultiMatchQueryBuilder addFields(MultiMatchQueryBuilder builder, Set<Field> fields) {
        fields.stream().filter(Field::isIndexed).forEach(field -> builder.field(field.getName(), field.getBoost()));
        return builder;
    }

In 8.x this functionality is missing.

What I do now is using a String.format(...) to get the boost in:

private @NotNull MultiMatchQuery.Builder createTermMatcher(
        final @NotNull String terms,
        final @NotNull Set<Field> fields) {
        return new MultiMatchQuery.Builder()
            .query(terms)
            .fields(fields
                .stream()
                .filter(Field::isIndexed)
                .map(field -> String.format("%s^%s", 
                    field.getName(),
                   field.getBoost()))
                .toList())
            .operator(Operator.And);
    }

Is there another way of doing this with the elastic-search Java-SDK ?

Your approach is correct, the Java API client has no helper method to combine field name and boost like the 7.x client.

1 Like