Beispiel #1
0
        public async Task Consume(ConsumeContext <UserPersisted> context)
        {
            var alias = await _elasticClient.AliasExistsAsync(new AliasExistsDescriptor().Name(context.Message.Id.ToString()));

            if (alias.Exists)
            {
                await _elasticClient.AliasAsync(a => a
                                                .Remove(r => r.Index("records").Alias(context.Message.Id.ToString()))
                                                .Remove(r => r.Index("folders").Alias(context.Message.Id.ToString()))
                                                .Remove(r => r.Index("files").Alias(context.Message.Id.ToString()))
                                                .Remove(r => r.Index("models").Alias(context.Message.Id.ToString())));
            }

            try
            {
                await _elasticClient.AliasAsync(a => a
                                                .Add(add => add.Index("files").Alias(context.Message.Id.ToString())
                                                     .Filter <dynamic>(qc => qc.Bool(b => b.Should(s => s.Term("OwnedBy", context.Message.Id.ToString()), s => s.Term("IsPublic", true)))))
                                                .Add(add => add.Index("folders").Alias(context.Message.Id.ToString())
                                                     .Filter <dynamic>(qc => qc.Bool(b => b.Should(s => s.Term("OwnedBy", context.Message.Id.ToString()), s => s.Term("IsPublic", true)))))
                                                .Add(add => add.Index("records").Alias(context.Message.Id.ToString())
                                                     .Filter <dynamic>(qc => qc.Bool(b => b.Should(s => s.Term("OwnedBy", context.Message.Id.ToString()), s => s.Term("IsPublic", true)))))
                                                .Add(add => add.Index("models").Alias(context.Message.Id.ToString())
                                                     .Filter <dynamic>(qc => qc.Bool(b => b.Should(s => s.Term("OwnedBy", context.Message.Id.ToString()), s => s.Term("IsPublic", true)))))
                                                );

                Log.Information($"Alias {context.Message.Id} successfully created");
            }
            catch (ElasticsearchClientException e)
            {
                Log.Error($"Creating alias server error: {e.Response.ServerError}");
            }
        }
		public override async Task TeardownAsync(IElasticClient client, ColoredConsoleWriter output)
		{
			var removeResponse = await client.AliasAsync(a => a
				.Remove(remove => remove
					.Alias(_aliasName)
					.Index<Developer>()
				)).ConfigureAwait(false);

			if (!removeResponse.IsValid)
				output.WriteOrange($"Invalid response when adding alias for {nameof(AliasExistsAsyncOperation)} operation");
		}
Beispiel #3
0
        public override async Task TeardownAsync(IElasticClient client, ColoredConsoleWriter output)
        {
            var removeResponse = await client.AliasAsync(a => a
                                                         .Remove(remove => remove
                                                                 .Alias(_aliasName)
                                                                 .Index <Developer>()
                                                                 )).ConfigureAwait(false);

            if (!removeResponse.IsValid)
            {
                output.WriteOrange($"Invalid response when adding alias for {nameof(AliasExistsAsyncOperation)} operation");
            }
        }
Beispiel #4
0
        public override async Task ProfileAsync(IElasticClient client, ColoredConsoleWriter output)
        {
            for (int i = 0; i < _iterations; i++)
            {
                IBulkAliasResponse aliasResponse;
                if (i == 0)
                {
                    aliasResponse = await client.AliasAsync(a => a
                                                            .Add(add => add
                                                                 .Alias($"dev{i}").Index <Developer>()
                                                                 )).ConfigureAwait(false);
                }
                else if (i == _iterations - 1)
                {
                    aliasResponse = await client.AliasAsync(a => a
                                                            .Remove(remove => remove
                                                                    .Alias($"dev{i - 1}")
                                                                    .Index(typeof(Developer))
                                                                    )).ConfigureAwait(false);
                }
                else
                {
                    aliasResponse = await client.AliasAsync(a => a
                                                            .Add(add => add
                                                                 .Alias($"dev{i}").Index <Developer>()
                                                                 )
                                                            .Remove(remove => remove
                                                                    .Alias($"dev{i - 1}")
                                                                    .Index(typeof(Developer))
                                                                    )).ConfigureAwait(false);
                }

                if (!aliasResponse.IsValid)
                {
                    output.WriteOrange($"Invalid response from {nameof(AliasAsyncOperation)} operation");
                }
            }
        }
		public override async Task ProfileAsync(IElasticClient client, ColoredConsoleWriter output)
		{
			for (int i = 0; i < _iterations; i++)
			{
				IBulkAliasResponse aliasResponse;
				if (i == 0)
				{
					aliasResponse = await client.AliasAsync(a => a
						.Add(add => add
							.Alias($"dev{i}").Index<Developer>()
						)).ConfigureAwait(false);
				}
				else if (i == _iterations - 1)
				{
					aliasResponse = await client.AliasAsync(a => a
						.Remove(remove => remove
							.Alias($"dev{i - 1}")
							.Index(typeof (Developer))
						)).ConfigureAwait(false);
				}
				else
				{
					aliasResponse = await client.AliasAsync(a => a
						.Add(add => add
							.Alias($"dev{i}").Index<Developer>()
						)
						.Remove(remove => remove
							.Alias($"dev{i - 1}")
							.Index(typeof (Developer))
						)).ConfigureAwait(false);
				}

				if (!aliasResponse.IsValid)
					output.WriteOrange($"Invalid response from {nameof(AliasAsyncOperation)} operation");
			}
		}
