/* 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. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * 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. */ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Runtime.Serialization; using OpenSearch.Net.Utf8Json; namespace OpenSearch.Client { /// /// The percolate query can be used to match queries stored in an index /// [InterfaceDataContract] [ReadAs(typeof(PercolateQuery))] public interface IPercolateQuery : IQuery { /// /// The source of the document to percolate. /// [DataMember(Name = "document")] [JsonFormatter(typeof(SourceFormatter))] object Document { get; set; } /// /// The source of the documents to percolate. Like but allows /// multiple documents to be percolated. /// [DataMember(Name = "documents")] [JsonFormatter(typeof(SourceFormatter>))] IEnumerable Documents { get; set; } /// /// The name of the field containing the percolated query on an existing document. This is a required parameter. /// [DataMember(Name = "field")] Field Field { get; set; } /// /// The id of the document to fetch for percolation. /// Can be specified to percolate an existing document instead of providing /// [DataMember(Name = "id")] Id Id { get; set; } /// /// The index the document resides in for percolation. /// Can be specified to percolate an existing document instead of providing /// [DataMember(Name = "index")] IndexName Index { get; set; } /// /// Preference to be used to fetch the document to percolate. /// Can be specified to percolate an existing document instead of providing /// [DataMember(Name = "preference")] string Preference { get; set; } /// /// Routing to be used to fetch the document to percolate. /// Can be specified to percolate an existing document instead of providing /// [DataMember(Name = "routing")] Routing Routing { get; set; } /// /// The expected version of the document to be fetched for percolation. /// Can be specified to percolate an existing document instead of providing /// [DataMember(Name = "version")] long? Version { get; set; } } /// /// The percolate query can be used to match queries stored in an index /// public class PercolateQuery : QueryBase, IPercolateQuery { private Routing _routing; /// public object Document { get; set; } /// public IEnumerable Documents { get; set; } /// public Field Field { get; set; } /// public Id Id { get; set; } /// public IndexName Index { get; set; } /// public string Preference { get; set; } /// public Routing Routing { get => _routing ?? (Document == null ? null : new Routing(Document)); set => _routing = value; } /// public long? Version { get; set; } protected override bool Conditionless => IsConditionless(this); internal override void InternalWrapInContainer(IQueryContainer c) => c.Percolate = this; internal static bool IsConditionless(IPercolateQuery q) { var docFields = q.Document == null && q.Documents == null; if (!docFields) return false; return q.Index == null || q.Id.IsConditionless() || q.Field.IsConditionless(); } } /// /// The percolate query can be used to match queries stored in an index /// /// The document type that contains the percolated query public class PercolateQueryDescriptor : QueryDescriptorBase, IPercolateQuery> , IPercolateQuery where T : class { private Routing _routing; protected override bool Conditionless => PercolateQuery.IsConditionless(this); object IPercolateQuery.Document { get; set; } IEnumerable IPercolateQuery.Documents { get; set; } Field IPercolateQuery.Field { get; set; } Id IPercolateQuery.Id { get; set; } IndexName IPercolateQuery.Index { get; set; } string IPercolateQuery.Preference { get; set; } Routing IPercolateQuery.Routing { get => _routing ?? (Self.Document == null ? null : new Routing(Self.Document)); set => _routing = value; } long? IPercolateQuery.Version { get; set; } /// public PercolateQueryDescriptor Field(Field field) => Assign(field, (a, v) => a.Field = v); /// public PercolateQueryDescriptor Field(Expression> objectPath) => Assign(objectPath, (a, v) => a.Field = v); /// public PercolateQueryDescriptor Document(TDocument document) => Assign(document, (a, v) => a.Document = v); /// public PercolateQueryDescriptor Documents(params TDocument[] documents) => Assign(documents.Cast(), (a, v) => a.Documents = v); /// public PercolateQueryDescriptor Documents(IEnumerable documents) => Assign(documents.Cast(), (a, v) => a.Documents = v); /// public PercolateQueryDescriptor Id(string id) => Assign(id, (a, v) => a.Id = v); /// /// The index the document resides in for percolation. /// Can be specified to percolate an existing document instead of providing /// public PercolateQueryDescriptor Index(IndexName index) => Assign(index, (a, v) => a.Index = v); /// /// The index the document resides in for percolation. /// Can be specified to percolate an existing document instead of providing /// public PercolateQueryDescriptor Index() => Assign(typeof(TDocument), (a, v) => a.Index = v); /// public PercolateQueryDescriptor Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v); /// public PercolateQueryDescriptor Preference(string preference) => Assign(preference, (a, v) => a.Preference = v); /// public PercolateQueryDescriptor Version(long? version) => Assign(version, (a, v) => a.Version = v); } }