示例#1
0
        public async Task CreateIndexIfNeededAsync(CancellationToken cancellationToken = default)
        {
            CheckEsEnabled();

            var client = _clientProvider.GetClient();

            var existsResponse = await client.Indices.ExistsAsync(_options.IndexName, ct : cancellationToken);

            HandleError(existsResponse);

            if (!existsResponse.Exists)
            {
                HandleError(await client.Indices.CreateAsync(_options.IndexName, c => c
                                                             .Map <EsDocument>(m =>
                {
                    return(m
                           .Properties(p => p
                                       .Keyword(x => x.Name(d => d.Id))
                                       .Keyword(x => x.Name(d => d.ProjectId))
                                       .Keyword(x => x.Name(d => d.Name))
                                       .Keyword(x => x.Name(d => d.FileName))
                                       .Keyword(x => x.Name(d => d.Version))
                                       .Keyword(x => x.Name(d => d.LanguageCode))
                                       .Text(x => x.Name(d => d.Content))));
                }), cancellationToken));
            }
        }
 /// <summary>
 /// 初始化一个<see cref="ElasticsearchContext"/>类型的实例
 /// </summary>
 /// <param name="provider">ES客户端提供程序</param>
 /// <param name="resolver">索引名称解析器</param>
 /// <param name="options">ES选项配置</param>
 public ElasticsearchContext(IElasticClientProvider provider, IIndexNameResolver resolver, IOptions <ElasticsearchOptions> options)
 {
     _provider = provider;
     _resolver = resolver;
     _client   = provider.GetClient();
     _options  = options.Value;
 }
 /// <summary>
 /// 获取客户端
 /// </summary>
 /// <typeparam name="T">实体类型</typeparam>
 public IElasticClient GetClient <T>() => _provider.GetClient(GetIndexName(Helper.SafeIndexName <T>()));