Ejemplo n.º 1
0
        public ElasticSearchDriver(ILogger <ElasticSearchDriver> logger, Uri uri, string index)
        {
            IndexName = index;
            Logger    = logger;


            var connectionSettings = new ConnectionSettings(uri)
                                     .DefaultIndex(index)
                                     .ThrowExceptions();

            lowLevelClient = new ElasticLowLevelClient(connectionSettings);
            client         = new ElasticClient(connectionSettings);

            if (!ExtantIndicies.Contains(index))
            {
                lock (ExtantIndicies)
                {
                    var exists = client.IndexExists(index);
                    if (!exists.Exists)
                    {
                        Logger.LogInformation("Index does not exist, creating it now");

                        var descriptor = new CreateIndexDescriptor(index)
                                         .Mappings(ms => ms
                                                   .Map <Document>(m => m.AutoMap())
                                                   );

                        var response = client.CreateIndex(descriptor);
                    }
                    ExtantIndicies.Add(index);
                }
            }
        }
        public void Inserttheaters([FromBody] Theaters theater)
        {
            //elasticClient.DeleteIndex("theater_ind");

            if (!elasticClient.Indices.Exists("theater_ind").Exists)
            {
                var indexSettings = new IndexSettings();
                indexSettings.NumberOfReplicas = 1;
                indexSettings.NumberOfShards   = 3;


                var createIndexDescriptor = new CreateIndexDescriptor("theater_ind")
                                            .Mappings(ms => ms
                                                      .Map <Theaters>(m => m.AutoMap())
                                                      )
                                            .InitializeUsing(new IndexState()
                {
                    Settings = indexSettings
                })
                                            .Aliases(a => a.Alias("theaterAlias"));

                var response = elasticClient.Indices.Create(createIndexDescriptor);
            }
            //Insert Data

            elasticClient.Index <Theaters>(theater, idx => idx.Index("theater_ind"));
        }
 public override void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     foreach (var contributor in _contributors)
     {
         contributor.Contribute(descriptor, client);
     }
 }
Ejemplo n.º 4
0
        public void Helper()
        {
            var node     = new Uri("http://myserver:9200");
            var settings = new ConnectionSettings(node);
            var client   = new ElasticClient(settings);

            var settings2 = new ConnectionSettings()
                            .DefaultIndex("defaultindex");

            //创建索引
            var descriptor = new CreateIndexDescriptor("db_student")
                             .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1));

            client.CreateIndex(descriptor);


            //删除索引
            var descriptor2 = new DeleteIndexDescriptor("db_student").Index("db_student");

            client.DeleteIndex(descriptor2);

            //删除指定索引所在节点下的所有索引
            var descriptor3 = new DeleteIndexDescriptor("db_student").AllIndices();


            var descriptor4 = new CreateIndexDescriptor("db_student")
                              .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1))
                              .Mappings(ms => ms
                                        .Map <Student>(m => m.AutoMap()));

            client.CreateIndex(descriptor4);
        }
Ejemplo n.º 5
0
        private static void IndexAccounts(IEnumerable <Account> accounts)
        {
            // Connect Elasticsearch
            // TODO: Console Usage
            var node        = new Uri("http://10.6.14.157:9200");
            var elasticUser = "******";
            var elasticPwd  = "123qwe";
            var setting     = new ConnectionSettings(node);

            setting.BasicAuthentication(elasticUser, elasticPwd);
            var elasticSearchClient = new ElasticClient(setting);


            // Index Mapping
            var descriptor = new CreateIndexDescriptor("account")
                             .Mappings(ms => ms.Map <Account>(m => m.AutoMap()));

            elasticSearchClient.CreateIndex(descriptor);

            foreach (var account in accounts)
            {
                var respose = elasticSearchClient.Index(account, idx => idx.Index("account"));
                Console.WriteLine("Indexed an account with respose : {0}", respose.Result);
            }
        }
        private CreateIndexDescriptor AddCategoryAnalyzers(CreateIndexDescriptor descriptor)
        {
            var autoComplete = new CustomAnalyzer
            {
                Filter = new List <string> {
                    "lowercase", "asciifolding", "autocomplete_filter"
                },
                Tokenizer = "standard"
            };

            var autoCompleteNative = new CustomAnalyzer
            {
                Filter = new List <string> {
                    "lowercase", "autocomplete_filter"
                },
                Tokenizer = "standard"
            };

            descriptor.Analysis(x => x
                                .TokenFilters(f => f
                                              .Add("autocomplete_filter", new EdgeNGramTokenFilter {
                MaxGram = 20, MinGram = 1
            }))
                                .Analyzers(a => a
                                           .Add("autocomplete", autoComplete)
                                           .Add("autocompletenative", autoCompleteNative)));

            return(descriptor);
        }
