/* 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 OpenSearch.Net.Utf8Json; using OpenSearch.Net.Utf8Json.Internal; namespace OpenSearch.Client { [InterfaceDataContract] [JsonFormatter(typeof(GeoBoundingBoxQueryFormatter))] public interface IGeoBoundingBoxQuery : IFieldNameQuery { IBoundingBox BoundingBox { get; set; } GeoExecution? Type { get; set; } GeoValidationMethod? ValidationMethod { get; set; } } public class GeoBoundingBoxQuery : FieldNameQueryBase, IGeoBoundingBoxQuery { public IBoundingBox BoundingBox { get; set; } public GeoExecution? Type { get; set; } public GeoValidationMethod? ValidationMethod { get; set; } protected override bool Conditionless => IsConditionless(this); internal override void InternalWrapInContainer(IQueryContainer c) => c.GeoBoundingBox = this; internal static bool IsConditionless(IGeoBoundingBoxQuery q) => q.Field.IsConditionless() || q.BoundingBox?.BottomRight == null && q.BoundingBox?.TopLeft == null && q.BoundingBox?.WellKnownText == null; } public class GeoBoundingBoxQueryDescriptor : FieldNameQueryDescriptorBase, IGeoBoundingBoxQuery, T> , IGeoBoundingBoxQuery where T : class { protected override bool Conditionless => GeoBoundingBoxQuery.IsConditionless(this); IBoundingBox IGeoBoundingBoxQuery.BoundingBox { get; set; } GeoExecution? IGeoBoundingBoxQuery.Type { get; set; } GeoValidationMethod? IGeoBoundingBoxQuery.ValidationMethod { get; set; } public GeoBoundingBoxQueryDescriptor BoundingBox(double topLeftLat, double topLeftLon, double bottomRightLat, double bottomRightLon) => BoundingBox(f => f.TopLeft(topLeftLat, topLeftLon).BottomRight(bottomRightLat, bottomRightLon)); public GeoBoundingBoxQueryDescriptor BoundingBox(GeoLocation topLeft, GeoLocation bottomRight) => BoundingBox(f => f.TopLeft(topLeft).BottomRight(bottomRight)); public GeoBoundingBoxQueryDescriptor BoundingBox(string wkt) => BoundingBox(f => f.WellKnownText(wkt)); public GeoBoundingBoxQueryDescriptor BoundingBox(Func boundingBoxSelector) => Assign(boundingBoxSelector, (a, v) => a.BoundingBox = v?.Invoke(new BoundingBoxDescriptor())); public GeoBoundingBoxQueryDescriptor Type(GeoExecution? type) => Assign(type, (a, v) => a.Type = v); public GeoBoundingBoxQueryDescriptor ValidationMethod(GeoValidationMethod? validation) => Assign(validation, (a, v) => a.ValidationMethod = v); } internal class GeoBoundingBoxQueryFormatter : IJsonFormatter { private static readonly AutomataDictionary Fields = new AutomataDictionary { { "_name", 0 }, { "boost", 1 }, { "validation_method", 2 }, { "type", 3 } }; public IGeoBoundingBoxQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.GetCurrentJsonToken() != JsonToken.BeginObject) return null; var query = new GeoBoundingBoxQuery(); var count = 0; while (reader.ReadIsInObject(ref count)) { var property = reader.ReadPropertyNameSegmentRaw(); if (Fields.TryGetValue(property, out var value)) { switch (value) { case 0: query.Name = reader.ReadString(); break; case 1: query.Boost = reader.ReadDouble(); break; case 2: query.ValidationMethod = formatterResolver.GetFormatter() .Deserialize(ref reader, formatterResolver); break; case 3: query.Type = formatterResolver.GetFormatter() .Deserialize(ref reader, formatterResolver); break; } } else { query.Field = property.Utf8String(); query.BoundingBox = formatterResolver.GetFormatter() .Deserialize(ref reader, formatterResolver); } } return query; } public void Serialize(ref JsonWriter writer, IGeoBoundingBoxQuery value, IJsonFormatterResolver formatterResolver) { if (value == null) { writer.WriteNull(); return; } var written = false; writer.WriteBeginObject(); if (!value.Name.IsNullOrEmpty()) { writer.WritePropertyName("_name"); writer.WriteString(value.Name); written = true; } if (value.Boost != null) { if (written) writer.WriteValueSeparator(); writer.WritePropertyName("boost"); writer.WriteDouble(value.Boost.Value); written = true; } if (value.ValidationMethod != null) { if (written) writer.WriteValueSeparator(); writer.WritePropertyName("validation_method"); formatterResolver.GetFormatter() .Serialize(ref writer, value.ValidationMethod.Value, formatterResolver); written = true; } if (value.Type != null) { if (written) writer.WriteValueSeparator(); writer.WritePropertyName("type"); formatterResolver.GetFormatter() .Serialize(ref writer, value.Type.Value, formatterResolver); written = true; } if (written) writer.WriteValueSeparator(); var settings = formatterResolver.GetConnectionSettings(); writer.WritePropertyName(settings.Inferrer.Field(value.Field)); formatterResolver.GetFormatter() .Serialize(ref writer, value.BoundingBox, formatterResolver); writer.WriteEndObject(); } } }