Skip to content

Commit

Permalink
#275 KnnQuery support
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-arapiles committed Nov 18, 2022
1 parent fcdfa93 commit b8016c1
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

//----------------------------------------------------
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
//----------------------------------------------------

package org.opensearch.client.opensearch._types.query_dsl;

import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;

import javax.annotation.Nullable;
import java.util.function.Function;

@JsonpDeserializable
public class KnnQuery extends QueryBase implements QueryVariant {

private final String field;
private final JsonData vector;
private final JsonData k;

protected KnnQuery(Builder builder) {
super(builder);
this.field = ApiTypeHelper.requireNonNull(builder.field, this, "field");

this.k = builder.k;
this.vector = builder.vector;
}

public static KnnQuery of(Function<Builder, ObjectBuilder<KnnQuery>> fn) {
return fn.apply(new Builder()).build();
}


@Override
public Query.Kind _queryKind() {
return null;
}

public static class Builder extends AbstractBuilder<Builder> implements ObjectBuilder<KnnQuery> {
private String field;

/**
* Required - The target field
*/
public final Builder field(String value) {
this.field = value;
return this;
}

@Nullable
private JsonData vector;

@Nullable
private JsonData k;

/**
* API name: {@code vector}
*/
public final Builder vector(@Nullable JsonData vector) {
this.vector = vector;
return this;
}

/**
* API name: {@code k}
*/
public final Builder k(@Nullable JsonData k) {
this.k = k;
return this;
}

@Override
protected Builder self() {
return this;
}

/**
* Builds a {@link KnnQuery}.
*
* @throws NullPointerException
* if some required fields are null.
*/
public KnnQuery build() {
_checkSingleUse();

return new KnnQuery(this);
}
}
// ----------------------------------------------------------------------------------------

public static final JsonpDeserializer<KnnQuery> _DESERIALIZER = ObjectBuilderDeserializer.lazy(
Builder::new,
KnnQuery::setupKnnQueryDeserializer);

protected static void setupKnnQueryDeserializer(ObjectDeserializer<Builder> op) {
QueryBase.setupQueryBaseDeserializer(op);
op.add(Builder::vector, JsonData._DESERIALIZER, "vector");
op.add(Builder::k, JsonData._DESERIALIZER, "k");

op.setKey(Builder::field, JsonpDeserializer.stringDeserializer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public enum Kind implements JsonEnum {

Intervals("intervals"),

Knn("knn"),
Match("match"),

MatchAll("match_all"),
Expand Down Expand Up @@ -535,6 +536,23 @@ public IntervalsQuery intervals() {
return TaggedUnionUtils.get(this, Kind.Intervals);
}

/**
* Is this variant instance of kind {@code knn}?
*/
public boolean isKnn() {
return _kind == Kind.Knn;
}

/**
* Get the {@code knn} variant value
*
* @throws IllegalStateException
* if the current variant is not of the {@code knn} kind.
*/
public KnnQuery knn() {
return TaggedUnionUtils.get(this, Kind.Knn);
}

/**
* Is this variant instance of kind {@code match}?
*/
Expand Down Expand Up @@ -1340,6 +1358,16 @@ public ObjectBuilder<Query> intervals(Function<IntervalsQuery.Builder, ObjectBui
return this.intervals(fn.apply(new IntervalsQuery.Builder()).build());
}

public ObjectBuilder<Query> knn(KnnQuery v) {
this._kind = Kind.Knn;
this._value = v;
return this;
}

public ObjectBuilder<Query> knn(Function<KnnQuery.Builder, ObjectBuilder<KnnQuery>> fn) {
return this.knn(fn.apply(new KnnQuery.Builder()).build());
}

public ObjectBuilder<Query> match(MatchQuery v) {
this._kind = Kind.Match;
this._value = v;
Expand Down

0 comments on commit b8016c1

Please sign in to comment.