public string GetTypeNameFor(Type type)
        {
            if (type == null)
            {
                return(null);
            }
            string typeName;

            if (_connectionSettings.DefaultTypeNames.TryGetValue(type, out typeName))
            {
                return(typeName);
            }

            var att = ElasticsearchTypeAttribute.From(type);

            if (att != null && !att.Name.IsNullOrEmpty())
            {
                typeName = att.Name;
            }
            else
            {
                typeName = _connectionSettings.DefaultTypeNameInferrer(type);
            }
            return(typeName);
        }
        /// <summary>
        /// 获取IdProperty的名称
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string GetIdPropertyName <TEntity>(TEntity entity)
        {
            ElasticsearchTypeAttribute elasticsearchType = ReflectionKit.GetClassCustomAttribute <ElasticsearchTypeAttribute>(typeof(TEntity));

            if (elasticsearchType != null)
            {
                return(elasticsearchType.IdProperty);
            }
            else
            {
                throw new Exception("");
            }
        }
Example #3
0
 protected virtual byte[] ReturnRequestBody(IEnumerable <object> datas, Type type)
 {
     return(ResponseBuilder.CreateSearchResponse(datas.ToList(), obj =>
     {
         object[] objs = type.GetCustomAttributes(typeof(ElasticsearchTypeAttribute), true);
         if (objs.Length > 0)
         {
             ElasticsearchTypeAttribute attr = objs[0] as ElasticsearchTypeAttribute;
             string id = type.GetProperty(attr.IdProperty).GetValue(obj).ToString();
             return id;
         }
         return "";
     }));
 }
        private PropertyInfo GetInferredId(Type type)
        {
            // if the type specifies through ElasticAttribute what the id prop is
            // use that no matter what

            string propertyName;

            this._connectionSettings.IdProperties.TryGetValue(type, out propertyName);
            if (!propertyName.IsNullOrEmpty())
            {
                return(GetPropertyCaseInsensitive(type, propertyName));
            }

            var esTypeAtt = ElasticsearchTypeAttribute.From(type);

            propertyName = (esTypeAtt?.IdProperty.IsNullOrEmpty() ?? true) ? "Id" : esTypeAtt?.IdProperty;

            return(GetPropertyCaseInsensitive(type, propertyName));
        }