コード例 #1
0
        public virtual void DeleteIndexes(IElasticClient client, IEnumerable <IElasticIndex> indexes = null)
        {
            if (indexes == null)
            {
                indexes = GetIndexes();
            }

            foreach (var idx in indexes)
            {
                IIndicesResponse deleteResponse;

                var templatedIndex = idx as ITemplatedElasticIndex;
                if (templatedIndex != null)
                {
                    deleteResponse = client.DeleteIndex(idx.VersionedName + "-*");

                    if (client.TemplateExists(idx.VersionedName).Exists)
                    {
                        var response = client.DeleteTemplate(idx.VersionedName);
                        Debug.Assert(response.IsValid, response.ServerError != null ? response.ServerError.Error : "An error occurred deleting the index template.");
                    }
                }
                else
                {
                    deleteResponse = client.DeleteIndex(idx.VersionedName);
                }

                Debug.Assert(deleteResponse.IsValid, deleteResponse.ServerError != null ? deleteResponse.ServerError.Error : "An error occurred deleting the indexes.");
            }
        }
コード例 #2
0
 public void Initialize(string uri = "http://localhost:9200/", bool deleteIndex = false)
 {
     _logger.Info("Initialing elastic search with uri: " + uri);
     var connectionString = new ConnectionSettings(
         new Uri(uri))
         .DefaultIndex(OSM_NAMES_INDEX)
         .PrettyJson();
     _elasticClient = new ElasticClient(connectionString);
     if (deleteIndex && _elasticClient.IndexExists(OSM_NAMES_INDEX).Exists)
     {
         _elasticClient.DeleteIndex(OSM_NAMES_INDEX);
     }
     if (deleteIndex && _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX).Exists)
     {
         _elasticClient.DeleteIndex(OSM_HIGHWAYS_INDEX);
     }
     _elasticClient.CreateIndex(OSM_HIGHWAYS_INDEX,
             c => c.Mappings(
                 ms => ms.Map<object>(m =>
                     m.Properties(ps => ps.GeoShape(g => g.Name("geometry")
                         .Tree(GeoTree.Geohash)
                         .TreeLevels(10)
                         .DistanceErrorPercentage(0.2))))));
     _logger.Info("Finished initialing elastic search with uri: " + uri);
 }
コード例 #3
0
        public void Initialize()
        {
            var uri = _options.ElasticsearchServerAddress;

            _logger.LogInformation("Initialing elastic search with uri: " + uri);
            var pool             = new SingleNodeConnectionPool(new Uri(uri));
            var connectionString = new ConnectionSettings(
                pool,
                new HttpConnection(),
                new SerializerFactory(s => new GeoJsonNetSerializer(s, _geometryFactory)))
                                   .PrettyJson();

            _elasticClient = new ElasticClient(connectionString);
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists == false)
            {
                CreatePointsOfInterestIndex(OSM_POIS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_POIS_ALIAS).Index(OSM_POIS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_POIS_INDEX2);
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists == false)
            {
                CreateHighwaysIndex(OSM_HIGHWAYS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_HIGHWAYS_ALIAS).Index(OSM_HIGHWAYS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_HIGHWAYS_INDEX2);
            }
            if (_elasticClient.IndexExists(SHARES).Exists == false)
            {
                _elasticClient.CreateIndex(SHARES);
            }
            if (_elasticClient.IndexExists(CUSTOM_USER_LAYERS).Exists == false)
            {
                _elasticClient.CreateIndex(CUSTOM_USER_LAYERS);
            }
            if (_elasticClient.IndexExists(IMAGES).Exists == false)
            {
                CreateImagesIndex();
            }
            _logger.LogInformation("Finished initialing elasticsearch with uri: " + uri);
        }
コード例 #4
0
 private void DeleteIndex()
 {
     if (IsIndexExists())
     {
         client.DeleteIndex(INDEX_NAME);
     }
 }
コード例 #5
0
 /// <summary>
 /// The delete index API allows to delete an existing index.
 /// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
 /// </summary>
 /// <param name="client"></param>
 /// <param name="index">The name of the index to be deleted</param>
 /// <param name="deleteIndexSelector">A descriptor that further describes the parameters for the delete index operation</param>
 public static IIndicesResponse DeleteIndex(this IElasticClient client, string index,
                                            Func <DeleteIndexDescriptor, DeleteIndexDescriptor> deleteIndexSelector = null)
 {
     index.ThrowIfNullOrEmpty("index");
     deleteIndexSelector = deleteIndexSelector ?? (d => d);
     return(client.DeleteIndex(d => deleteIndexSelector(d).Index(index)));
 }
