/// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="index"></param>
        /// <param name="shards"></param>
        /// <returns></returns>
        public bool CreateIndex(string index, int shards = 5)
        {
            bool           flag   = false;
            StringResponse resStr = null;

            try
            {
                resStr = client.IndicesCreate <StringResponse>(index,
                                                               PostData.String($"{{\"settings\" : {{\"index\" : {{\"number_of_replicas\" : 0, \"number_of_shards\":\"{shards}\",\"refresh_interval\":\"-1\"}}}}}}"));
                var resObj = JObject.Parse(resStr.Body);
                if ((bool)resObj["acknowledged"])
                {
                    flag = true;
                }
                else
                {
                    LogUtil.LogInfo(logger, resStr.DebugInformation, nodeId);
                }
            }
            catch (Exception ex)
            {
                if (resStr != null)
                {
                    LogUtil.LogInfo(logger, resStr.DebugInformation, nodeId);
                }
                LogUtil.LogError(logger, ex.ToString(), nodeId);
            }

            return(flag);
        }
        public void CreateIndexWithMapping(string indexName, string typeName)
        {
            Type t = typeof(T);

            JObject jsonProperties = new JObject();

            foreach (var m in t.GetProperties())
            {
                JObject prop = new JObject();

                JProperty propFieldType;
                if (m.GetCustomAttributes(typeof(FieldType), true).Length > 0)
                {
                    propFieldType = new JProperty("type", ((FieldType)m.GetCustomAttributes(typeof(FieldType), true)[0]).ConvertFieldTypeToElasticFieldType());
                }
                else
                {
                    propFieldType = new JProperty("type", "string");
                }
                prop.Add(propFieldType);

                JProperty propNotAnalyzed;
                if (m.GetCustomAttributes(typeof(NotAnalyzed), true).Length > 0)
                {
                    propNotAnalyzed = new JProperty("index", "not_analyzed");
                    prop.Add(propNotAnalyzed);
                }

                JProperty propAnalyzer;
                if (m.GetCustomAttributes(typeof(Analyzer), true).Length > 0)
                {
                    propAnalyzer = new JProperty("analyzer", ((Analyzer)m.GetCustomAttributes(typeof(Analyzer), true)[0]).ConvertAnalyzerTypeToElasticAnalyzer());
                    prop.Add(propAnalyzer);
                }

                jsonProperties.Add(m.Name, prop);
            }

            JObject jsonType = new JObject();

            jsonType.Add("properties", jsonProperties);

            JObject jsonMappings = new JObject();

            jsonMappings.Add(typeName, jsonType);

            JObject jsonQuery = new JObject();

            jsonQuery.Add("mappings", jsonMappings);

            string            json         = JsonConvert.SerializeObject(jsonQuery);
            PostData <object> jsonPostData = new PostData <object>(json);
            var results = _elasticsearchClient.IndicesCreate <object>(indexName, jsonPostData);
        }
        /// <summary>
        /// Create index in elasticsearch.
        /// </summary>
        /// <param name="indexName">Index name.</param>
        protected void CreateIndex(String indexName)
        {
            ElasticsearchResponse <DynamicResponse> response;

            //String locale, settings;

            //            locale = "{\"index\" : { \"analysis\" : { \"analyzer\" : {" +
            //                     "\"collation\" : { \"tokenizer\" : \"icu_tokenizer\", " +
            //                     "\"filter\" : [\"SwedishCollator\"] }}, " +
            //                     "\"filter\" : { \"SwedishCollator\" : { " +
            //                     "\"type\" : \"lowercase\", \"country\" : \"SE\", \"language\" : \"sv\" }}}}}";
            ////            "\"type\" : \"icu_collation\", \"country\" : \"SE\", \"language\" : \"sv\" }}}}}";
            //            settings = "{\"settings\" : " +
            //                            "{\"index\" : {\"analysis\" : " +
            //                                "{\"filter\" : {\"SwedishCollation\": {\"type\" : \"icu_collation\", \"country\" : \"SE\", \"language\" : \"sv\"}}," +
            //                                " \"analyzer\" : {\"SwedishAnalyzer\" : {\"type\" : \"custom\", \"tokenizer\" : \"keyword\", \"filter\" : \"SwedishCollation\"}}}}}, " +
            //                       "\"mappings\" : {\"_default_\": {\"properties\" : {\"name\" : {\"type\" : \"string\", \"analyzer\" : \"SwedishAnalyzer\"}}}}}";
            response = _client.IndicesCreate <DynamicResponse>(indexName, null);
            CheckResponse(response);
            //        response = mClient.IndicesPutSettings(indexName, locale);
            //response = mClient.IndicesPutAlias(indexName, IndexName, null);
            //CheckResponse(response);
        }