Ejemplo n.º 7
0
        public ElasticSearchService(ILoggerFactory loggerFactory, IConfiguration configuration)
        {
            _logger = loggerFactory.CreateLogger <ElasticSearchService>();
            Client  = new Lazy <Task <IElasticClient> >(async() =>
            {
                var pool = new SingleNodeConnectionPool(
                    new Uri(configuration.GetValue <string>("ELASTICSEARCH_ENDPOINT")));
                var connSettings = new ConnectionSettings(pool)
                                   .IncludeServerStackTraceOnError()
                                   .EnableHttpPipelining()
                                   .EnableHttpCompression();
                var client = new ElasticClient(connSettings);
                try
                {
                    var mapping =
                        new CreateIndexDescriptor(Constants.ReviewIndexName)
                        .Map <Review>(m => m
                                      .AutoMap()
                                      );

                    var existResponse = await client.Indices.ExistsAsync(Constants.ReviewIndexName);
                    if (!existResponse.Exists)
                    {
                        await client.Indices.CreateAsync(mapping);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                }

                return(client);
            }, LazyThreadSafetyMode.ExecutionAndPublication);
        }
Ejemplo n.º 8
0
        public void CreateIndex()
        {
            if (indexQuery.IsExists(Constants.SlambyMetadataIndex))
            {
                return;
            }

            var client = elasticClientFactory.GetClient();

            var descriptor = new CreateIndexDescriptor(Constants.SlambyMetadataIndex);

            descriptor
            .Settings(s => s
                      .NumberOfReplicas(0)
                      .NumberOfShards(1))
            .Mappings(m => m
                      .Map <MetadataElastic>(mm => mm
                                             .AutoMap()
                                             .Dynamic(false)
                                             ));

            var createResp = client.CreateIndex(descriptor);

            ResponseValidator(createResp);

            var metadataElastic = new MetadataElastic()
            {
                DBVersion = 0
            };

            Index(metadataElastic);
        }
        public ResultModel <bool> InsertLog(LogModel logModel)
        {
            if (!ElasticClient.IndexExists("error_log").Exists)
            {
                var indexSettings = new IndexSettings
                {
                    NumberOfReplicas = 1,
                    NumberOfShards   = 3
                };


                var createIndexDescriptor = new CreateIndexDescriptor(Constant.IndexName)
                                            .Mappings(ms => ms
                                                      .Map <LogModel>(m => m.AutoMap())
                                                      )
                                            .InitializeUsing(new IndexState()
                {
                    Settings = indexSettings
                })
                                            .Aliases(a => a.Alias("error_log"));

                var response = ElasticClient.CreateIndex(createIndexDescriptor);
            }
            //Insert Data

            ElasticClient.Index(logModel, idx => idx.Index(Constant.IndexName));

            return(new ResultModel <bool>
            {
                Value = true,
                IsSuccess = true
            });
        }
 public override void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     if (_replicas.HasValue && _replicas >= 0)
     {
         descriptor.NumberOfReplicas(_replicas.Value);
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Create index
        /// </summary>
        /// <param name="index">Index name</param>
        /// <param name="descriptor">CreateIndexDescriptor object</param>
        /// <param name="force">Force</param>
        ///
        private bool CreateIndex(string index, CreateIndexDescriptor descriptor, bool force)
        {
            if (string.IsNullOrWhiteSpace(index))
            {
                throw new ArgumentNullException("index", "An index name was not provided.");
            }

            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor", "An object of type CreateIndexDescriptor was not provided.");
            }

            var exists = IndexExists(index);

            if (!exists || force)
            {
                if (exists)
                {
                    _elasticClient.DeleteIndex(d => d.Index(index));
                }

                var response = _elasticClient.CreateIndex(index, d => descriptor);

                return(response.IsValid);
            }

            return(false);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Create index
        /// </summary>
        /// <typeparam name="T">Index type</typeparam>
        /// <param name="index">Index name</param>
        /// <param name="numberOfShards">Number of shards</param>
        /// <param name="numberOfReplicas">Number of replicas</param>
        /// <param name="force">Force</param>
        /// <param name="languageCode">Language Code Enum</param>
        ///
        public bool CreateIndex <T>(string index, int numberOfShards, int numberOfReplicas, bool force = false, LanguageCode languageCode = LanguageCode.EN)
            where T : class
        {
            var descriptor = new CreateIndexDescriptor(_connectionSettings);

            descriptor.NumberOfShards(numberOfShards);
            descriptor.NumberOfReplicas(numberOfReplicas);

            var analysis = _analysisResolver.Resolve <T>(languageCode);

            if (analysis != null)
            {
                descriptor.Analysis(a => analysis);
            }

            // Add any custom analyzers
            _analyzerResolver = new ElasticsearchAnalyzerResolver();
            if (_analysisResolver != null)
            {
                descriptor = _analyzerResolver.Resolve <T>(descriptor);
            }

            var mapping = _mappingResolver.Resolve <T>(_connectionSettings);

            if (mapping != null)
            {
                descriptor.AddMapping <T>(m => mapping);
            }

            var success = CreateIndex(index, descriptor, force);

            return(success);
        }
 public override void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     if (_shards.HasValue && _shards.Value > 0)
     {
         descriptor.NumberOfShards(_shards.Value);
     }
 }
Ejemplo n.º 14
0
        public void OverridingAutoMappedProperties()
        {
            /**
             * Here we are using AutoMap() to automatically map our company type, but then we're
             * overriding our employee property and making it a `nested` type, since by default,
             * AutoMap() will infer objects as `object`.
             */
            var descriptor = new CreateIndexDescriptor("myindex")
                             .Mappings(ms => ms
                                       .Map <Company>(m => m
                                                      .AutoMap()
                                                      .Properties(ps => ps
                                                                  .Nested <Employee>(n => n
                                                                                     .Name(c => c.Employees)
                                                                                     )
                                                                  )
                                                      )
                                       );

            var expected = new
            {
                mappings = new
                {
                    company = new
                    {
                        properties = new
                        {
                            name = new
                            {
                                type = "string"
                            },
                            employees = new
                            {
                                type = "nested",
                            }
                        }
                    }
                }
            };

            Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);

            /**
             * AutoMap is idempotent. Calling it before or after manually
             * mapped properties should still yield the same results.
             */
            descriptor = new CreateIndexDescriptor("myindex")
                         .Mappings(ms => ms
                                   .Map <Company>(m => m
                                                  .Properties(ps => ps
                                                              .Nested <Employee>(n => n
                                                                                 .Name(c => c.Employees)
                                                                                 )
                                                              )
                                                  .AutoMap()
                                                  )
                                   );

            Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
        }
