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)); } } } } }
/// <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 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 static void WriteProperties(JsonWriter jsonWriter, BYteWareTypeInfo bti, PropertyInfo propInfo, IElasticSearchFieldProperties props, string fieldName, string type) { #pragma warning disable CC0021 // Use nameof var fieldProps = props as IElasticProperties; jsonWriter.WritePropertyName("type"); jsonWriter.WriteValue(type); if (!string.IsNullOrEmpty(props.Normalizer)) { jsonWriter.WritePropertyName("normalizer"); jsonWriter.WriteValue(props.Normalizer); } else if (type == ElasticSearchClient.GetElasticSearchTypeFromFieldType(FieldType.keyword)) { jsonWriter.WritePropertyName("normalizer"); jsonWriter.WriteValue("lowercase_normalizer"); } if (!string.IsNullOrEmpty(props.Analyzer)) { jsonWriter.WritePropertyName("analyzer"); jsonWriter.WriteValue(props.Analyzer); } if (props.Boost.HasValue) { jsonWriter.WritePropertyName("boost"); jsonWriter.WriteValue(props.Boost.Value); } if (props.DocValues.HasValue) { jsonWriter.WritePropertyName("doc_values"); jsonWriter.WriteValue(props.DocValues.Value); } if (props.EagerGlobalOrdinals.HasValue) { jsonWriter.WritePropertyName("eager_global_ordinals"); jsonWriter.WriteValue(props.EagerGlobalOrdinals.Value); } if (props.FieldData.HasValue) { jsonWriter.WritePropertyName("fielddata"); jsonWriter.WriteValue(props.FieldData.Value); } if (props.ESIndex.HasValue) { jsonWriter.WritePropertyName("index"); jsonWriter.WriteValue(props.ESIndex.Value); } if (props.IndexOptions.HasValue) { jsonWriter.WritePropertyName("index_options"); jsonWriter.WriteValue(Enum.GetName(typeof(IndexOptions), props.IndexOptions.Value)); } if (props.Norms.HasValue) { jsonWriter.WritePropertyName("norms"); jsonWriter.WriteValue(props.Norms.Value); } if (props.PositionOffsetGap.HasValue) { jsonWriter.WritePropertyName("position_offset_gap"); jsonWriter.WriteValue(props.PositionOffsetGap.Value); } if (props.Store.HasValue) { jsonWriter.WritePropertyName("store"); jsonWriter.WriteValue(props.Store.Value); } if (!string.IsNullOrEmpty(props.SearchAnalyzer)) { jsonWriter.WritePropertyName("search_analyzer"); jsonWriter.WriteValue(props.SearchAnalyzer); } if (!string.IsNullOrEmpty(props.SearchQuoteAnalyzer)) { jsonWriter.WritePropertyName("search_quote_analyzer"); jsonWriter.WriteValue(props.SearchQuoteAnalyzer); } if (!string.IsNullOrEmpty(props.Similarity)) { jsonWriter.WritePropertyName("similarity"); jsonWriter.WriteValue(props.Similarity); } if (props.TermVector.HasValue) { jsonWriter.WritePropertyName("term_vector"); jsonWriter.WriteValue(Enum.GetName(typeof(TermVectorOption), props.TermVector.Value)); } if (props.IgnoreAbove.HasValue) { jsonWriter.WritePropertyName("ignore_above"); jsonWriter.WriteValue(props.IgnoreAbove.Value); } if (!string.IsNullOrEmpty(props.NullValue)) { jsonWriter.WritePropertyName("null_value"); jsonWriter.WriteValue(props.NullValue); } if (props.Coerce.HasValue) { jsonWriter.WritePropertyName("coerce"); jsonWriter.WriteValue(props.Coerce.Value); } if (props.IgnoreMalformed.HasValue) { jsonWriter.WritePropertyName("ignore_malformed"); jsonWriter.WriteValue(props.IgnoreMalformed.Value); } if (props.ScalingFactor.HasValue) { jsonWriter.WritePropertyName("scaling_factor"); jsonWriter.WriteValue(props.ScalingFactor.Value); } if (!string.IsNullOrEmpty(props.DateFormat)) { jsonWriter.WritePropertyName("format"); jsonWriter.WriteValue(props.DateFormat); } if (!string.IsNullOrEmpty(props.Locale)) { jsonWriter.WritePropertyName("locale"); jsonWriter.WriteValue(props.Locale); } if (fieldProps != null && fieldProps.IncludeInAll.HasValue) { jsonWriter.WritePropertyName("include_in_all"); jsonWriter.WriteValue(fieldProps.IncludeInAll.Value); } if (props.PreserveSeparators.HasValue) { jsonWriter.WritePropertyName("preserve_separators"); jsonWriter.WriteValue(props.PreserveSeparators.Value); } if (props.PreservePositionIncrements.HasValue) { jsonWriter.WritePropertyName("preserve_position_increments"); jsonWriter.WriteValue(props.PreservePositionIncrements.Value); } if (props.MaxInputLength.HasValue) { jsonWriter.WritePropertyName("max_input_length"); jsonWriter.WriteValue(props.MaxInputLength.Value); } if (fieldProps != null && !string.IsNullOrEmpty(fieldProps.CopyTo)) { jsonWriter.WritePropertyName("copy_to"); jsonWriter.WriteStartArray(); foreach (var item in fieldProps.CopyTo.Split(";, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { jsonWriter.WriteValue(item); } jsonWriter.WriteEndArray(); } if (type == ElasticSearchClient.GetElasticSearchTypeFromFieldType(FieldType.completion)) { var defType = ElasticSearchClient.GetFieldTypeFromType(propInfo.PropertyType); if (defType != FieldType.text && defType != FieldType.keyword) { throw new ElasticIndexException(CaptionHelper.GetLocalizedText(ElasticSearchClient.IndexExceptionGroup, "SuggestNoString")); } var sf = bti.ESSuggestFields.First(t => t.FieldName == fieldName); if (sf.ContextSettings.Any()) { jsonWriter.WritePropertyName("contexts"); jsonWriter.WriteStartArray(); foreach (var ca in sf.ContextSettings) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("name"); jsonWriter.WriteValue(ca.ContextName); jsonWriter.WritePropertyName("type"); jsonWriter.WriteValue(Enum.GetName(typeof(SuggestContextType), ca.ContextType)); if (ca.ContextPathFieldInfo != null) { jsonWriter.WritePropertyName("path"); jsonWriter.WriteValue(ca.ContextPathFieldInfo.ESFieldName); } if (!string.IsNullOrEmpty(ca.Precision)) { jsonWriter.WritePropertyName("precision"); jsonWriter.WriteValue(ca.Precision); } jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); } } #pragma warning restore CC0021 // Use nameof }