コード例 #6
0
        public void Init()
        {
            _esClient.DeleteIndex(did => did.Index(ElasticClientBuilder.IndexName));
            var result = _esClient.CreateIndex(cid => cid
                                               .Index(ElasticClientBuilder.IndexName)
                                               .Analysis(ad => ad
                                                         .Tokenizers(fd => fd
                                                                     .Add("my_edge_ngram_tokenizer", new EdgeNGramTokenizer()
            {
                MaxGram    = 8,
                MinGram    = 2,
                TokenChars = new[] { "letter", "digit" }
            }))
                                                         .Analyzers(fd => fd
                                                                    .Add("my_edge_ngram_analyzer", new CustomAnalyzer("my_edge_ngram_analyzer")
            {
                Tokenizer = "my_edge_ngram_tokenizer",
                Filter    = new List <string>()
                {
                    "standard", "lowercase"
                }
            })))
                                               .AddMapping <Customer>(m => m.MapFromAttributes())
                                               .AddMapping <Product>(m => m.MapFromAttributes()));
//            SetupAnalyzer(_esClient);
            var requestString = UTF8Encoding.UTF8.GetString(result.ConnectionStatus.Request);
        }
コード例 #7
0
 protected override void IntegrationTeardown(IElasticClient client, CallUniqueValues values)
 {
     foreach (var(k, v) in values)
     {
         client.DeleteIndex(v);
     }
 }
コード例 #8
0
 public void DeleteIndexIfExists(IndexDefinition index)
 {
     if (_client.IndexExists(index.Name).Exists)
     {
         _client.DeleteIndex(index.Name);
     }
 }
コード例 #9
0
        public void ReIndex()
        {
            const string newIndex = "cx_demo_claims_2";
            var          reindex  =
                _client.Reindex <ClaimModel>(Storage.ClaimIndex, newIndex, (inbox => inbox
                                                                            .MatchAll()));

            var observer = new ReindexObserver(onNext: response =>
            {
                Console.WriteLine($"Indexing documents");
            }, onError: error =>
            {
                Console.WriteLine($"Document error occured: {error.Message}");
            },
                                               onCompleted: () =>
            {
                Console.WriteLine($"Document re-index");
            });

            reindex.Subscribe(observer);

            _client.DeleteIndex(Indices.Index(new IndexName {
                Name = Storage.ClaimIndex
            }));
            _client.Alias(x => x.Add(a => a.Alias(Storage.ClaimsAlias).Index(newIndex)));
        }
コード例 #10
0
 /// <summary>
 /// delete index
 /// </summary>
 /// <param name="indexName"></param>
 public void DeleteIndex(string indexName)
 {
     if (_client.IndexExists(indexName).Exists)
     {
         var indexResponse = _client.DeleteIndex(indexName);
     }
 }
コード例 #11
0
        public void Initialize(string uri = "http://localhost:9200/")
        {
            _logger.LogInformation("Initialing elastic search with uri: " + uri);
            var pool             = new SingleNodeConnectionPool(new Uri(uri));
            var connectionString = new ConnectionSettings(
                pool,
                new HttpConnection(),
                new SerializerFactory(s => new GeoJsonNetSerializer(s)))
                                   .PrettyJson();

            _elasticClient = new ElasticClient(connectionString);
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists == false)
            {
                CreatePointsOfInterestIndex(OSM_POIS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_POIS_ALIAS).Index(OSM_POIS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_POIS_INDEX2);
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists == false)
            {
                CreateHighwaysIndex(OSM_HIGHWAYS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_HIGHWAYS_ALIAS).Index(OSM_HIGHWAYS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_HIGHWAYS_INDEX2);
            }
            if (_elasticClient.IndexExists(RATINGS).Exists == false)
            {
                _elasticClient.CreateIndex(RATINGS);
            }
            if (_elasticClient.IndexExists(SHARES).Exists == false)
            {
                _elasticClient.CreateIndex(SHARES);
            }
            if (_elasticClient.IndexExists(USER_LAYERS).Exists == false)
            {
                _elasticClient.CreateIndex(SHARES);
            }
            _logger.LogInformation("Finished initialing elasticsearch with uri: " + uri);
        }
コード例 #12
0
        public void Initialize(string uri = "http://localhost:9200/")
        {
            _logger.LogInformation("Initialing elastic search with uri: " + uri);
            var pool             = new SingleNodeConnectionPool(new Uri(uri));
            var connectionString = new ConnectionSettings(
                pool,
                new HttpConnection(),
                new ConnectionSettings.SourceSerializerFactory((builtin, settings) => new JsonNetSerializer(builtin, settings, () => new Newtonsoft.Json.JsonSerializerSettings
            {
                Converters = GeoJsonSerializer.Create(_geometryFactory, 3).Converters
            })))
                                   .PrettyJson();

            _elasticClient = new ElasticClient(connectionString);
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists == false)
            {
                CreatePointsOfInterestIndex(OSM_POIS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_POIS_ALIAS).Index(OSM_POIS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_POIS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_POIS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_POIS_INDEX2);
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists == false &&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists == false)
            {
                CreateHighwaysIndex(OSM_HIGHWAYS_INDEX1);
                _elasticClient.Alias(a => a.Add(add => add.Alias(OSM_HIGHWAYS_ALIAS).Index(OSM_HIGHWAYS_INDEX1)));
            }
            if (_elasticClient.IndexExists(OSM_HIGHWAYS_INDEX1).Exists&&
                _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX2).Exists)
            {
                _elasticClient.DeleteIndex(OSM_HIGHWAYS_INDEX2);
            }
            if (_elasticClient.IndexExists(SHARES).Exists == false)
            {
                _elasticClient.CreateIndex(SHARES);
            }
            if (_elasticClient.IndexExists(CUSTOM_USER_LAYERS).Exists == false)
            {
                _elasticClient.CreateIndex(CUSTOM_USER_LAYERS);
            }
            _logger.LogInformation("Finished initialing elasticsearch with uri: " + uri);
        }
