예제 #1
0
 /// <summary>
 /// Create an index with the specified index settings
 /// </summary>
 public IIndicesOperationResponse CreateIndex(string index, IndexSettings settings)
 {
     string data = this.Serialize(settings);
     string path = this.PathResolver.CreateIndexPath(index);
     var status = this.Connection.PostSync(path, data);
     return this.Deserialize<IndicesOperationResponse>(status);
 }
예제 #2
0
        private static void CreateEmployeeIndex()
        {
            var settings = new IndexSettings();
              var typeMapping = new TypeMapping("employee");
              var stringNotAnalyzed = new TypeMappingProperty
              {
            Type = "string",
            Index = "not_analyzed"
              };
              //ElasticSearch camel-cases field names
              typeMapping.Properties = new Dictionary<string, TypeMappingProperty>();
              typeMapping.Properties.Add("id", stringNotAnalyzed);
              typeMapping.Properties.Add("companyId", stringNotAnalyzed);

              settings.Mappings.Add(typeMapping);

              settings.NumberOfReplicas = 1;
              settings.NumberOfShards = 5;

              //default analyzer is Standard

              var result = esClient.CreateIndex("employees", settings);
              if (!result.OK)
              {
            Log("Unable to create and configure employees ElasticSearch index");
            return;
              }
              Log("Employees index created");
        }
예제 #3
0
 public static int CresteIndex(string indexname, int replicas, int shards,IndexSettings settings)
 {
     int count = 0;
     settings.NumberOfReplicas = replicas;
     settings.NumberOfShards = shards;
     Client().CreateIndex(c => c
         .Index(indexname)
         .InitializeUsing(settings)
         );
     return count;
 }
        public static void CreateEmployeeIndex()
        {
            var settings = new IndexSettings {NumberOfReplicas = 1, NumberOfShards = 5};

            var result = ElasticSearchClient.CreateIndex(IndexName, settings);
            if (!result.OK)
            {
                Log("Unable to create and configure employees ElasticSearch index");
                return;
            }
            Log("Employees index created");
        }
예제 #5
0
 public static int CresteIndex(string indexname)
 {
     int count = 0;
     var settings = new IndexSettings();
     settings.NumberOfReplicas = 1;
     settings.NumberOfShards = 2;
     Client().CreateIndex(c => c
         .Index(indexname)
         .InitializeUsing(settings)
         );
     return count;
 }
예제 #6
0
파일: IndicesTests.cs 프로젝트: occ/NEST
        public void CreateIndexMultiFieldMap()
        {
            var client = this.ConnectedClient;

            var typeMapping = new TypeMapping(Guid.NewGuid().ToString("n"));
            var property = new TypeMappingProperty
                           {
                               Type = "multi_field"
                           };

            var primaryField = new TypeMappingProperty
                               {
                                   Type = "string",
                                   Index = "not_analyzed"
                               };

            var analyzedField = new TypeMappingProperty
                                {
                                    Type = "string",
                                    Index = "analyzed"
                                };

            property.Fields = new Dictionary<string, TypeMappingProperty>();
            property.Fields.Add("name", primaryField);
            property.Fields.Add("name_analyzed", analyzedField);

            typeMapping.Properties.Add("name", property);

            var settings = new IndexSettings();
            settings.Mappings.Add(typeMapping);
            settings.NumberOfReplicas = 1;
            settings.NumberOfShards = 5;
            settings.Analysis.Analyzer.Add("snowball", new SnowballAnalyzerSettings { Language = "English" });

            var indexName = Guid.NewGuid().ToString();
            var response = client.CreateIndex(indexName, settings);

            Assert.IsTrue(response.IsValid);
            Assert.IsTrue(response.OK);

            Assert.IsNotNull(this.ConnectedClient.GetMapping(indexName, typeMapping.Name));

            response = client.DeleteIndex(indexName);

            Assert.IsTrue(response.IsValid);
            Assert.IsTrue(response.OK);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var s = new IndexSettings();

            SetKnownIndexSettings(reader, serializer, s);
            if (!typeof(IUpdateIndexSettingsRequest).IsAssignableFrom(objectType))
            {
                return(s);
            }

            var request = new UpdateIndexSettingsRequest()
            {
                IndexSettings = s
            };

            return(request);
        }
