-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thomas Farr <[email protected]>
- Loading branch information
Showing
10 changed files
with
691 additions
and
495 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
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
957 changes: 481 additions & 476 deletions
957
src/OpenSearch.Client/QueryDsl/Abstractions/Container/QueryContainerDescriptor.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
75 changes: 75 additions & 0 deletions
75
src/OpenSearch.Client/QueryDsl/Specialized/Neural/NeuralQuery.cs
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,75 @@ | ||
/* 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. | ||
*/ | ||
|
||
using System.Runtime.Serialization; | ||
using OpenSearch.Net.Utf8Json; | ||
|
||
namespace OpenSearch.Client; | ||
|
||
/// <summary> | ||
/// A neural query. | ||
/// </summary> | ||
[InterfaceDataContract] | ||
[JsonFormatter(typeof(FieldNameQueryFormatter<NeuralQuery, INeuralQuery>))] | ||
public interface INeuralQuery : IFieldNameQuery | ||
{ | ||
/// <summary> | ||
/// The query text from which to produce queries. | ||
/// </summary> | ||
[DataMember(Name = "query_text")] | ||
string QueryText { get; set; } | ||
|
||
/// <summary> | ||
/// The number of results the k-NN search returns. | ||
/// </summary> | ||
[DataMember(Name = "k")] | ||
int? K { get; set; } | ||
|
||
/// <summary> | ||
/// The ID of the model that will be used in the embedding interface. | ||
/// The model must be indexed in OpenSearch before it can be used in Neural Search. | ||
/// </summary> | ||
[DataMember(Name = "model_id")] | ||
string ModelId { get; set; } | ||
} | ||
|
||
[DataContract] | ||
public class NeuralQuery : FieldNameQueryBase, INeuralQuery | ||
{ | ||
/// <inheritdoc /> | ||
public string QueryText { get; set; } | ||
/// <inheritdoc /> | ||
public int? K { get; set; } | ||
/// <inheritdoc /> | ||
public string ModelId { get; set; } | ||
|
||
protected override bool Conditionless => IsConditionless(this); | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer container) => container.Neural = this; | ||
|
||
internal static bool IsConditionless(INeuralQuery q) => string.IsNullOrEmpty(q.QueryText) || q.K == null || q.K == 0 || string.IsNullOrEmpty(q.ModelId) || q.Field.IsConditionless(); | ||
} | ||
|
||
public class NeuralQueryDescriptor<T> | ||
: FieldNameQueryDescriptorBase<NeuralQueryDescriptor<T>, INeuralQuery, T>, | ||
INeuralQuery | ||
where T : class | ||
{ | ||
protected override bool Conditionless => NeuralQuery.IsConditionless(this); | ||
string INeuralQuery.QueryText { get; set; } | ||
int? INeuralQuery.K { get; set; } | ||
string INeuralQuery.ModelId { get; set; } | ||
|
||
/// <inheritdoc cref="INeuralQuery.QueryText" /> | ||
public NeuralQueryDescriptor<T> QueryText(string queryText) => Assign(queryText, (a, v) => a.QueryText = v); | ||
|
||
/// <inheritdoc cref="INeuralQuery.K" /> | ||
public NeuralQueryDescriptor<T> K(int? k) => Assign(k, (a, v) => a.K = v); | ||
|
||
/// <inheritdoc cref="INeuralQuery.ModelId" /> | ||
public NeuralQueryDescriptor<T> ModelId(string modelId) => Assign(modelId, (a, v) => a.ModelId = v); | ||
} |
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
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
104 changes: 104 additions & 0 deletions
104
tests/Tests/QueryDsl/Specialized/Neural/NeuralQueryUsageTests.cs
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,104 @@ | ||
/* 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. | ||
*/ | ||
|
||
using OpenSearch.Client; | ||
using Tests.Core.ManagedOpenSearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
namespace Tests.QueryDsl.Specialized.Neural; | ||
|
||
public class NeuralQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public NeuralQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<INeuralQuery>(a => a.Neural) | ||
{ | ||
q => | ||
{ | ||
q.Field = null; | ||
q.QueryText = "wild west"; | ||
q.K = 5; | ||
q.ModelId = "aFcV879"; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = null; | ||
q.K = 5; | ||
q.ModelId = "aFcV879"; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = ""; | ||
q.K = 5; | ||
q.ModelId = "aFcV879"; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = "wild west"; | ||
q.K = null; | ||
q.ModelId = "aFcV879"; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = "wild west"; | ||
q.K = 0; | ||
q.ModelId = "aFcV879"; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = "wild west"; | ||
q.K = 5; | ||
q.ModelId = null; | ||
}, | ||
q => | ||
{ | ||
q.Field = "passage_embedding"; | ||
q.QueryText = "wild west"; | ||
q.K = 5; | ||
q.ModelId = ""; | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new NeuralQuery | ||
{ | ||
Boost = 1.1, | ||
Field = Infer.Field<Project>(f => f.Vector), | ||
QueryText = "wild west", | ||
K = 5, | ||
ModelId = "aFcV879" | ||
}; | ||
|
||
protected override object QueryJson => | ||
new | ||
{ | ||
neural = new | ||
{ | ||
vector = new | ||
{ | ||
boost = 1.1, | ||
query_text = "wild west", | ||
k = 5, | ||
model_id = "aFcV879" | ||
} | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.Neural(n => n | ||
.Boost(1.1) | ||
.Field(f => f.Vector) | ||
.QueryText("wild west") | ||
.K(5) | ||
.ModelId("aFcV879") | ||
); | ||
} |