コード例 #13
0
        public IDeleteIndexResponse DeleteIndex(IndexName index, [CallerMemberName] string callerName = "")
        {
            var timer  = Stopwatch.StartNew();
            var result = _client.DeleteIndex(index);

            SendLog(result.ApiCall, null, timer.ElapsedMilliseconds, $"Delete Index {index.Name}");
            return(result);
        }
コード例 #14
0
        private void PrepareIndexes(IRootResolver resolver)
        {
            _elasticClient = resolver.Resolve <IElasticClient>();

            var readModelTypes =
                GetLoadableTypes <ElasticsearchTypeAttribute>(typeof(ElasticsearchThingyReadModel).Assembly);

            foreach (var readModelType in readModelTypes)
            {
                var esType = readModelType.GetTypeInfo()
                             .GetCustomAttribute <ElasticsearchTypeAttribute>();

                var aliasResponse = _elasticClient.GetAlias(x => x.Name(esType.Name));

                if (aliasResponse.ApiCall.Success)
                {
                    if (aliasResponse.Indices != null)
                    {
                        foreach (var indice in aliasResponse?.Indices)
                        {
                            _elasticClient.DeleteAlias(indice.Key, esType.Name);

                            _elasticClient.DeleteIndex(indice.Key,
                                                       d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)));
                        }

                        _elasticClient.DeleteIndex(esType.Name,
                                                   d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)));
                    }
                }

                var indexName = GetIndexName(esType.Name);

                _indexes.Add(indexName);

                _elasticClient.CreateIndex(indexName, c => c
                                           .Settings(s => s
                                                     .NumberOfShards(1)
                                                     .NumberOfReplicas(0))
                                           .Aliases(a => a.Alias(esType.Name))
                                           .Mappings(m => m
                                                     .Map(TypeName.Create(readModelType), d => d
                                                          .AutoMap())));
            }
        }