예제 #8
0
        public IndicesResponse CreateIndex(string index, IndexSettings settings)
        {
            string path     = this.CreatePath(index);
            string data     = JsonConvert.SerializeObject(settings, Formatting.None, SerializationSettings);
            var    status   = this.Connection.PostSync(path, data);
            var    response = new IndicesResponse();

            response.ConnectionStatus = status;
            try
            {
                response         = JsonConvert.DeserializeObject <IndicesResponse>(status.Result);
                response.IsValid = true;
            }
            catch (Exception e) {
            }
            return(response);
        }
예제 #9
0
        /// <summary>
        /// Update the index settings for the specified index
        /// </summary>
        public SettingsOperationResponse UpdateSettings(string index, IndexSettings settings)
        {
            string path = this.CreatePath(index) + "_settings";

            settings.Settings = settings.Settings
                                .Where(kv => IndexSettings.UpdateWhiteList.Any(p =>
            {
                return(kv.Key.StartsWith(p));
            }
                                                                               )).ToDictionary(kv => kv.Key, kv => kv.Value);

            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("index");
                jsonWriter.WriteStartObject();
                foreach (var kv in settings.Settings)
                {
                    jsonWriter.WritePropertyName(kv.Key);
                    jsonWriter.WriteValue(kv.Value);
                }

                jsonWriter.WriteEndObject();
            }
            string data = sb.ToString();

            var status = this.Connection.PutSync(path, data);

            var r = new SettingsOperationResponse();

            try
            {
                r = JsonConvert.DeserializeObject <SettingsOperationResponse>(status.Result);
            }
            catch
            {
            }
            r.IsValid          = status.Success;
            r.ConnectionStatus = status;
            return(r);
        }
 public ActionResult CreateIndex()
 {
     ConnectionStatus connectionStatus;
     if (ElasticClient.TryConnect(out connectionStatus))
     {
         if (ElasticClient.IndexExists(IndexName).Exists)
             ElasticClient.DeleteIndex(IndexName);
         var indexSettings = new IndexSettings();
         //indexSettings.Analysis.Analyzers.Add("analyzer", new LanguageAnalyzer(Language.Swedish));
         //indexSettings.Analysis.Tokenizers.Add("tokenizer", new EdgeNGramTokenizer());
         ElasticClient.CreateIndex(IndexName, indexSettings);
         var products = new List<Product>
             {
                 new Product {Id = 1, Name = "Alfa", Content = "lorem ipsum doremi fasoleti"},
                 new Product {Id = 2, Name = "Beta", Content = "one ring to rule them all"},
                 new Product {Id = 3, Name = "Gamma", Content = "Lunch & Learn är sjukt spännande och roligt"}
             };
         ElasticClient.IndexMany(products, IndexName);
     }
     return RedirectToAction("Index");
 }
        /// <summary>
        /// Update the index settings for the specified index
        /// </summary>
        public ISettingsOperationResponse UpdateSettings(string index, IndexSettings settings)
        {
            string path = this.PathResolver.CreateIndexPath(index, "_settings");

            settings.Settings = settings.Settings
                                .Where(kv => IndexSettings.UpdateWhiteList.Any(p => kv.Key.StartsWith(p))).ToDictionary(kv => kv.Key, kv => kv.Value);

            string data   = this.Serializer.Serialize(settings, Formatting.None);
            var    status = this.Connection.PutSync(path, data);

            var r = new SettingsOperationResponse();

            try
            {
                r = this.Deserialize <SettingsOperationResponse>(status.Result);
            }
            catch
            {
            }
            r.IsValid          = status.Success;
            r.ConnectionStatus = status;
            return(r);
        }
        public HttpResponseMessage CreateIndex(FormDataCollection postData)
        {
            SampleConfiguration config = new SampleConfiguration();
            esNode = new Uri(config.ElasticsearchServerHost);
            string indexName = postData.Get("indexName");
            connectionSettings = new ConnectionSettings(esNode, defaultIndex: indexName);

            var indexSettings = new IndexSettings();
            indexSettings.NumberOfShards = 1;
            indexSettings.NumberOfReplicas = 0;
            esClient = new ElasticClient(connectionSettings);

            IIndicesOperationResponse indexOperationResponse = esClient.CreateIndex(c => c
                .Index(indexName)
                .InitializeUsing(indexSettings)
                .AddMapping<Product>(m => m.MapFromAttributes())
                );

            if (indexOperationResponse.Acknowledged == true)
                return Request.CreateResponse(HttpStatusCode.Created, indexOperationResponse.Acknowledged);
            else
                return Request.CreateResponse(HttpStatusCode.InternalServerError, indexOperationResponse.Acknowledged);
        }
