Ejemplo n.º 1
0
        public QueryResponse <T> Query <T>(string query) where T : class
        {
            var index = this.Settings.DefaultIndex;

            index.ThrowIfNullOrEmpty("Cannot infer default index for current connection.");

            var    type     = typeof(T);
            var    typeName = Inflector.MakePlural(type.Name).ToLower();
            string path     = this.createPath(index, typeName) + "_search";

            var status = this.Connection.PostSync(path, query);

            if (status.Error != null)
            {
                return(new QueryResponse <T>()
                {
                    IsValid = false,
                    ConnectionError = status.Error
                });
            }

            var response = JsonConvert.DeserializeObject <QueryResponse <T> >(status.Result);

            return(response);
        }
Ejemplo n.º 2
0
        public T Get <T>(string id) where T : class
        {
            var index = this.Settings.DefaultIndex;

            index.ThrowIfNullOrEmpty("Cannot infer default index for current connection.");

            var type     = typeof(T);
            var typeName = Inflector.MakePlural(type.Name).ToLower();

            return(this.Get <T>(id, this.createPath(index, typeName)));
        }
Ejemplo n.º 3
0
        private string InferTypeName <T>() where T : class
        {
            var type     = typeof(T);
            var typeName = type.Name;

            if (this.Settings.TypeNameInferrer != null)
            {
                typeName = this.Settings.TypeNameInferrer(typeName);
            }
            if (this.Settings.TypeNameInferrer == null || string.IsNullOrEmpty(typeName))
            {
                typeName = Inflector.MakePlural(type.Name).ToLower();
            }
            return(typeName);
        }
Ejemplo n.º 4
0
        public ConnectionStatus Index <T>(T @object) where T : class
        {
            @object.ThrowIfNull("object");

            var index = this.Settings.DefaultIndex;

            if (string.IsNullOrEmpty(index))
            {
                throw new NullReferenceException("Cannot infer default index for current connection.");
            }

            var type     = typeof(T);
            var typeName = Inflector.MakePlural(type.Name).ToLower();
            var path     = this.createPath(index, typeName);

            var    idProperty = type.GetProperty("Id");
            int?   id         = null;
            string idString   = string.Empty;

            if (idProperty != null)
            {
                if (idProperty.PropertyType == typeof(int))
                {
                    id = (int?)@object.TryGetPropertyValue("Id");
                }
                if (idProperty.PropertyType == typeof(string))
                {
                    idString = (string)@object.TryGetPropertyValue("Id");
                }
                if (id.HasValue)
                {
                    idString = id.Value.ToString();
                }
                if (!string.IsNullOrEmpty(idString))
                {
                    path = this.createPath(index, typeName, idString);
                }
            }
            return(this.Index <T>(@object, path));
        }
Ejemplo n.º 5
0
        private string InferTypeName <T>() where T : class
        {
            var type     = typeof(T);
            var typeName = type.Name;
            var att      = this.PropertyNameResolver.GetElasticPropertyFor <T>();

            if (att != null && !att.Name.IsNullOrEmpty())
            {
                typeName = att.Name;
            }
            else
            {
                if (this.Settings.TypeNameInferrer != null)
                {
                    typeName = this.Settings.TypeNameInferrer(typeName);
                }
                if (this.Settings.TypeNameInferrer == null || string.IsNullOrEmpty(typeName))
                {
                    typeName = Inflector.MakePlural(type.Name).ToLower();
                }
            }
            return(typeName);
        }
Ejemplo n.º 6
0
        private string GenerateBulkCommand <T>(IEnumerable <T> @objects)
        {
            @objects.ThrowIfNull("objects");

            var index = this.Settings.DefaultIndex;

            if (string.IsNullOrEmpty(index))
            {
                throw new NullReferenceException("Cannot infer default index for current connection.");
            }

            if (@objects.Count() == 0)
            {
                return(null);
            }

            var type     = typeof(T);
            var typeName = Inflector.MakePlural(type.Name).ToLower();

            Func <T, string> idSelector = null;
            var idProperty = type.GetProperty("Id");

            if (idProperty != null)
            {
                if (idProperty.PropertyType == typeof(int))
                {
                    idSelector = (@object) => ((int)@object.TryGetPropertyValue("Id")).ToString();
                }
                else if (idProperty.PropertyType == typeof(int?))
                {
                    idSelector = (@object) =>
                    {
                        int?val = (int?)@object.TryGetPropertyValue("Id");
                        return((val.HasValue) ? val.Value.ToString() : string.Empty);
                    }
                }
                ;
                else if (idProperty.PropertyType == typeof(string))
                {
                    idSelector = (@object) => (string)@object.TryGetPropertyValue("Id");
                }
            }

            var sb      = new StringBuilder();
            var command = "{{ \"index\" : {{ \"_index\" : \"{0}\", \"_type\" : \"{1}\", \"_id\" : \"{2}\" }} }}\n";

            //if we can't reflect id let ES create one.
            if (idSelector == null)
            {
                command = "{{ \"index\" : {{ \"_index\" : \"{0}\", \"_type\" : \"{1}\" }} }}\n".F(index, typeName);
            }

            foreach (var @object in objects)
            {
                string jsonCommand = JsonConvert.SerializeObject(@object, Formatting.None, this.SerializationSettings);
                if (idSelector == null)
                {
                    sb.Append(command);
                }
                else
                {
                    sb.Append(command.F(index, typeName, idSelector(@object)));
                }
                sb.Append(jsonCommand + "\n");
            }
            var json = sb.ToString();

            return(json);
        }