/// <inheritdoc/> protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { if (member == null) { throw new ArgumentNullException(nameof(member)); } var jp = base.CreateProperty(member, memberSerialization); var bti = BYteWareTypeInfo.GetBYteWareTypeInfo(member.DeclaringType); var props = bti.ESProperties(member.Name); jp.PropertyName = ElasticSearchClient.FieldName(string.IsNullOrEmpty(props?.FieldName) ? member.Name : props.FieldName); var defType = ElasticSearchClient.GetFieldTypeFromType(member.Type()); // Omit empty values, because Suggest Fields will raise errors otherwise if (defType == FieldType.text || defType == FieldType.keyword) { if (typeof(IEnumerable).IsAssignableFrom(member.Type())) { jp.ShouldSerialize = t => (member.Get(t) as IEnumerable)?.Cast <object>().Any() ?? false; } else { jp.ShouldSerialize = t => !string.IsNullOrEmpty(member.Get(t)?.ToString()); } } if (!string.IsNullOrEmpty(props.WeightField)) { jp.PropertyType = typeof(object); jp.ValueProvider = new SuggestWeightFieldValueProvider(member, jp.PropertyName); } return(jp); }
private IEnumerable <string> ESFieldsInternal(bool wildcards) { foreach (var p in GetTopPropertyInfos) { var props = ESProperties(p.Name); var propertyName = ElasticSearchClient.FieldName(string.IsNullOrEmpty(props?.FieldName) ? p.Name : props.FieldName); if (props != null && !props.OptOut) { var etype = props.FieldType ?? ElasticSearchClient.GetFieldTypeFromType(p.PropertyType); foreach (var item in ElasticSearchFieldsInternal(p, propertyName, etype, wildcards)) { yield return(item); } var modelESField = props as IModelMemberElasticSearchField; var multiFields = modelESField != null ? modelESField.Fields : Attribute.GetCustomAttributes(p, typeof(ElasticMultiFieldAttribute), true).OfType <IElasticSearchFieldProperties>(); foreach (var ga in multiFields.GroupBy(t => t.FieldName)) { var a = ga.First(); foreach (var item in ElasticSearchFieldsInternal(p, a.FieldName.ToLowerInvariant(), a.FieldType ?? ElasticSearchClient.GetFieldTypeFromType(p.PropertyType), wildcards)) { yield return(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", propertyName, item)); } } } } }
/// <summary> /// Returns the name of the WeightFieldMember /// </summary> /// <param name="esField">IModelMemberElasticSearchField instance</param> /// <param name="value">FieldType value</param> public static void Set_FieldType(IModelMemberElasticSearchField esField, FieldType?value) { if (esField != null) { if (value.HasValue && esField.Parent is IModelMember member && string.IsNullOrEmpty(esField.FieldName)) { esField.FieldName = ElasticSearchClient.FieldName(member.Name); } ((ModelNode)esField).SetValue <FieldType?>(nameof(IModelMemberElasticSearchField.FieldType), value); } }
private static void MapFieldProperties(ElasticAttribute pa, IElasticSearchFieldProperties field, IModelMember member) { MapInterfaceProperties <IElasticSearchFieldProperties>(pa, field); if (string.IsNullOrWhiteSpace(field.FieldName)) { field.FieldName = ElasticSearchClient.FieldName(member.Name); } if (!field.FieldType.HasValue) { field.FieldType = ElasticSearchClient.GetFieldTypeFromType(member.Type); } }
private void AddSuggestContextPathFieldInfo(SuggestField sf) { foreach (var context in sf.ContextSettings.Where(cs => !string.IsNullOrWhiteSpace(cs.PathField))) { var mi = ClassInfo.FindMember(context.PathField); var cpi = _ContextPathInfos.FirstOrDefault(i => i.PathMemberName.Equals(context.PathField) && i.MemberInfo == mi); if (cpi == null) { cpi = new SuggestContextPathFieldInfo { PathMemberName = context.PathField, ESFieldName = ElasticSearchClient.FieldName(context.PathField), MemberInfo = mi, IsIndexed = IsMemberESIndexed(context.PathField), }; _ContextPathInfos.Add(cpi); } context.ContextPathFieldInfo = cpi; } }
internal void WriteProperties(JsonWriter jsonWriter) { foreach (var p in byteWareTypeInfo.GetTopPropertyInfos) { var props = byteWareTypeInfo.ESProperties(p.Name); var type = ElasticSearchClient.GetElasticSearchType(props, p.PropertyType); var propertyName = ElasticSearchClient.FieldName(string.IsNullOrEmpty(props?.FieldName) ? p.Name : props.FieldName); if ((props != null && props.OptOut) || (props == null && OptOut) || (type == null)) { continue; } jsonWriter.WritePropertyName(propertyName); jsonWriter.WriteStartObject(); { if (props == null) { // properties that follow can not be inferred from the CLR. #pragma warning disable CC0021 // Use nameof jsonWriter.WritePropertyName("type"); #pragma warning restore CC0021 // Use nameof jsonWriter.WriteValue(type); } else { WriteProperties(jsonWriter, byteWareTypeInfo, p, props, propertyName, type); var multiFields = BYteWareTypeInfo.Model != null ? (props as IModelMemberElasticSearchField)?.Fields : Attribute.GetCustomAttributes(p, typeof(ElasticMultiFieldAttribute), true).OfType <IElasticSearchFieldProperties>(); if (multiFields != null && multiFields.Any()) { jsonWriter.WritePropertyName("fields"); jsonWriter.WriteStartObject(); foreach (var ga in multiFields.GroupBy(t => t.FieldName)) { var a = ga.First(); var fieldName = ElasticSearchClient.FieldName(ga.Key); jsonWriter.WritePropertyName(fieldName); jsonWriter.WriteStartObject(); WriteProperties(jsonWriter, byteWareTypeInfo, p, a, string.Format(CultureInfo.InvariantCulture, "{0}.{1}", propertyName, fieldName), ElasticSearchClient.GetElasticSearchType(a, p.PropertyType)); jsonWriter.WriteEndObject(); } jsonWriter.WriteEndObject(); } } if (type == "object" || type == "nested") { var deepType = p.PropertyType; var dbti = BYteWareTypeInfo.GetBYteWareTypeInfo(BYteWareTypeInfo.GetUnderlyingType(deepType)); var seenTypes = new ConcurrentDictionary <Type, int>(SeenTypes); seenTypes.AddOrUpdate(deepType, 0, (t, i) => ++ i); var newTypeMappingWriter = new TypeMappingWriter(elasticSearchClient, dbti, MaxRecursion, OptOut, baseBWTypeInfo, seenTypes); var nestedProperties = newTypeMappingWriter.MapProperties(); jsonWriter.WritePropertyName("properties"); nestedProperties.WriteTo(jsonWriter); foreach (var pathField in byteWareTypeInfo.ESSuggestContextPathFields.Where(pi => dbti.ClassInfo.Members.Contains(pi.MemberInfo))) { WritePathField(jsonWriter, pathField); } } } jsonWriter.WriteEnd(); } }
protected override string ResolvePropertyName(string propertyName) { return(ElasticSearchClient.FieldName(propertyName)); }