예제 #13
0
파일: IndicesTests.cs 프로젝트: occ/NEST
        public void CreateIndex()
        {
            var client = this.ConnectedClient;
            var typeMapping = this.ConnectedClient.GetMapping(Test.Default.DefaultIndex, "elasticsearchprojects");
            typeMapping.Name = "mytype";
            var settings = new IndexSettings();
            settings.Mappings.Add(typeMapping);
            settings.NumberOfReplicas = 1;
            settings.NumberOfShards = 5;
            settings.Analysis.Analyzer.Add("snowball", new SnowballAnalyzerSettings { Language = "English" });

            var indexName = Guid.NewGuid().ToString();
            var response = client.CreateIndex(indexName, settings);

            Assert.IsTrue(response.IsValid);
            Assert.IsTrue(response.OK);

            Assert.IsNotNull(this.ConnectedClient.GetMapping(indexName, "mytype"));

            response = client.DeleteIndex(indexName);

            Assert.IsTrue(response.IsValid);
            Assert.IsTrue(response.OK);
        }
        /// <summary>
        /// Update the index settings for the default index
        /// </summary>
        public ISettingsOperationResponse UpdateSettings(IndexSettings settings)
        {
            var index = this._connectionSettings.DefaultIndex;

            return(this.UpdateSettings(index, settings));
        }
예제 #15
0
 /// <summary>
 /// Create an index with the specified index settings
 /// </summary>
 public IIndicesResponse CreateIndex(string index, IndexSettings settings)
 {
     string data = JsonConvert.SerializeObject(settings, Formatting.None, SerializationSettings);
     return CreateIndexRaw(index, data);
 }
예제 #16
0
 /// <summary>
 /// Create an index with the specified index settings
 /// </summary>
 public IIndicesOperationResponse CreateIndex(string index, IndexSettings settings)
 {
     string data = this.Serialize(settings);
     return CreateIndexRaw(index, data);
 }
예제 #17
0
        /// <summary>
        /// Create an index with the specified index settings
        /// </summary>
        public IIndicesOperationResponse CreateIndex(string index, IndexSettings settings)
        {
            string data = JsonConvert.SerializeObject(settings, Formatting.None, SerializationSettings);

            return(CreateIndexRaw(index, data));
        }
예제 #18
0
파일: IndicesTests.cs 프로젝트: occ/NEST
        public void UpdateSettingsSimple()
        {
            var index = Guid.NewGuid().ToString();
            var client = this.ConnectedClient;
            var settings = new IndexSettings();
            settings.NumberOfReplicas = 1;
            settings.NumberOfShards = 5;
            settings.Add("refresh_interval", "1s");
            settings.Add("search.slowlog.threshold.fetch.warn", "1s");
            client.CreateIndex(index, settings);

            settings["refresh_interval"] = "-1";
            settings["search.slowlog.threshold.fetch.warn"] = "5s";

            var r = this.ConnectedClient.UpdateSettings(index, settings);

            Assert.True(r.IsValid);
            Assert.True(r.OK);
            var getResponse = this.ConnectedClient.GetIndexSettings(index);
            Assert.AreEqual(getResponse.Settings["refresh_interval"], "-1");
            Assert.AreEqual(getResponse.Settings["search.slowlog.threshold.fetch.warn"], "1s");

            this.ConnectedClient.DeleteIndex(index);
        }