Exemple #4
0
        public static void InsertIntoES(string csv_filename, ElasticLowLevelClient lowLevelClient, bool isAsync, int bulkLimit, int shardCount, int replicaCount)
        {
            var idelResponse = lowLevelClient.IndicesDelete <BytesResponse>("people");

            var icrResponse = lowLevelClient.IndicesCreate <BytesResponse>("people", PostData.Serializable(
                                                                               new
            {
                settings = new
                {
                    number_of_shards   = shardCount,
                    number_of_replicas = replicaCount
                },
                mappings = new
                {
                    _doc = new
                    {
                        properties = new
                        {
                            FirstName      = new { type = "text" },
                            LastName       = new { type = "text" },
                            UserName       = new { type = "text" },
                            SSC_Grade      = new { type = "text" },
                            HSC_Grade      = new { type = "text" },
                            Bachelor_Grade = new { type = "text" },
                            Age            = new { type = "integer" },
                            Gender         = new { type = "text" },
                            Email          = new { type = "text" },
                            DateOfBirth    = new { type = "date" },
                            Streent        = new { type = "text" },
                            Suite          = new { type = "text" },
                            City           = new { type = "text" },
                            ZipCode        = new { type = "text" },
                        }
                    }
                }
            }
                                                                               ));

            var people = new List <object>
            {
                new { index = new { _index = "people", _type = "_doc" } }
            };


            int counter = 0;
            int id      = 0;


            using (StreamReader reader = new StreamReader(csv_filename))
            {
                reader.ReadLine();
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    var values = line.Split(",");

                    if (id == 0)
                    {
                        people = new List <object>
                        {
                        };
                    }

                    if (counter >= bulkLimit)
                    {
                        if (isAsync)
                        {
                            lowLevelClient.BulkAsync <StringResponse>(PostData.MultiJson(people));
                        }
                        else
                        {
                            lowLevelClient.Bulk <StringResponse>(PostData.MultiJson(people));
                        }

                        counter = 0;
                        people  = new List <object>
                        {
                        };
                    }
                    else
                    {
                        people.Add(new { index = new { _index = "people", _type = "_doc", _id = $"{id}" } });

                        people.Add(
                            new
                        {
                            FirstName      = values[0],
                            LastName       = values[1],
                            UserName       = values[2],
                            SSC_Grade      = values[3],
                            HSC_Grade      = values[4],
                            Bachelor_Grade = values[5],
                            Age            = Int32.Parse(values[6]),
                            Gender         = values[7],
                            Email          = values[8],
                            DateOfBirth    = DateTime.Parse(values[9]),
                            Street         = values[10],
                            Suite          = values[11],
                            City           = values[12],
                            ZipCode        = values[13],
                        });
                        counter++;
                        id++;
                    }


                    Console.WriteLine(id);
                }


                if (isAsync)
                {
                    lowLevelClient.BulkAsync <StringResponse>(PostData.MultiJson(people));
                }
                else
                {
                    lowLevelClient.Bulk <StringResponse>(PostData.MultiJson(people));
                }
            }
        }