public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var t = value as ITermsQuery;
			if (t == null) return;

			var settings = serializer.GetConnectionSettings();
			var field = settings.Inferrer.Field(t.Field);

			writer.WriteStartObject();
			{
				if (t.Terms.HasAny())
				{
					writer.WritePropertyName(field);
					serializer.Serialize(writer, t.Terms);
				}
				else if (t.TermsLookup != null)
				{
					writer.WritePropertyName(field);
					serializer.Serialize(writer, t.TermsLookup);
				}
				if (t.Boost.HasValue)
				{
					writer.WritePropertyName("boost");
					writer.WriteValue(t.Boost.Value);
				}
				if (!t.Name.IsNullOrEmpty())
				{
					writer.WritePropertyName("_name");
					writer.WriteValue(t.Name);
				}
			}
			writer.WriteEndObject();
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var marker = value as Indices;
			if (marker == null)
			{
				writer.WriteNull();
				return;
			}
			marker.Match(
				all =>
				{
					writer.WriteStartArray();
					writer.WriteValue("_all");
					writer.WriteEndArray();
				},
				many =>
				{
					var settings = serializer.GetConnectionSettings();
					writer.WriteStartArray();
					foreach (var m in many.Indices.Cast<IUrlParameter>())
						writer.WriteValue(m.GetString(settings));
					writer.WriteEndArray();
				}
			);
		}
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			var o = JObject.Load(reader);
			var fields = o.Properties().ToDictionary(p => p.Name, p => p.Value.ToObject<object>());
			var inferrer = serializer.GetConnectionSettings().Inferrer;
			var fieldValues = new FieldValues(inferrer, fields);
			return fieldValues;
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var property = value as PropertyName;
			if (property == null)
			{
				writer.WriteNull();
				return;
			}
			writer.WriteValue(new ElasticInferrer(serializer.GetConnectionSettings()).PropertyName(property));
		}
Пример #5
0
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var field = value as Field;
			if (field == null)
			{
				writer.WriteNull();
				return;
			}
			var settings = serializer.GetConnectionSettings();
			writer.WriteValue(settings.Inferrer.Field(field));
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var fields = value as Fields;
			writer.WriteStartArray();
			var infer = serializer.GetConnectionSettings().Inferrer;
			foreach (var f in fields?.ListOfFields ?? Enumerable.Empty<Field>())
			{
				writer.WriteValue(infer.Field(f));
			}
			writer.WriteEndArray();
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var dictionary = value as IDictionary;
			if (dictionary == null) return;

			var settings = serializer.GetConnectionSettings();

			var seenEntries = new Dictionary<string, object>();

			foreach (DictionaryEntry entry in dictionary)
			{
				if (entry.Value == null && serializer.NullValueHandling == NullValueHandling.Ignore)
					continue;
				string key;
				var fieldName = entry.Key as Field;
				var propertyName = entry.Key as PropertyName;
				var indexName = entry.Key as IndexName;
				var typeName = entry.Key as TypeName;
				if (settings == null)
					key = Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
				else if (fieldName != null)
					key = settings.Inferrer.Field(fieldName);
				else if (propertyName != null)
				{
					if (propertyName.Property != null)
					{
						IPropertyMapping mapping;
						if (settings.PropertyMappings.TryGetValue(propertyName.Property, out mapping) && mapping.Ignore)
						{
							continue;
						}
					}

					key = settings.Inferrer.PropertyName(propertyName);
				}
				else if (indexName != null)
					key = settings.Inferrer.IndexName(indexName);
				else if (typeName != null)
					key = settings.Inferrer.TypeName(typeName);
				else
					key = Convert.ToString(entry.Key, CultureInfo.InvariantCulture);

				seenEntries[key] = entry.Value;
			}

			writer.WriteStartObject();
			foreach(var entry in seenEntries)
			{
				writer.WritePropertyName(entry.Key);
				serializer.Serialize(writer, entry.Value);
			}
			writer.WriteEndObject();
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var marker = value as Features;
			if (marker == null)
			{
				writer.WriteNull();
				return;
			}
			var settings = serializer.GetConnectionSettings();
			var s = ((IUrlParameter)marker).GetString(settings);
			writer.WriteValue(s);
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var marker = value as Indices;
			if (marker == null)
			{
				writer.WriteNull();
				return;
			}
			marker.Match(
				all=> writer.WriteValue("_all"),
				many => writer.WriteValue(((IUrlParameter)marker).GetString(serializer.GetConnectionSettings()))
			);
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var marker = value as TypeName;
			if (marker == null)
			{
				writer.WriteNull();
				return;
			}
			var settings = serializer.GetConnectionSettings();

			var typeName = settings.Inferrer.TypeName(marker);
			writer.WriteValue(typeName);
		}