Ejemplo n.º 15
0
        public virtual CreateIndexDescriptor Map(CreateIndexDescriptor descriptor)
        {
            Func <Func <TypeMappingDescriptor <T>, ITypeMapping>, CreateIndexDescriptor> seed = f =>
            {
                descriptor = descriptor.Mappings(m => m.Map <T>(f));
                return(descriptor);
            };

            var res = this.Mappers.Aggregate(seed, (x, next) =>
            {
                var returnRes = new Func <Func <TypeMappingDescriptor <T>, ITypeMapping>, CreateIndexDescriptor>
                                (
                    pr =>
                {
                    x(next.MapProperty(pr));

                    return(descriptor);
                });
                return(returnRes);
            });

            var desc = res(new Func <TypeMappingDescriptor <T>, ITypeMapping>(q => null));

            return(desc);
        }
Ejemplo n.º 16
0
 public override CreateIndexDescriptor ConfigureIndex(CreateIndexDescriptor idx)
 {
     return(base.ConfigureIndex(idx.Settings(s => s
                                             .NumberOfShards(_configuration.Options.NumberOfShards)
                                             .NumberOfReplicas(_configuration.Options.NumberOfReplicas)
                                             .Priority(5))));
 }
Ejemplo n.º 17
0
        public JsonResult DataSearch(string genre, string description)
        {
            var indexDescriptor = new CreateIndexDescriptor("importsql")
                                  .Map <importsql>(m => m.AutoMap());

            if (!string.IsNullOrEmpty(genre) && !string.IsNullOrEmpty(description))
            {
                var responsedata = _connectionToEs.EsClient().Search <importsql>(s => s
                                                                                 .Index("importsql")
                                                                                 .Size(50)
                                                                                 .Query(q => q
                                                                                        .Match(m => m
                                                                                               .Field(f => f.genre)
                                                                                               .Query(genre)
                                                                                               ) &&
                                                                                        q
                                                                                        .Match(m => m
                                                                                               .Field(f => f.description)
                                                                                               .Query(description)
                                                                                               ))
                                                                                 );

                var datasend = (from hits in responsedata.Hits
                                select hits.Source).ToList();

                return(Json(new { datasend, responsedata.Took }, behavior: JsonRequestBehavior.AllowGet));
            }
            else if (!string.IsNullOrEmpty(genre))
            {
                var responsedata = _connectionToEs.EsClient().Search <importsql>(s => s
                                                                                 .Index("importsql")
                                                                                 .Size(50)
                                                                                 .Query(q => q
                                                                                        .Match(m => m
                                                                                               .Field(f => f.genre)
                                                                                               .Query(genre)
                                                                                               )));

                var datasend = (from hits in responsedata.Hits
                                select hits.Source).ToList();

                return(Json(new { datasend, responsedata.Took }, behavior: JsonRequestBehavior.AllowGet));
            }
            else if (!string.IsNullOrEmpty(description))
            {
                var responsedata = _connectionToEs.EsClient().Search <importsql>(s => s
                                                                                 .Index("importsql")
                                                                                 .Size(50)
                                                                                 .Query(q => q
                                                                                        .Match(m => m
                                                                                               .Field(f => f.description)
                                                                                               .Query(description)
                                                                                               )));
                var datasend = (from hits in responsedata.Hits
                                select hits.Source).ToList();

                return(Json(new { datasend, responsedata.Took }, behavior: JsonRequestBehavior.AllowGet));
            }
            return(Json(data: null, behavior: JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 18
0
        public void IgnoringProperties()
        {
            var descriptor = new CreateIndexDescriptor("myindex")
                             .Mappings(ms => ms
                                       .Map <CompanyWithAttributesAndPropertiesToIgnore>(m => m
                                                                                         .AutoMap()
                                                                                         )
                                       );

            /** Thus we do not map properties on the second occurrence of our Child property */
            var expected = new
            {
                mappings = new
                {
                    company = new
                    {
                        properties = new
                        {
                            name = new
                            {
                                type = "string"
                            }
                        }
                    }
                }
            };

            var settings = WithConnectionSettings(s => s
                                                  .InferMappingFor <CompanyWithAttributesAndPropertiesToIgnore>(i => i
                                                                                                                .Ignore(p => p.AnotherPropertyToIgnore)
                                                                                                                )
                                                  );

            settings.Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
        }
        public virtual IndexResult CreateIndex <T>(CreateIndexDescriptor createIndexDescriptor) where T : class
        {
            ElasticClient elasticClient = this.DbContextFactory.GetDbContext() as ElasticClient;

            string indexName = typeof(T).Name.ToLower();
            string aliasName = string.Format("{0}_{1}", "alias", indexName);

            if (createIndexDescriptor == null)
            {
                createIndexDescriptor = new CreateIndexDescriptor(indexName)
                                        .Mappings(ms => ms
                                                  .Map <T>(m => m.AutoMap()))
                                        .Aliases(a => a.Alias(aliasName));
            }

            var request = new IndexExistsRequest(indexName);
            var result  = elasticClient.Indices.Exists(request);

            if (result.Exists)
            {
                return(null);
            }

            var response = elasticClient.Indices.Create(createIndexDescriptor);

            return(new IndexResult
            {
                IsValid = response.IsValid,
                ResponseMessage = response.DebugInformation,
                Exception = response.OriginalException
            });
        }
Ejemplo n.º 20
0
        public ElasticSearchLogReadRepository(ILog log, IAppConfiguration config)
            : base(log, config)
        {
            var node           = new Uri(config.ElasticSearchIndexStoreSettings.ServerName);
            var connectionPool = new SingleNodeConnectionPool(node);

            var settings = new ConnectionSettings(connectionPool, (Func <ConnectionSettings, IElasticsearchSerializer>)null);

            client = new ElasticClient(settings);

            appLogIndexName  = (config.ElasticSearchIndexStoreSettings.AppLogIndex ?? "").ToLower();
            perfLogIndexName = (config.ElasticSearchIndexStoreSettings.PerformanceLogIndex ?? "").ToLower();

            //client.DeleteIndex(perfLogIndexName);

            if (client.IndexExists(perfLogIndexName).Exists == false)
            {
                client.CreateIndex(perfLogIndexName);
            }

            //client.DeleteIndex(appLogIndexName);

            if (client.IndexExists(appLogIndexName).Exists == false)
            {
                var mappings = new CreateIndexDescriptor(appLogIndexName).Mappings(ms => ms.Map <AppLog>(map => map.AutoMap()));
                var resp     = client.CreateIndex(mappings);
            }
        }
Ejemplo n.º 21
0
        [U] public void HidesInheritedMembers()
        {
            var descriptor = new CreateIndexDescriptor("myindex")
                             .Mappings(ms => ms
                                       .Map <DictionaryDocument>(m => m.AutoMap(new IgnoreInheritedPropertiesVisitor <DictionaryDocument>()))
                                       );

            /**
             */
            // json
            var expected = new
            {
                mappings = new
                {
                    dictionarydocument = new
                    {
                        properties = new
                        {
                            id = new
                            {
                                type = "integer"
                            }
                        }
                    }
                }
            };

            // hide
            Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
        }
        public async Task CreateIndexAsync(string indexName)
        {
            var createIndexDescriptor = new CreateIndexDescriptor(indexName.ToLowerInvariant())
                                        .Mappings(m => m.Map <Product>(p => p.AutoMap()));

            await _elasticClient.CreateIndexAsync(createIndexDescriptor);
        }
Ejemplo n.º 23
0
 public override CreateIndexDescriptor ConfigureIndex(CreateIndexDescriptor idx)
 {
     return(base.ConfigureIndex(idx.Settings(s => s
                                             .NumberOfShards(_settings.ElasticsearchNumberOfShards)
                                             .NumberOfReplicas(_settings.ElasticsearchNumberOfReplicas)
                                             .Priority(5))));
 }
Ejemplo n.º 24
0
        public void CreateIndex()
        {
            if (!_client.IndexExists(ElasticConfig.IndexName).Exists)
            {
                var settings = GetIndexSettings();

                var indexDescriptor = new CreateIndexDescriptor(ElasticConfig.IndexName)
                                      .Mappings(ms => ms
                                                .Map <Post>(m => m
                                                            .Properties(props => props
                                                                        .Text(t => t
                                                                              .Name(post => post.Title)
                                                                              .Analyzer("english")
                                                                              )
                                                                        .Text(t => t
                                                                              .Name(post => post.Body)
                                                                              .Analyzer("english")
                                                                              )
                                                                        .Keyword(k => k
                                                                                 .Name(post => post.Tags)
                                                                                 )
                                                                        .Completion(c => c
                                                                                    .Name(post => post.Suggest)
                                                                                    )
                                                                        )
                                                            .AutoMap()
                                                            )
                                                )
                                      .Settings(s => settings);

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

            BulkIndex(HostingEnvironment.MapPath("~/data/posts.xml"), 20000);
        }
 public static CreateIndexDescriptor CreateBackofficeCourseDescriptor(this CreateIndexDescriptor descriptor)
 {
     return(descriptor?.Map <BackofficeCourse>(m => m.AutoMap().Properties(pr => pr
                                                                           .Keyword(i => i.Name("id"))
                                                                           .Keyword(i => i.Name("name"))
                                                                           )));
 }
Ejemplo n.º 26
0
        public virtual async Task CreateIndexCustomSuggestAsync <T, TKey>(string indexName) where T : ElasticEntity <TKey>
        {
            var exis = await ElasticSearchClient.IndexExistsAsync(indexName);

            if (exis.Exists)
            {
                return;
            }
            var newName = indexName + DateTime.Now.Ticks;

            var createIndexDescriptor = new CreateIndexDescriptor(newName)
                                        .Settings(o => o.NumberOfShards(1).NumberOfReplicas(1).Setting("max_result_window", int.MaxValue))
                                        .Mappings(ms => ms
                                                  .Map <T>(m => m
                                                           .AutoMap()
                                                           .Properties(ps => ps
                                                                       .Completion(c => c
                                                                                   .Contexts(ctx => ctx.Category(csg => csg.Name("userId").Path("u")))
                                                                                   .Name(d => d.Suggest)
                                                                                   ))));

            var result = await ElasticSearchClient
                         .CreateIndexAsync(createIndexDescriptor);

            if (result.Acknowledged)
            {
                await ElasticSearchClient.AliasAsync(al => al.Add(add => add.Index(newName).Alias(indexName)));

                return;
            }
            throw new ElasticSearchException($"Create Index {indexName} failed : :" + result.ServerError.Error.Reason);
        }
Ejemplo n.º 27
0
 /// <inheritdoc cref="IIndexConfigurator.Configure" />
 public CreateIndexDescriptor Configure(CreateIndexDescriptor descriptor)
 {
     return(descriptor.Analysis(y => y
                                .Analyzers(z => z
                                           /* Code : aucun traitement. */
                                           .Add("code", new CustomAnalyzer("code")
     {
         Tokenizer = "keyword",
         Filter = new List <string> {
             "standard"
         }
     })
                                           /* Texte français pour la recherche : normalisé et découpé. */
                                           .Add("text_fr", new CustomAnalyzer("text_fr")
     {
         Tokenizer = "pattern",
         Filter = new List <string> {
             "standard", "lowercase", "asciifolding"
         }
     })
                                           /* Tri : normalisé pas mais pas découpé. */
                                           .Add("sort", new CustomAnalyzer("sort")
     {
         Tokenizer = "keyword",
         Filter = new List <string> {
             "standard", "lowercase", "asciifolding"
         }
     }))
                                .Tokenizers(w => w
                                            .Add("keyword", new KeywordTokenizer())
                                            .Add("pattern", new PatternTokenizer {
         Pattern = @"([^\p{L}\d^&^-^.]+)"
     }))));
 }
Ejemplo n.º 28
0
        //This version includes the race information that goes with the triathlete
        //it requires that the Triathlete.Race be available for serialization and all of the nested classes.
        public void ReIndexObsolete()
        {
            var client = SetupElasticSearch();

            var descriptor = new CreateIndexDescriptor("mytriindex")
                             .Mappings(ms => ms
                                       .Map <Triathlete>(m => m.AutoMap())
                                       .Map <RequestContext>(m => m.AutoMap())
                                       );


            int count = 0;

            foreach (var t in _DBContext.Triathletes
                     .Include("RequestContext")
                     .Include("RequestContext.Race")
                     .Include("RequestContext.Gender")
                     .Include("RequestContext.AgeGroup")
                     .Include("RequestContext.Race.Conditions")
                     )
            {
                RequestContext r  = t.RequestContext;
                Race           ra = t.Race;

                count++;
                client.Index(t, idx => idx.Index("mytriindex"));

                if (count > 10)
                {
                    break;
                }
            }
        }
Ejemplo n.º 29
0
 /// <summary>
 /// 设置shards和replicas和model搜索deep
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="x"></param>
 /// <param name="shards"></param>
 /// <param name="replicas"></param>
 /// <param name="deep"></param>
 /// <returns></returns>
 public static CreateIndexDescriptor GetCreateIndexDescriptor <T>(this CreateIndexDescriptor x,
                                                                  int shards = 5, int replicas = 1, int deep = 5)
     where T : class, IElasticSearchIndex
 {
     return(x.Settings(s =>
                       s.NumberOfShards(shards).NumberOfReplicas(replicas)).Mappings(map => map.Map <T>(m => m.AutoMap(deep))));
 }
        public ElasticsearchMonitoringProvider(string uri, string indexName, string username,
                                               string password, bool recreateIndex, ILogger logger)
        {
            var connectionSettings = new ConnectionSettings(new Uri(uri)).DefaultIndex(indexName).ThrowExceptions();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                connectionSettings = connectionSettings.BasicAuthentication(username, password);
            }

            elasticClient = new ElasticClient(connectionSettings);

            if (recreateIndex)
            {
                logger.LogInformation($"Recreating index {indexName}");
                if (elasticClient.IndexExists(indexName).Exists)
                {
                    elasticClient.DeleteIndex(indexName);
                }
            }

            if (!elasticClient.IndexExists(indexName).Exists)
            {
                var descriptor = new CreateIndexDescriptor(indexName)
                                 .Mappings(ms => ms
                                           .Map <ReceivedMessage>(m => m.AutoMap())
                                           .Map <SentMessage>(m => m.AutoMap())
                                           );

                elasticClient.CreateIndex(indexName, d => descriptor);
            }
        }
Ejemplo n.º 31
0
        private void CreateTracingIndexExecute(string index)
        {
            _logger.LogInformation($"Not exists index {index}.");

            var tracingIndex = new CreateIndexDescriptor(index);

            tracingIndex.Mappings(x =>
                                  x.Map <Span>(m => m
                                               .AutoMap()
                                               .Properties(p => p.Keyword(t => t.Name(n => n.TraceId)))
                                               .Properties(p => p.Keyword(t => t.Name(n => n.SpanId)))
                                               .Properties(p => p.Nested <Tag>(n => n.Name(name => name.Tags).AutoMap()))));

            var response = _elasticClient.CreateIndex(tracingIndex);

            if (response.IsValid)
            {
                _logger.LogInformation($"Create index {index} success.");
            }
            else
            {
                var exception = new InvalidOperationException($"Create index {index} error : {response.ServerError}");
                _logger.LogError(exception, exception.Message);
                throw exception;
            }
        }
Ejemplo n.º 32
0
        public virtual CreateIndexDescriptor CreateIndex(CreateIndexDescriptor idx)
        {
            const string SET_FIXED_SCRIPT = @"ctx._source['fixed'] = !!ctx._source['date_fixed']";

            return(idx.AddMapping <Stack>(map => map
                                          .Dynamic(DynamicMappingOption.Ignore)
                                          .Transform(t => t.Script(SET_FIXED_SCRIPT).Language(ScriptLang.Groovy))
                                          .IncludeInAll(false)
                                          .Properties(p => p
                                                      .String(f => f.Name(e => e.Id).IndexName("id").Index(FieldIndexOption.NotAnalyzed).IncludeInAll())
                                                      .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(Fields.Stack.FirstOccurrence))
                                                      .Date(f => f.Name(s => s.LastOccurrence).IndexName(Fields.Stack.LastOccurrence))
                                                      .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.Analyzed).IncludeInAll().Boost(1.2))
                                                      .String(f => f.Name(s => s.References).IndexName("links").Index(FieldIndexOption.Analyzed).IncludeInAll())
                                                      .Date(f => f.Name(s => s.DateFixed).IndexName("fixedon"))
                                                      .Boolean(f => f.Name("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"))
                                                      )));
        }
 public void Contribute(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     if (CanContribute(descriptor, client))
     {
         ContributeCore(descriptor, client);
         ContributionComplete();
     }
     else
     {
         ContributionRejected();
     }
 }
 public abstract void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client);
 public override void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     descriptor.AddAlias(_aliasName, AddAliasCore);
 }
 public override void ContributeCore(CreateIndexDescriptor descriptor, IElasticClient client)
 {
     Debug.WriteLine("Contributed {0}", GetType().Name);
 }