コード例 #15
0
 public override void TearDown()
 {
     try
     {
         var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <ElasticsearchTestAggregateReadModel>();
         _elasticClient.DeleteIndex(readModelDescription.IndexName.Value);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #16
0
        public void CreateIndex(List <Worktask> list)
        {
            if (!client.IndexExists(ElasticConfig.IndexName).Exists)
            {
                var indexDescriptor = new CreateIndexDescriptor(ElasticConfig.IndexName)
                                      .Mappings(ms => ms
                                                .Map <Worktask>((TypeMappingDescriptor <Worktask> m) => m.AutoMap())
                                                .Map <Domain.Entities.Attachment>((TypeMappingDescriptor <Domain.Entities.Attachment> m) => m.AutoMap())

                                                );

                client.CreateIndex(ElasticConfig.IndexName, i => indexDescriptor);
            }
            else
            {
                var res             = client.LowLevel.IndicesGetMapping <Worktask>(ElasticConfig.IndexName);
                var del             = client.DeleteIndex(new DeleteIndexRequest(ElasticConfig.IndexName));
                var indexDescriptor = new CreateIndexDescriptor(ElasticConfig.IndexName)
                                      .Mappings(ms => ms
                                                .Map <Worktask>((TypeMappingDescriptor <Worktask> m) => m.AutoMap())
                                                .Map <Domain.Entities.Attachment>((TypeMappingDescriptor <Domain.Entities.Attachment> m) => m.AutoMap())

                                                );

                client.CreateIndex(ElasticConfig.IndexName, i => indexDescriptor);
            }

            foreach (Worktask item in list)
            {
                try
                {
                    client.Index(item);
                }
                catch
                {
                    throw;
                }
            }
        }
コード例 #17
0
 public void TearDown()
 {
     try
     {
         var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <ElasticsearchThingyReadModel>();
         var indexName            = readModelDescription.IndexName.Value;
         Console.WriteLine("Deleting test index '{0}'", indexName);
         _elasticClient.DeleteIndex(indexName);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #18
0
 public void TearDown()
 {
     try
     {
         Console.WriteLine($"Deleting test index '{_indexName}'");
         _elasticClient.DeleteIndex(
             _indexName,
             r => r.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #19
0
        public void DeleteIndex()
        {
            if (!Client.IndexExists(IndexName).Exists)
            {
                return;
            }

            var deleteIndexRequest = Client.DeleteIndex(IndexName);

            if (!deleteIndexRequest.IsValid)
            {
                RequestError(deleteIndexRequest);
            }
        }
コード例 #20
0
        private static bool DeleteThenCreateIndex()
        {
            var deleteResult = _client.DeleteIndex("pmcd-ai-index");

            if (!deleteResult.Acknowledged)
            {
                return(false);
            }

            var ci = _client.CreateIndex("pmcd-ai-index",
                                         d => d.Mappings(m => m.Map <Importer.Ai>(mp => mp.AutoMap()))
                                         .Aliases(a => a.Alias("pmcd-ai-alias")));

            return(ci.Acknowledged);
        }
コード例 #21
0
        public BulkProfileTests(ClusterBase cluster)
        {
            _client = cluster.Client;

            if (_client.IndexExists(IndexName).Exists)
            {
                _client.DeleteIndex(IndexName);
            }

            var createIndexResponse = _client.CreateIndex(IndexName);

            if (!createIndexResponse.IsValid)
            {
                Console.WriteLine($"invalid response creating index: {createIndexResponse.ServerError?.Error?.Reason}");
            }
        }
コード例 #22
0
        private void BuildIndexes()
        {
            if (_client.IndexExists(IndexName).Exists)
            {
                _client.DeleteIndex(IndexName);
            }

            _client.CreateIndex(IndexName, indexDescriptor => indexDescriptor
                                .Settings(s => s
                                          .NumberOfShards(1)
                                          .NumberOfReplicas(0))
                                .Mappings(mappings => mappings
                                          .Map <BoardGame>(mapper => mapper.AutoMap())));

            _client.Map <BoardGame>(mapper => mapper.Index(IndexName).AutoMap());
        }
コード例 #23
0
        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="client"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public static void DeleteIndexIfExists(this IElasticClient client, string indexName)
        {
            indexName = indexName.ToLower();

            var exist_response = client.IndexExists(indexName);

            exist_response.ThrowIfException();

            if (!exist_response.Exists)
            {
                return;
            }

            var response = client.DeleteIndex(indexName);

            response.ThrowIfException();
        }
コード例 #24
0
        internal static void SeedElasticsearch(this IApplicationBuilder app)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            IElasticClient client   = app.ApplicationServices.GetService <IElasticClient>();
            AppSettings    settings = app.ApplicationServices.GetService <AppSettings>();
            string         index    = settings != null &&
                                      settings.ElasticConfig != null &&
                                      string.IsNullOrWhiteSpace(settings.ElasticConfig.DefaultIndex) ?
                                      settings.ElasticConfig.DefaultIndex :
                                      "friends";

            var observable = Observable.Create <bool> (async o => {
                var elasticHealth = await client.CatHealthAsync();
                if (elasticHealth.IsValid)
                {
                    Console.WriteLine("Elasticsearch is UP!");
                    o.OnNext(true);
                    o.OnCompleted();
                }
                else
                {
                    Console.WriteLine("Elasticsearch is down!");
                    o.OnError(new Exception("Elasticsearch is down"));
                }
            });

            var observer = Observer.Create <bool> (isElasticsearchUp => {
                // Remove then create the index
                client.DeleteIndex(index);
                client.CreateIndex(index, c => c.Mappings(ms => ms.Map <Friend> (m => m.AutoMap())));

                // Bulk insert random friends; bulk for performance
                client.Bulk((s) => {
                    return(s.IndexMany(Friend.BuildRandomFriends(10)));
                });
            });

            observable.DelaySubscription(TimeSpan.FromSeconds(1)).Retry(60).Subscribe(observer);
        }
コード例 #25
0
        public void DeleteIndexes(IElasticClient client)
        {
            var deleteResponse = client.DeleteIndex(i => i.AllIndices());

            Debug.Assert(deleteResponse.IsValid, deleteResponse.ServerError != null ? deleteResponse.ServerError.Error : "An error occurred deleting the indexes.");

            foreach (var index in GetIndexes())
            {
                var templatedIndex = index as ITemplatedElasticSeachIndex;
                if (templatedIndex != null)
                {
                    if (client.TemplateExists(index.VersionedName).Exists)
                    {
                        var response = client.DeleteTemplate(index.VersionedName);
                        Debug.Assert(response.IsValid, response.ServerError != null ? response.ServerError.Error : "An error occurred deleting the index template.");
                    }
                }
            }
        }
コード例 #26
0
        /// <summary>
        /// This will remove any indices older than a given date
        /// </summary>
        /// <param name="olderThan">The date</param>
        /// <param name="minIndices">The minimum number of indices to keep regardless of date.</param>
        /// <returns>An array of indices that were deleted.</returns>
        public string[] DeleteOldIndices(DateTime olderThan, int minIndices)
        {
            Tuple <string[], string[]> indices = GetIndices(olderThan);

            string[] oldIndices = indices.Item1;
            string[] newIndices = indices.Item2;

            if (oldIndices.Length > 0)
            {
                //There are old candidates, but will we have minIndices left when finished.
                int diff = minIndices - newIndices.Length;
                if (diff > 0)
                {
                    //Skip past the old items.  P.S. If oldIndices is less than diff, then
                    //Skip will return empty array.  yay!!
                    oldIndices = oldIndices.Skip(diff).ToArray();
                }


                if (oldIndices.Length > 0)
                {
                    var indicesToDelete = from index in oldIndices
                                          select new IndexName()
                    {
                        Name = index
                    };

                    var response = _client.DeleteIndex(Indices.Index(indicesToDelete));

                    if (!response.IsValid)
                    {
                        _logger.LogError("Elasticsearch Response is Not Valid deleting indices '{0}'.", oldIndices);
                        _logger.LogError("Returned debug info: {0}.", response.DebugInformation);
                        throw new Exception(String.Format("Error deleting Indices, {0}", oldIndices));
                    }
                }
            }

            return(oldIndices);
        }
コード例 #27
0
        /// <summary>
        /// 重建索引
        /// </summary>
        /// <param name="client"></param>
        /// <param name="oldIndexName">就索引名字</param>
        /// <param name="newIndexName">新索引名字</param>
        /// <param name="shardsNumber"></param>
        /// <returns></returns>
        public static bool ReIndex <T>(IElasticClient client, string oldIndexName, string newIndexName, int shardsNumber = 1) where T : class
        {
            //检查新索引别名
            var alias = client.AliasExists($"{newIndexName}-reindex");

            //如果改别名不存在
            if (!alias.Exists)
            {
                //检查是否存在新索引
                IExistsResponse newIndex = client.IndexExists(newIndexName);
                client.UpdateIndexSettings(Indices.Index(newIndexName), setting => setting.IndexSettings(isetting => isetting.Setting("max_result_window", 100000)));
                //如果新索引不存在则创建一个新索引,需要修改各项创建索引时才需要的参数则在这处理
                if (!newIndex.Exists)
                {
                    CreateIndexDescriptor createIndex = new CreateIndexDescriptor(newIndexName)
                                                        .Settings(s => s.NumberOfShards(shardsNumber).Analysis(a => a.Analyzers(aa => aa.Language("standard_listing", sa => sa.Language(Language.Chinese)))))
                                                        .Mappings(ms => ms.Map <T>(m => m.AutoMap()));
                    ICreateIndexResponse create = client.CreateIndex(createIndex);
                    Console.WriteLine($"{newIndexName}索引创建:{create.ApiCall.Success}");
                    //给新创建的索引添加一个索引别名表示时重新创建的
                    var r2 = client.Alias(t => t.Add(a => a.Index(newIndexName).Alias($"{newIndexName}-reindex")));
                    Console.WriteLine($"索引别名{newIndexName}=>{newIndexName}:{r2.ApiCall.Success}");
                }
                //将老索引中的文档索引导新建的索引
                var r1 = client.ReindexOnServer(r => r.Source(s => s.Index(Indices.Index(oldIndexName))).Destination(d => d.Index(newIndexName)));
                Console.WriteLine($"重新索引:{r1.ApiCall.Success}");
                if (r1.ApiCall.Success)
                {
                    //老索引中的所有文档全部到新索引之后删除老索引
                    client.DeleteIndex(Indices.Index(oldIndexName));
                    //新索引设置别名为老索引的名字
                    var r3 = client.Alias(t => t.Add(a => a.Index(newIndexName).Alias(oldIndexName)));
                    Console.WriteLine($"索引别名{newIndexName}=>{oldIndexName}:{r3.ApiCall.Success}");
                }
            }
            return(alias.Exists);
        }
コード例 #28
0
 public static void ClearItems(IElasticClient client)
 {
     client.DeleteIndex(i => i.Index<Person>());
     //client.DeleteByQuery<Person>(q => q.AllIndices().MatchAll());
 }
コード例 #29
0
        public void DeleteIndexes(IElasticClient client) {
            var deleteResponse = client.DeleteIndex(i => i.AllIndices());
            Debug.Assert(deleteResponse.IsValid, deleteResponse.ServerError != null ? deleteResponse.ServerError.Error : "An error occurred deleting the indexes.");

            foreach (var index in GetIndexes()) {
                var templatedIndex = index as ITemplatedElasticSeachIndex;
                if (templatedIndex != null) {
                    if (client.TemplateExists(index.VersionedName).Exists) {
                        var response = client.DeleteTemplate(index.VersionedName);
                        Debug.Assert(response.IsValid, response.ServerError != null ? response.ServerError.Error : "An error occurred deleting the index template.");
                    }
                }
            }
        }
コード例 #30
0
        public void ElasticSearch_CheckConnection()
        {
            var responseDelete = _esClient.DeleteIndex(IndexName);

            if (!_esClient.IndexExists(IndexName).Exists)
            {
                // Create a customize Startswith
                ICreateIndexRequest Config(CreateIndexDescriptor r) => r.Settings(s => s.NumberOfShards(1)
                                                                                  .NumberOfReplicas(5)
                                                                                  .Analysis(al => al.Analyzers(a => a.Custom("analyzer_startswith", c => c.Tokenizer("keyword").Filters("lowercase")))))
                .Mappings(m => m.Map <Person>(t => t.AutoMap()));

                var responseCreate = _esClient.CreateIndex(IndexName, Config);
                //esClient.Map<Person>(m =>
                //{
                //    var putMappingDescriptor = m.Index(Indices.Index("person")).AutoMap();
                //    return putMappingDescriptor;
                //});
            }

            var person = new Person
            {
                Id        = 1,
                FirstName = "Võ Kế",
                LastName  = "Nghiệp"
            };
            var repInsertDoc = _esClient.Index(person, s => s.Index <Person>());

            person = new Person
            {
                Id        = 2,
                FirstName = "Võ Trọng",
                LastName  = "Nghĩa"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());

            person = new Person
            {
                Id        = 3,
                FirstName = "Võ Trắc",
                LastName  = "Nghị"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());

            person = new Person
            {
                Id        = 4,
                FirstName = "Võ Nguyên",
                LastName  = "Khang"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());

            person = new Person
            {
                Id        = 5,
                FirstName = "Delete",
                LastName  = "Delete"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());
            var repDeleteDoc = _esClient.Delete <Person>(5);

            person = new Person
            {
                Id        = 6,
                FirstName = "Update",
                LastName  = "Update"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());
            person       = new Person
            {
                Id        = 6,
                FirstName = "Update 1",
                LastName  = "Update 1"
            };
            repInsertDoc = _esClient.Index(person, s => s.Index <Person>());
            var repGetDoc = _esClient.Get <Person>(6);

            //esClient.DeleteIndex("people");
            //Assert.AreEqual(repInsertDoc.Result, Result.Updated);
            var repUpdateDoc = _esClient.Update <Person, Person>(new Person {
                Id = 6, FirstName = "Update 1"
            },
                                                                 p => p.Doc(new Person {
                FirstName = "Update 2"
            }));

            Assert.AreEqual(repUpdateDoc.Version, 3);
        }
コード例 #31
0
        private static void ConfigureMapping(IElasticClient searchclient, bool deleteExistingIndexes = false) {
            if (deleteExistingIndexes)
                searchclient.DeleteIndex(i => i.AllIndices());

            if (!searchclient.IndexExists(new IndexExistsRequest(new IndexNameMarker { Name = ElasticSearchRepository<Stack>.StacksIndexName })).Exists)
                searchclient.CreateIndex(ElasticSearchRepository<Stack>.StacksIndexName, d => d
                    .AddAlias("stacks")
                    .AddMapping<Stack>(map => map
                        .Dynamic(DynamicMappingOption.Ignore)
                        .IncludeInAll(false)
                        .Properties(p => p
                            .String(f => f.Name(s => s.OrganizationId).IndexName("organization").Index(FieldIndexOption.NotAnalyzed))
                            .String(f => f.Name(s => s.ProjectId).IndexName("project").Index(FieldIndexOption.NotAnalyzed))
                            .String(f => f.Name(s => s.SignatureHash).IndexName("signature").Index(FieldIndexOption.NotAnalyzed))
                            .String(f => f.Name(e => e.Type).IndexName("type").Index(FieldIndexOption.NotAnalyzed))
                            .Date(f => f.Name(s => s.FirstOccurrence).IndexName("first"))
                            .Date(f => f.Name(s => s.LastOccurrence).IndexName("last"))
                            .String(f => f.Name(s => s.Title).IndexName("title").Index(FieldIndexOption.Analyzed).IncludeInAll().Boost(1.1))
                            .String(f => f.Name(s => s.Description).IndexName("description").Index(FieldIndexOption.Analyzed).IncludeInAll())
                            .String(f => f.Name(s => s.Tags).IndexName("tag").Index(FieldIndexOption.NotAnalyzed).IncludeInAll().Boost(1.2))
                            .String(f => f.Name(s => s.References).IndexName("references").Index(FieldIndexOption.Analyzed).IncludeInAll())
                            .Date(f => f.Name(s => s.DateFixed).IndexName("fixed"))
                            .Boolean(f => f.Name(s => s.IsHidden).IndexName("hidden"))
                            .Boolean(f => f.Name(s => s.IsRegressed).IndexName("regressed"))
                            .Boolean(f => f.Name(s => s.OccurrencesAreCritical).IndexName("critical"))
                            .Number(f => f.Name(s => s.TotalOccurrences).IndexName("occurrences"))
                        )
                    )
                );

            searchclient.PutTemplate(ElasticSearchRepository<PersistentEvent>.EventsIndexName, d => d
                .Template(ElasticSearchRepository<PersistentEvent>.EventsIndexName + "-*")
                .AddMapping<PersistentEvent>(map => map
                    .Dynamic(DynamicMappingOption.Ignore)
                    .IncludeInAll(false)
                    .DisableSizeField(false)
                    .Properties(p => p
                        .String(f => f.Name(e => e.OrganizationId).IndexName("organization").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.ProjectId).IndexName("project").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.StackId).IndexName("stack").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.ReferenceId).IndexName("reference").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.SessionId).IndexName("session").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.Type).IndexName("type").Index(FieldIndexOption.NotAnalyzed))
                        .String(f => f.Name(e => e.Source).IndexName("source").Index(FieldIndexOption.NotAnalyzed).IncludeInAll())
                        .Date(f => f.Name(e => e.Date).IndexName("date"))
                        .String(f => f.Name(e => e.Message).IndexName("message").Index(FieldIndexOption.Analyzed).IncludeInAll())
                        .String(f => f.Name(e => e.Tags).IndexName("tag").Index(FieldIndexOption.NotAnalyzed).IncludeInAll().Boost(1.1))
                        .Boolean(f => f.Name(e => e.IsFirstOccurrence).IndexName("first"))
                        .Boolean(f => f.Name(e => e.IsFixed).IndexName("fixed"))
                        .Boolean(f => f.Name(e => e.IsHidden).IndexName("hidden"))
                        .Object<DataDictionary>(f => f.Name(e => e.Data).Properties(p2 => p2
                            .String(f2 => f2.Name(Event.KnownDataKeys.Version).Index(FieldIndexOption.NotAnalyzed))
                            .Object<RequestInfo>(f2 => f2.Name(Event.KnownDataKeys.RequestInfo).Properties(p3 => p3
                                .String(f3 => f3.Name(r => r.ClientIpAddress).IndexName("ip").Index(FieldIndexOption.Analyzed).IncludeInAll())))
                            .Object<Error>(f2 => f2.Name(Event.KnownDataKeys.Error).Properties(p3 => p3
                                .String(f3 => f3.Name(r => r.Type).Index(FieldIndexOption.Analyzed).IncludeInAll())))
                            .Object<EnvironmentInfo>(f2 => f2.Name(Event.KnownDataKeys.EnvironmentInfo).Properties(p3 => p3
                                .String(f3 => f3.Name(r => r.MachineName).Index(FieldIndexOption.Analyzed).IncludeInAll())))
                            .Object<UserInfo>(f2 => f2.Name(Event.KnownDataKeys.UserInfo).Properties(p3 => p3
                                .String(f3 => f3.Name(r => r.Identity).Index(FieldIndexOption.Analyzed).IncludeInAll().Boost(1.1))))))
                    )
                )
            );
        }
コード例 #32
0
        private static void ConfigureMapping(IElasticClient searchclient, bool deleteExistingIndexes = false)
        {
            if (deleteExistingIndexes)
            {
                searchclient.DeleteIndex(i => i.AllIndices());
            }

            if (!searchclient.IndexExists(new IndexExistsRequest(new IndexNameMarker {
                Name = ElasticSearchRepository <Stack> .StacksIndexName
            })).Exists)
            {
                searchclient.CreateIndex(ElasticSearchRepository <Stack> .StacksIndexName, d => d
                                         .AddAlias("stacks")
                                         .AddMapping <Stack>(map => map
                                                             .Dynamic(DynamicMappingOption.Ignore)
                                                             .IncludeInAll(false)
                                                             .Properties(p => p
                                                                         .String(f => f.Name(s => s.OrganizationId).IndexName("organization").Index(FieldIndexOption.NotAnalyzed))
                                                                         .String(f => f.Name(s => s.ProjectId).IndexName("project").Index(FieldIndexOption.NotAnalyzed))
                                                                         .String(f => f.Name(s => s.SignatureHash).IndexName("signature").Index(FieldIndexOption.NotAnalyzed))
                                                                         .String(f => f.Name(e => e.Type).IndexName("type").Index(FieldIndexOption.NotAnalyzed))
                                                                         .Date(f => f.Name(s => s.FirstOccurrence).IndexName("first"))
                                                                         .Date(f => f.Name(s => s.LastOccurrence).IndexName("last"))
                                                                         .String(f => f.Name(s => s.Title).IndexName("title").Index(FieldIndexOption.Analyzed).IncludeInAll().Boost(1.1))
                                                                         .String(f => f.Name(s => s.Description).IndexName("description").Index(FieldIndexOption.Analyzed).IncludeInAll())
                                                                         .String(f => f.Name(s => s.Tags).IndexName("tag").Index(FieldIndexOption.NotAnalyzed).IncludeInAll().Boost(1.2))
                                                                         .String(f => f.Name(s => s.References).IndexName("references").Index(FieldIndexOption.Analyzed).IncludeInAll())
                                                                         .Date(f => f.Name(s => s.DateFixed).IndexName("fixed"))
                                                                         .Boolean(f => f.Name(s => s.IsHidden).IndexName("hidden"))
                                                                         .Boolean(f => f.Name(s => s.IsRegressed).IndexName("regressed"))
                                                                         .Boolean(f => f.Name(s => s.OccurrencesAreCritical).IndexName("critical"))
                                                                         .Number(f => f.Name(s => s.TotalOccurrences).IndexName("occurrences"))
                                                                         )
                                                             )
                                         );
            }

            searchclient.PutTemplate(ElasticSearchRepository <PersistentEvent> .EventsIndexName, d => d
                                     .Template(ElasticSearchRepository <PersistentEvent> .EventsIndexName + "-*")
                                     .AddMapping <PersistentEvent>(map => map
                                                                   .Dynamic(DynamicMappingOption.Ignore)
                                                                   .IncludeInAll(false)
                                                                   .DisableSizeField(false)
                                                                   .Properties(p => p
                                                                               .String(f => f.Name(e => e.OrganizationId).IndexName("organization").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.ProjectId).IndexName("project").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.StackId).IndexName("stack").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.ReferenceId).IndexName("reference").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.SessionId).IndexName("session").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.Type).IndexName("type").Index(FieldIndexOption.NotAnalyzed))
                                                                               .String(f => f.Name(e => e.Source).IndexName("source").Index(FieldIndexOption.NotAnalyzed).IncludeInAll())
                                                                               .Date(f => f.Name(e => e.Date).IndexName("date"))
                                                                               .String(f => f.Name(e => e.Message).IndexName("message").Index(FieldIndexOption.Analyzed).IncludeInAll())
                                                                               .String(f => f.Name(e => e.Tags).IndexName("tag").Index(FieldIndexOption.NotAnalyzed).IncludeInAll().Boost(1.1))
                                                                               .Boolean(f => f.Name(e => e.IsFirstOccurrence).IndexName("first"))
                                                                               .Boolean(f => f.Name(e => e.IsFixed).IndexName("fixed"))
                                                                               .Boolean(f => f.Name(e => e.IsHidden).IndexName("hidden"))
                                                                               .Object <DataDictionary>(f => f.Name(e => e.Data).Properties(p2 => p2
                                                                                                                                            .String(f2 => f2.Name(Event.KnownDataKeys.Version).Index(FieldIndexOption.NotAnalyzed))
                                                                                                                                            .Object <RequestInfo>(f2 => f2.Name(Event.KnownDataKeys.RequestInfo).Properties(p3 => p3
                                                                                                                                                                                                                            .String(f3 => f3.Name(r => r.ClientIpAddress).IndexName("ip").Index(FieldIndexOption.Analyzed).IncludeInAll())))
                                                                                                                                            .Object <Error>(f2 => f2.Name(Event.KnownDataKeys.Error).Properties(p3 => p3
                                                                                                                                                                                                                .String(f3 => f3.Name(r => r.Type).Index(FieldIndexOption.Analyzed).IncludeInAll())))
                                                                                                                                            .Object <EnvironmentInfo>(f2 => f2.Name(Event.KnownDataKeys.EnvironmentInfo).Properties(p3 => p3
                                                                                                                                                                                                                                    .String(f3 => f3.Name(r => r.MachineName).Index(FieldIndexOption.Analyzed).IncludeInAll())))
                                                                                                                                            .Object <UserInfo>(f2 => f2.Name(Event.KnownDataKeys.UserInfo).Properties(p3 => p3
                                                                                                                                                                                                                      .String(f3 => f3.Name(r => r.Identity).Index(FieldIndexOption.Analyzed).IncludeInAll().Boost(1.1))))))
                                                                               )
                                                                   )
                                     );
        }
