Why am I getting this `unexpected byte [0x10]` in the constructor for a custom PluginBuilder?

Using Elasticsearch 5.6.5 I'm getting the following strange error:

org.elasticsearch.transport.RemoteTransportException: [iad1esdn20vz166][x.x.x.x:31203][indices:data/read/search[phase/query]]
Caused by: java.lang.IllegalStateException: unexpected byte [0x10]
at org.elasticsearch.common.io.stream.StreamInput.readBoolean(StreamInput.java:409) ~[elasticsearch-5.6.5.jar:5.6.5]
at org.elasticsearch.common.io.stream.StreamInput.readBoolean(StreamInput.java:399) ~[elasticsearch-5.6.5.jar:5.6.5]
at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.read(ValuesSourceAggregationBuilder.java:119) ~[elasticsearch-5.6.5.jar:5.6.5]
at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.(ValuesSourceAggregationBuilder.java:99) ~[elasticsearch-5.6.5.jar:5.6.5]
at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder$LeafOnly.(ValuesSourceAggregationBuilder.java:53) ~[elasticsearch-5.6.5.jar:5.6.5]
at com.foo.elasticsearch.VectorAggregationPluginBuilder.(VectorAggregationPluginBuilder.java:88) ~[?:?]
...

Can anyone see anything that I'm obviously doing wrong with my plugin? The error is thrown at the line: super(in, ValuesSourceType.BYTES, ValueType.STRING); - and then you can see the stack trace above from that point to line 119 in org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder

public class VectorAggregationPluginBuilder extends ValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Bytes, VectorAggregationPluginBuilder> {
    public static final String NAME = "vector_avg";

    public static final ParseField ENCODED_QUERY_VECTOR_FIELD = new ParseField("encoded_vector");
    public static final ParseField USE_COSINE_FIELD = new ParseField("cosine");

    private static final ObjectParser<VectorAggregationPluginBuilder, QueryParseContext> PARSER;

    private String encodedQueryVector = null;
    private double[] queryVector = null;
    private double magnitude = 0;
    private boolean useCosine = false;

    static {
        PARSER = new ObjectParser<>(VectorAggregationPluginBuilder.NAME);
        ValuesSourceParserHelper.declareBytesFields(PARSER, false, false);
        PARSER.declareStringOrNull(VectorAggregationPluginBuilder::encodedQueryVector, VectorAggregationPluginBuilder.ENCODED_QUERY_VECTOR_FIELD);
        PARSER.declareBoolean(VectorAggregationPluginBuilder::useCosine, VectorAggregationPluginBuilder.USE_COSINE_FIELD);
    }

    public static AggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
        return PARSER.parse(context.parser(), new VectorAggregationPluginBuilder(aggregationName), context);
    }

    public VectorAggregationPluginBuilder(String name) {
        super(name, ValuesSourceType.BYTES, ValueType.STRING);
    }

    /**
     * Read from a stream.
     */
    public VectorAggregationPluginBuilder(StreamInput in) throws IOException {
        super(in, ValuesSourceType.BYTES, ValueType.STRING);
        encodedQueryVector = in.readOptionalString();
        if (encodedQueryVector != null) {
            queryVector = Util.convertBase64ToArray(encodedQueryVector);
        }
        Boolean useCosineNullable = in.readOptionalBoolean();
        if (useCosineNullable != null){
            useCosine = useCosineNullable.booleanValue();
        }
    }

    @Override
    protected void innerWriteTo(StreamOutput out) throws IOException {
        out.writeOptionalString(encodedQueryVector);
        out.writeOptionalBoolean(new Boolean(useCosine));
    }

    @Override
    protected boolean serializeTargetValueType() {
        return true;
    }


    public VectorAggregationPluginBuilder encodedQueryVector(String encodedQueryVector) {
        if (encodedQueryVector != null){
            this.encodedQueryVector = encodedQueryVector;
        }
        return this;
    }

    public String encodedQueryVector() {
        if (encodedQueryVector != null){
            return encodedQueryVector;
        }
        return null;
    }

    public VectorAggregationPluginBuilder useCosine(boolean useCosine) {
        this.useCosine = useCosine;
        return this;
    }

    public boolean useCosine() {
        return useCosine;
    }

    @Override
    protected VectorAggregationFactory innerBuild(SearchContext context, ValuesSourceConfig<ValuesSource.Bytes> config,
            AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException {
        return new VectorAggregationFactory(name, config, queryVector, useCosine, magnitude, context, parent, subFactoriesBuilder, metaData);
    }

    @Override
    public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
        if (encodedQueryVector != null) {
            builder.field(ENCODED_QUERY_VECTOR_FIELD.getPreferredName(), encodedQueryVector);
        }
        builder.field(USE_COSINE_FIELD.getPreferredName(), useCosine);
        return builder;
    }

    @Override
    protected int innerHashCode() {
        return Objects.hash(useCosine, encodedQueryVector);
    }

    @Override
    protected boolean innerEquals(Object obj) {
        VectorAggregationPluginBuilder other = (VectorAggregationPluginBuilder) obj;
        return Objects.equals(encodedQueryVector, other.encodedQueryVector) &&
            Objects.equals(useCosine, other.useCosine);
    }

    @Override
    public String getType() {
        return NAME;
    }
}
1 Like

The problem was the line super(in, ValuesSourceType.BYTES, ValueType.STRING); It should have been super(in, ValuesSourceType.BYTES);

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.