Пример #11
0
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var id = value as Id;
			if (id == null)
			{
				writer.WriteNull();
				return;
			}
			if (id.Document != null)
			{
				var settings = serializer.GetConnectionSettings();
				var indexName = settings.Inferrer.Id(id.Document.GetType(), id.Document);
				writer.WriteValue(indexName);
			}
			else writer.WriteValue(id.Value);
		}
Пример #12
0
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (value == null)
			{
				writer.WriteNull();
				return;
			}

			var id = (Id)value;
			if (id.Document != null)
			{
				var settings = serializer.GetConnectionSettings();
				var documentId = settings.Inferrer.Id(id.Document.GetType(), id.Document);
				writer.WriteValue(documentId);
			}
			else writer.WriteValue(id.Value);
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var request = value as IMultiPercolateRequest;
			if (request == null) return;
			if (request.Percolations == null) return;

			var settings = serializer.GetConnectionSettings();
			var elasticsearchSerializer = settings.Serializer;
			if (elasticsearchSerializer == null) return;
			foreach (var percolation in request.Percolations)
			{
				var requestParameters = percolation.GetRequestParameters() ?? new PercolateRequestParameters();
				var header = new PercolateHeader
				{
					percolate_index = requestParameters.GetQueryStringValue<string>("percolate_index"),
					percolate_type = requestParameters.GetQueryStringValue<string>("percolate_type"),
					routing = requestParameters.GetQueryStringValue<string[]>("routing"),
					preference = requestParameters.GetQueryStringValue<string>("preference"),
					percolate_routing = requestParameters.GetQueryStringValue<string>("percolate_routing"),
					percolate_preference = requestParameters.GetQueryStringValue<string>("percolate_preference"),
					version = requestParameters.GetQueryStringValue<long?>("version")
				};
				var routeValues = (percolation as IRequest)?.RouteValues;
				if (routeValues != null)
				{
					header.id = routeValues.Get<Id>("id");
					header.index = routeValues.Get<IndexName>("index");
					header.type = routeValues.Get<TypeName>("type");
					if (request.Index != null && request.Index.EqualsMarker(header.index)) header.index = null;
					if (request.Type != null && request.Type.EqualsMarker(header.type)) header.type = null;
				}
				var headerBytes = elasticsearchSerializer.SerializeToBytes(header, SerializationFormatting.None);
				writer.WriteRaw($"{{\"{percolation.MultiPercolateName}\":" + headerBytes.Utf8String() + "}\n");
				if (percolation == null)
				{
					writer.WriteRaw("{}\n");
				}
				else
				{
					var bodyBytes = elasticsearchSerializer.SerializeToBytes(percolation, SerializationFormatting.None);
					writer.WriteRaw(bodyBytes.Utf8String() + "\n");
				}
			}
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var dict = value as IDictionary<PropertyName, IProperty>;
			if (dict == null) return;
			var settings = serializer.GetConnectionSettings();
			var props = new Properties();
			foreach (var kv in dict)
			{
				var v = kv.Value as IPropertyWithClrOrigin;
				if (v?.ClrOrigin == null)
				{
					props.Add(kv.Key, kv.Value);
					continue;
				}
				//We do not have to take .Name into account from serializer PropertyName (kv.Key) already handles this
				var serializerMapping = settings.Serializer?.CreatePropertyMapping(v.ClrOrigin);
				if (serializerMapping == null || !serializerMapping.Ignore) 
					props.Add(kv.Key, kv.Value);
			}
			_dictionaryConverter.WriteJson(writer, props, serializer);
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var request = (IMultiSearchTemplateRequest)value;
			if (request == null) return;
			var settings = serializer.GetConnectionSettings();
			var elasticsearchSerializer = settings.Serializer;
			if (elasticsearchSerializer == null) return;

			if (request.Operations == null) return;

			foreach (var operation in request.Operations.Values)
			{
				IUrlParameter indices = request.Index == null || !request.Index.Equals(operation.Index)
					? operation.Index
					: null;

				IUrlParameter types = request.Type == null || !request.Type.Equals(operation.Type)
					? operation.Type
					: null;

				var searchType = operation.RequestParameters.GetQueryStringValue<SearchType>("search_type").GetStringValue();
				if (searchType == "query_then_fetch")
					searchType = null;

				var header = new
				{
					index = indices?.GetString(settings),
					type = types?.GetString(settings),
					search_type = searchType,
					preference = operation.Preference,
					routing = operation.Routing,
					ignore_unavailable = operation.IgnoreUnavalable
				};

				var headerString = elasticsearchSerializer.SerializeToString(header, SerializationFormatting.None);
				writer.WriteRaw($"{headerString}\n");
				var bodyString = elasticsearchSerializer.SerializeToString(operation, SerializationFormatting.None);
				writer.WriteRaw($"{bodyString}\n");
			};
		}
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var request = value as IMultiSearchRequest;
			if (request == null) return;
			var settings = serializer.GetConnectionSettings();
			var elasticsearchSerializer = settings.Serializer;
			if (elasticsearchSerializer == null) return;

			if (request.Operations == null) return;

			foreach (var operation in request.Operations.Values)
			{
				var indices = (request.Index != null && !request.Index.Equals(operation.Index))
					? operation.Index
					: null;

				var types = (request.Type != null && !request.Type.Equals(operation.Type))
					? operation.Type
					: null;

				var searchType = operation.RequestParameters.GetQueryStringValue<SearchType>("search_type").GetStringValue();
				if (searchType == "query_then_fetch")
					searchType = null;

				var header = new
				{
					index = (indices as IUrlParameter)?.GetString(settings),
					type = (types as IUrlParameter)?.GetString(settings),
					search_type = searchType,
					preference = operation.Preference,
					routing = operation.Routing,
					ignore_unavailable = operation.IgnoreUnavalable
				};

				var headerBytes = elasticsearchSerializer.SerializeToBytes(header, SerializationFormatting.None);
				writer.WriteRaw(headerBytes.Utf8String() + "\n");
				var bodyBytes = elasticsearchSerializer.SerializeToBytes(operation, SerializationFormatting.None);
				writer.WriteRaw(bodyBytes.Utf8String() + "\n");
			}
		}
		protected void WriteMetricProperties(IMetricAggregation metric, JsonWriter writer, JsonSerializer serializer)
		{

			if (metric.Field != null)
			{
				var settings = serializer.GetConnectionSettings();
				writer.WritePropertyName("field");
				writer.WriteValue(settings.Inferrer.Field(metric.Field));
			}
			
			if (metric.Script != null)
			{
				writer.WritePropertyName("script");
				serializer.Serialize(writer, metric.Script);
			}

			if (metric.Missing.HasValue)
			{
				writer.WritePropertyName("missing");
				writer.WriteValue(metric.Missing);
			}
		}
		private bool WriteDecay(JsonWriter writer, IDecayFunction decay, JsonSerializer serializer)
		{
			if (decay == null) return false;

			writer.WritePropertyName(decay.DecayType);
			writer.WriteStartObject();
			{
				writer.WritePropertyName(serializer.GetConnectionSettings().Inferrer.Field(decay.Field));
				writer.WriteStartObject();
				{
					var write = WriteNumericDecay(writer, decay as IDecayFunction<double?, double?>, serializer)
								|| WriteDateDecay(writer, decay as IDecayFunction<DateMath, Time>, serializer)
								|| WriteGeoDecay(writer, decay as IDecayFunction<GeoLocation, Distance>, serializer);
					if (!write) throw new Exception($"Can not write decay function json for {decay.GetType().Name}");

					if (decay.Decay.HasValue)
					{
						writer.WritePropertyName("decay");
						serializer.Serialize(writer, decay.Decay.Value);
					}
				}
				writer.WriteEndObject();
				if (decay.MultiValueMode.HasValue)
				{
					writer.WritePropertyName("multi_value_mode");
					serializer.Serialize(writer, decay.MultiValueMode.Value);
				}
			}
			writer.WriteEndObject();
			return true;

		}