コード例 #33
0
        public virtual void DeleteIndexes(IElasticClient client, IEnumerable<IElasticIndex> indexes = null) {
            if (indexes == null)
                indexes = GetIndexes();
            
            foreach (var idx in indexes) {
                IIndicesResponse deleteResponse;

                var templatedIndex = idx as ITemplatedElasticIndex;
                if (templatedIndex != null) {
                    deleteResponse = client.DeleteIndex(idx.VersionedName + "-*");

                    if (client.TemplateExists(idx.VersionedName).Exists) {
                        var response = client.DeleteTemplate(idx.VersionedName);
                        Debug.Assert(response.IsValid, response.ServerError != null ? response.ServerError.Error : "An error occurred deleting the index template.");
                    }
                } else {
                    deleteResponse = client.DeleteIndex(idx.VersionedName);
                }

                Debug.Assert(deleteResponse.IsValid, deleteResponse.ServerError != null ? deleteResponse.ServerError.Error : "An error occurred deleting the indexes.");
            }
        }
コード例 #34
0
 public void Dispose()
 {
     _nestClient.DeleteIndex(_userIndex);
     _nestClient.DeleteIndex(_roleIndex);
     _nestClient.DeleteIndex(_userRoleIndex);
 }
コード例 #35
0
 public void DeleteIndex(string indexName)
 {
     var resopnse = _client.DeleteIndex(indexName);
 }