-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses elastic/elasticsearch#44345
- Loading branch information
Showing
9 changed files
with
154 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using Elasticsearch.Net.Utf8Json; | ||
|
||
namespace Nest | ||
{ | ||
[InterfaceDataContract] | ||
[ReadAs(typeof(PinnedQuery))] | ||
public interface IPinnedQuery : IQuery | ||
{ | ||
[DataMember(Name = "ids")] | ||
IEnumerable<Id> Ids { get; set; } | ||
|
||
[DataMember(Name = "organic")] | ||
QueryContainer Organic { get; set; } | ||
} | ||
|
||
public class PinnedQuery : QueryBase, IPinnedQuery | ||
{ | ||
public IEnumerable<Id> Ids { get; set; } | ||
|
||
public QueryContainer Organic { get; set; } | ||
|
||
protected override bool Conditionless => IsConditionless(this); | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer c) => c.Pinned = this; | ||
|
||
internal static bool IsConditionless(IPinnedQuery q) => !q.Ids.HasAny() && q.Organic.IsConditionless(); | ||
} | ||
|
||
public class PinnedQueryDescriptor<T> | ||
: QueryDescriptorBase<PinnedQueryDescriptor<T>, IPinnedQuery> | ||
, IPinnedQuery | ||
where T : class | ||
{ | ||
protected override bool Conditionless => PinnedQuery.IsConditionless(this); | ||
IEnumerable<Id> IPinnedQuery.Ids { get; set; } | ||
QueryContainer IPinnedQuery.Organic { get; set; } | ||
|
||
public PinnedQueryDescriptor<T> Ids(params Id[] ids) => Assign(ids, (a, v) => a.Ids = v); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<Id> ids) => Ids(ids?.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(params string[] ids) => Assign(ids?.Select(v => (Id)v), (a, v) => a.Ids = v); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<string> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(params long[] ids) => Assign(ids?.Select(v => (Id)v), (a, v) => a.Ids = v); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<long> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(params Guid[] ids) => Assign(ids?.Select(v => (Id)v), (a, v) => a.Ids = v); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<Guid> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Organic(Func<QueryContainerDescriptor<T>, QueryContainer> selector) => | ||
Assign(selector, (a, v) => a.Organic = v?.Invoke(new QueryContainerDescriptor<T>())); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
src/Tests/Tests/QueryDsl/Specialized/Pinned/PinnedQueryUsageTests.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,67 @@ | ||
using System; | ||
using Nest; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
#pragma warning disable 618 //Testing an obsolete method | ||
|
||
namespace Tests.QueryDsl.Specialized.Pinned | ||
{ | ||
/** | ||
* See the Elasticsearch documentation on {ref_current}/query-dsl-rank-feature-query.html[rank feature query] for more details. | ||
*/ | ||
public class PinnedQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public PinnedQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IPinnedQuery>(a => a.Pinned) | ||
{ | ||
q => | ||
{ | ||
q.Ids = null; | ||
q.Organic = null; | ||
}, | ||
q => | ||
{ | ||
q.Ids = Array.Empty<Id>(); | ||
q.Organic = ConditionlessQuery; | ||
}, | ||
}; | ||
|
||
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IPinnedQuery>(a => a.Pinned) | ||
{ | ||
q => q.Organic = VerbatimQuery, | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new PinnedQuery() | ||
{ | ||
Name = "named_query", | ||
Boost = 1.1, | ||
Organic = new MatchAllQuery { Name = "organic_query" }, | ||
Ids = new Id[] { 1,11,22 }, | ||
}; | ||
|
||
protected override object QueryJson => new | ||
{ | ||
pinned = new | ||
{ | ||
_name = "named_query", | ||
boost = 1.1, | ||
organic = new | ||
{ | ||
match_all = new { _name = "organic_query" } | ||
}, | ||
ids = new [] { 1, 11, 22}, | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.Pinned(c => c | ||
.Name("named_query") | ||
.Boost(1.1) | ||
.Organic(qq => qq.MatchAll(m => m.Name("organic_query"))) | ||
.Ids(1, 11, 22) | ||
); | ||
} | ||
} |