-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] Add Support for Hybrid Query Type (#857)
* Add Support for Hybrid Query Type (#850) * Add Support for Hybrid Query Type Signed-off-by: Varun Jain <[email protected]> * Add samples, guide and integ tests Signed-off-by: Varun Jain <[email protected]> * Removing wildcard imports Signed-off-by: Varun Jain <[email protected]> * Adding import Signed-off-by: Varun Jain <[email protected]> * Adding import Signed-off-by: Varun Jain <[email protected]> --------- Signed-off-by: Varun Jain <[email protected]> (cherry picked from commit 821dae6) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Vacha Shah <[email protected]> * Adding version check for hybrid query integ tests (#863) (#864) Signed-off-by: Varun Jain <[email protected]> Signed-off-by: Vacha Shah <[email protected]> --------- Signed-off-by: Varun Jain <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Vacha Shah <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Varun Jain <[email protected]>
- Loading branch information
1 parent
7acd729
commit cada0fc
Showing
9 changed files
with
332 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/HybridQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.opensearch.client.opensearch._types.query_dsl; | ||
|
||
import jakarta.json.stream.JsonGenerator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import org.opensearch.client.json.JsonpDeserializer; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.json.ObjectBuilderDeserializer; | ||
import org.opensearch.client.json.ObjectDeserializer; | ||
import org.opensearch.client.util.ApiTypeHelper; | ||
import org.opensearch.client.util.ObjectBuilder; | ||
|
||
public class HybridQuery extends QueryBase implements QueryVariant { | ||
private final List<Query> queries; | ||
|
||
private HybridQuery(HybridQuery.Builder builder) { | ||
super(builder); | ||
this.queries = ApiTypeHelper.unmodifiable(builder.queries); | ||
} | ||
|
||
public static HybridQuery of(Function<HybridQuery.Builder, ObjectBuilder<HybridQuery>> fn) { | ||
return fn.apply(new HybridQuery.Builder()).build(); | ||
} | ||
|
||
/** | ||
* Required - list of search queries. | ||
* | ||
* @return list of queries provided under hybrid clause. | ||
*/ | ||
public final List<Query> queries() { | ||
return this.queries; | ||
} | ||
|
||
@Override | ||
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { | ||
super.serializeInternal(generator, mapper); | ||
generator.writeKey("queries"); | ||
generator.writeStartArray(); | ||
for (Query item0 : this.queries) { | ||
item0.serialize(generator, mapper); | ||
} | ||
generator.writeEnd(); | ||
} | ||
|
||
@Override | ||
public Query.Kind _queryKind() { | ||
return Query.Kind.Hybrid; | ||
} | ||
|
||
public HybridQuery.Builder toBuilder() { | ||
return new HybridQuery.Builder().queries(queries); | ||
} | ||
|
||
public static class Builder extends QueryBase.AbstractBuilder<HybridQuery.Builder> implements ObjectBuilder<HybridQuery> { | ||
private List<Query> queries; | ||
|
||
/** | ||
* API name: {@code hybrid} | ||
* <p> | ||
* Adds all elements of <code>list</code> to <code>hybrid</code>. | ||
*/ | ||
public final HybridQuery.Builder queries(List<Query> list) { | ||
this.queries = _listAddAll(this.queries, list); | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code hybrid} | ||
* <p> | ||
* Adds one or more values to <code>hybrid</code>. | ||
*/ | ||
public final HybridQuery.Builder queries(Query value, Query... values) { | ||
this.queries = _listAdd(this.queries, value, values); | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code hybrid} | ||
* <p> | ||
* Adds a value to <code>hybrid</code> using a builder lambda. | ||
*/ | ||
public final HybridQuery.Builder queries(Function<Query.Builder, ObjectBuilder<Query>> fn) { | ||
return queries(fn.apply(new Query.Builder()).build()); | ||
} | ||
|
||
@Override | ||
protected Builder self() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public HybridQuery build() { | ||
_checkSingleUse(); | ||
return new HybridQuery(this); | ||
} | ||
} | ||
|
||
public static final JsonpDeserializer<HybridQuery> _DESERIALIZER = ObjectBuilderDeserializer.lazy( | ||
HybridQuery.Builder::new, | ||
HybridQuery::setupHybridQueryDeserializer | ||
); | ||
|
||
protected static void setupHybridQueryDeserializer(ObjectDeserializer<HybridQuery.Builder> op) { | ||
setupQueryBaseDeserializer(op); | ||
op.add(HybridQuery.Builder::queries, JsonpDeserializer.arrayDeserializer(Query._DESERIALIZER), "queries"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ient/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/HybridQueryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.opensearch.client.opensearch._types.query_dsl; | ||
|
||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import org.opensearch.client.opensearch._types.FieldValue; | ||
import org.opensearch.client.opensearch.model.ModelTestCase; | ||
|
||
public class HybridQueryTest extends ModelTestCase { | ||
@Test | ||
public void toBuilder() { | ||
HybridQuery origin = new HybridQuery.Builder().queries( | ||
Arrays.asList( | ||
new TermQuery.Builder().field("passage_text").value(FieldValue.of("Foo bar")).build().toQuery(), | ||
new NeuralQuery.Builder().field("passage_embedding") | ||
.queryText("Hi world") | ||
.modelId("bQ1J8ooBpBj3wT4HVUsb") | ||
.k(100) | ||
.build() | ||
.toQuery(), | ||
new KnnQuery.Builder().field("passage_embedding").vector(new float[] { 0.01f, 0.02f }).k(2).build().toQuery() | ||
) | ||
).build(); | ||
HybridQuery copied = origin.toBuilder().build(); | ||
|
||
assertEquals(toJson(copied), toJson(origin)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.