예제 #19
0
파일: IndicesTests.cs 프로젝트: occ/NEST
        public void GetIndexSettingsComplex()
        {
            var index = Guid.NewGuid().ToString();
            var settings = new IndexSettings();
            settings.NumberOfReplicas = 4;
            settings.NumberOfShards = 8;
            settings.Analysis.Analyzer.Add("snowball", new SnowballAnalyzerSettings { Language = "English" });
            var typeMapping = this.ConnectedClient.GetMapping(Test.Default.DefaultIndex, "elasticsearchprojects");
            typeMapping.Name = index;
            settings.Mappings.Add(typeMapping);

            settings.Add("merge.policy.merge_factor","10");

            var createResponse = this.ConnectedClient.CreateIndex(index, settings);

            var r = this.ConnectedClient.GetIndexSettings(index);
            Assert.True(r.IsValid);
            Assert.NotNull(r.Settings);
            Assert.AreEqual(r.Settings.NumberOfReplicas, 4);
            Assert.AreEqual(r.Settings.NumberOfShards, 8);
            Assert.Greater(r.Settings.Count(), 0);
            Assert.True(r.Settings.ContainsKey("merge.policy.merge_factor"));

            this.ConnectedClient.DeleteIndex(index);
        }
        private async Task EnsureIndexExists(string currentIndexName, ElasticClient esClient)
        {
            IExistsResponse existsResult = await esClient.IndexExistsAsync(currentIndexName);
            if (!existsResult.IsValid)
            {
                this.ReportEsRequestError(existsResult, "Index exists check");
            }

            if (existsResult.Exists)
            {
                return;
            }

            // TODO: allow the consumer to fine-tune index settings
            IndexSettings indexSettings = new IndexSettings();
            indexSettings.NumberOfReplicas = 1;
            indexSettings.NumberOfShards = 5;
            indexSettings.Settings.Add("refresh_interval", "15s");

            IIndicesOperationResponse createIndexResult = await esClient.CreateIndexAsync(c => c.Index(currentIndexName).InitializeUsing(indexSettings));

            if (!createIndexResult.IsValid)
            {
                if (createIndexResult.ServerError != null &&
                    string.Equals(createIndexResult.ServerError.ExceptionType, "IndexAlreadyExistsException", StringComparison.OrdinalIgnoreCase))
                {
                    // This is fine, someone just beat us to create a new index.
                    return;
                }

                this.ReportEsRequestError(createIndexResult, "Create index");
            }
        }
예제 #21
0
 /// <summary>
 /// Initialize the descriptor using the values from for instance a previous Get Index Settings call.
 /// </summary>
 public CreateIndexDescriptor InitializeUsing(IndexSettings indexSettings)
 {
     this._indexSettings = indexSettings;
     return(this);
 }
예제 #22
0
        /// <summary>
        /// Create an index with the specified index settings
        /// </summary>
        public IIndicesOperationResponse CreateIndex(string index, IndexSettings settings)
        {
            string data = this.Serialize(settings);

            return(CreateIndexRaw(index, data));
        }
        /// <summary>
        /// Use this function to create a new Elasticsearch index
        /// </summary>
        /// <param name="C_INDEXNAME"></param>
        public bool CreateLibraryIndex()
        {
            try
            {
                var tCreateIndex = Task.Factory.StartNew(() =>
                {
                    var esIndexSettings = new IndexSettings();
                    esIndexSettings.NumberOfShards = 1;
                    esIndexSettings.NumberOfReplicas = 1;

                    return esClient.CreateIndex(c => c.Index(C_INDEXNAME)
                                                .InitializeUsing(esIndexSettings)
                                                .AddMapping<Book>(m => m.MapFromAttributes())
                                        );

                });

                var result = tCreateIndex.Result;

                return result.Acknowledged;
            }
            catch (AggregateException ae)
            {
                StringBuilder exceptions = new StringBuilder();

                foreach (Exception e in ae.Flatten().InnerExceptions)
                {
                    exceptions.Append(String.Format("\n Error creating index. Message: {0}", e.Message));
                }

                throw new ApplicationException(String.Format("Error creating index. Message: {0}", exceptions.ToString()));
            }
            catch (Exception ex)
            {
                throw new ApplicationException(String.Format("Error creating index. Message: {0}", ex.Message));
            }
        }
예제 #24
0
파일: Program.cs 프로젝트: tzkwizard/ELS
        public void CreateIndex(string name, string elUri)
        {



            Node = new Uri(elUri);
            var connectionPool = new SniffingConnectionPool(new[] { Node });
            Settings = new ConnectionSettings(Node);
            Client = new ElasticClient(Settings);
            var indexsettings = new IndexSettings();
            indexsettings.NumberOfReplicas = 1;
            indexsettings.NumberOfShards = 5;

            Client.CreateIndex(c => c
                .Index(name)
                .InitializeUsing(indexsettings)
                .AddMapping<activitylog>(m => m.MapFromAttributes()));
        }