|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.Serialization; |
| 5 | +using Elasticsearch.Net.Utf8Json; |
| 6 | + |
| 7 | +namespace Nest |
| 8 | +{ |
| 9 | + [InterfaceDataContract] |
| 10 | + [ReadAs(typeof(PinnedQuery))] |
| 11 | + public interface IPinnedQuery : IQuery |
| 12 | + { |
| 13 | + [DataMember(Name = "ids")] |
| 14 | + IEnumerable<Id> Ids { get; set; } |
| 15 | + |
| 16 | + [DataMember(Name = "organic")] |
| 17 | + QueryContainer Organic { get; set; } |
| 18 | + } |
| 19 | + |
| 20 | + public class PinnedQuery : QueryBase, IPinnedQuery |
| 21 | + { |
| 22 | + public IEnumerable<Id> Ids { get; set; } |
| 23 | + |
| 24 | + public QueryContainer Organic { get; set; } |
| 25 | + |
| 26 | + protected override bool Conditionless => IsConditionless(this); |
| 27 | + |
| 28 | + internal override void InternalWrapInContainer(IQueryContainer c) => c.Pinned = this; |
| 29 | + |
| 30 | + internal static bool IsConditionless(IPinnedQuery q) => !q.Ids.HasAny() && q.Organic.IsConditionless(); |
| 31 | + } |
| 32 | + |
| 33 | + public class PinnedQueryDescriptor<T> |
| 34 | + : QueryDescriptorBase<PinnedQueryDescriptor<T>, IPinnedQuery> |
| 35 | + , IPinnedQuery |
| 36 | + where T : class |
| 37 | + { |
| 38 | + protected override bool Conditionless => PinnedQuery.IsConditionless(this); |
| 39 | + IEnumerable<Id> IPinnedQuery.Ids { get; set; } |
| 40 | + QueryContainer IPinnedQuery.Organic { get; set; } |
| 41 | + |
| 42 | + public PinnedQueryDescriptor<T> Ids(params Id[] ids) => Assign(ids, (a, v) => a.Ids = v); |
| 43 | + |
| 44 | + public PinnedQueryDescriptor<T> Ids(IEnumerable<Id> ids) => Ids(ids?.ToArray()); |
| 45 | + |
| 46 | + public PinnedQueryDescriptor<T> Ids(IEnumerable<string> ids) => Ids(ids.ToArray()); |
| 47 | + |
| 48 | + public PinnedQueryDescriptor<T> Ids(IEnumerable<long> ids) => Ids(ids.ToArray()); |
| 49 | + |
| 50 | + public PinnedQueryDescriptor<T> Ids(IEnumerable<Guid> ids) => Ids(ids.ToArray()); |
| 51 | + |
| 52 | + public PinnedQueryDescriptor<T> Organic(Func<QueryContainerDescriptor<T>, QueryContainer> selector) => |
| 53 | + Assign(selector, (a, v) => a.Organic = v?.Invoke(new QueryContainerDescriptor<T>())); |
| 54 | + } |
| 55 | +} |
0 commit comments