Beispiel #6
0
        /// <summary>
        /// 初始化索引映射
        /// </summary>
        /// <param name="client">ES客户端</param>
        /// <param name="indexName">索引名</param>
        public static async Task InitializeIndexMapAsync(this IElasticClient client, string indexName)
        {
            var newName = indexName + DateTime.Now.Ticks;
            var result  = await client.CreateIndexAsync(newName,
                                                        x => x.Index(newName)
                                                        .Settings(o =>
                                                                  o.NumberOfShards(1)
                                                                  .NumberOfReplicas(1)
                                                                  .Setting("max_result_window", int.MaxValue)));

            if (result.Acknowledged)
            {
                await client.AliasAsync(x => x.Add(o => o.Index(newName).Alias(indexName)));

                return;
            }
            throw new ElasticsearchException($"创建索引 {indexName} 失败:{result.ServerError.Error.Reason}");
        }
Beispiel #7
0
        private async Task UpdateZeroDownTime(string index1, string index2, string alias, Func <string, Task> createIndexDelegate, List <Feature> features)
        {
            var currentIndex = index1;
            var newIndex     = index2;

            if (_elasticClient.IndexExists(index2).Exists)
            {
                currentIndex = index2;
                newIndex     = index1;
            }

            await createIndexDelegate(newIndex);
            await UpdateUsingPaging(features, newIndex);

            await _elasticClient.AliasAsync(a => a
                                            .Remove(i => i.Alias(alias).Index(currentIndex))
                                            .Add(i => i.Alias(alias).Index(newIndex))
                                            );

            await _elasticClient.DeleteIndexAsync(currentIndex);
        }
        public static async Task <IBulkAliasResponse> SwapAliasAsync(
            this IElasticClient client,
            string alias,
            string indexName)
        {
            if (string.IsNullOrEmpty(alias))
            {
                throw new ArgumentNullException(nameof(alias));
            }

            if (string.IsNullOrEmpty(indexName))
            {
                throw new ArgumentNullException(nameof(indexName));
            }

            var indicesForAlias = client.GetIndicesPointingToAlias(alias);

            var response = await client.AliasAsync(bulkAliasDescriptor =>
            {
                foreach (var index in indicesForAlias)
                {
                    bulkAliasDescriptor = bulkAliasDescriptor
                                          .Remove(removeDescriptor => removeDescriptor
                                                  .Alias(alias)
                                                  .Index(index));
                }

                bulkAliasDescriptor
                .Add(addDescriptor => addDescriptor
                     .Alias(alias)
                     .Index(indexName));

                return(bulkAliasDescriptor);
            });

            return(response);
        }
Beispiel #9
0
        public async Task UpdateDataZeroDownTime(List <Feature> names, List <Feature> highways)
        {
            // init
            var currentNameIndex    = OSM_NAMES_INDEX1;
            var currentHighwayIndex = OSM_HIGHWAYS_INDEX1;
            var newNameIndex        = OSM_NAMES_INDEX2;
            var newHighwayIndex     = OSM_HIGHWAYS_INDEX2;

            if (_elasticClient.IndexExists(OSM_NAMES_INDEX2).Exists)
            {
                currentNameIndex    = OSM_NAMES_INDEX2;
                currentHighwayIndex = OSM_HIGHWAYS_INDEX2;
                newNameIndex        = OSM_NAMES_INDEX1;
                newHighwayIndex     = OSM_HIGHWAYS_INDEX1;
            }
            // create new indexes
            await CreateNamesIndex(newNameIndex);
            await CreateHighwaysIndex(newHighwayIndex);

            // update data
            await UpdateUsingPaging(names, newNameIndex);
            await UpdateUsingPaging(highways, newHighwayIndex);

            // change alias
            await _elasticClient.AliasAsync(a => a
                                            .Remove(i => i.Alias(OSM_NAMES_ALIAS).Index(currentNameIndex))
                                            .Remove(i => i.Alias(OSM_HIGHWAYS_ALIAS).Index(currentHighwayIndex))
                                            .Add(i => i.Alias(OSM_NAMES_ALIAS).Index(newNameIndex))
                                            .Add(i => i.Alias(OSM_HIGHWAYS_ALIAS).Index(newHighwayIndex))
                                            );

            // delete old indexes
            await _elasticClient.DeleteIndexAsync(currentNameIndex);

            await _elasticClient.DeleteIndexAsync(currentHighwayIndex);
        }
Beispiel #10
0
        public async Task IndexEntityAsync(string indexName, string typeName, Guid id)
        {
            dynamic entity = await GetEntityFromDatabase(id);

            try
            {
                var indexingResult = await _elasticClient.IndexAsync <object>(new IndexRequest <object>(entity, indexName, typeName, id));

                Guid userId = entity.OwnedBy;

                var alias = await _elasticClient.AliasExistsAsync(new AliasExistsDescriptor().Name(userId.ToString()).Index(Indices.Parse(indexName)));

                if (!alias.Exists)
                {
                    await _elasticClient.AliasAsync(a => a
                                                    .Add(add => add.Index(indexName).Alias(userId.ToString())
                                                         .Filter <dynamic>(qc => qc.Bool(b => b.Should(s => s.Term("OwnedBy", userId.ToString()), s => s.Term("IsPublic", true))))));
                }
            }
            catch (ElasticsearchClientException e)
            {
                Log.Error($"{e.Response.ServerError}");
            }
        }