Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Latest commit

 

History

History
89 lines (67 loc) · 2.31 KB

File metadata and controls

89 lines (67 loc) · 2.31 KB

中 | EN

Masa.Utils.Data.Elasticsearch

用例:

Install-Package Masa.Utils.Data.Elasticsearch

基本用法:

使用Elasticsearch

builder.Services.AddElasticsearch("es", "http://localhost:9200"); // 或者builder.Services.AddElasticsearchClient("es", "http://localhost:9200");

创建索引:

public async Task<string> CreateIndexAsync([FromServices] IMasaElasticClient client)
{
    string indexName = "user_index_1";
    await client.CreateIndexAsync(indexName);
}

删除索引:

public async Task<string> DeleteIndexAsync([FromServices] IMasaElasticClient client)
{
    string indexName = "user_index_1";
    await client.DeleteIndexAsync(indexName);
}

根据别名删除索引:

public async Task<string> DeleteIndexByAliasAsync([FromServices] IMasaElasticClient client)
{
    string alias = "userIndex";
    await client.DeleteIndexByAliasAsync(alias);
}

绑定别名

public async Task<string> BindAliasAsync([FromServices] IMasaElasticClient client)
{
    string indexName = "user_index_1";
    string indexName2 = "user_index_2";
    string alias = "userIndex";
    await client.BindAliasAsync(new BindAliasIndexOptions(alias, new[] { indexName, indexName2 });
}

解除别名绑定

public async Task<string> BindAliasAsync([FromServices] IMasaElasticClient client)
{
    string indexName = "user_index_1";
    string indexName2 = "user_index_2";
    string alias = "userIndex";
    await client.UnBindAliasAsync(new UnBindAliasIndexOptions(alias, new[] { indexName, indexName2 }));
}

更多方法请查看IMasaElasticClient

常见问题

  1. 出错提示为:"Content-Type header [application/vnd.elasticsearch+json; compatible-with=7] is not supported"

    我们默认启用兼容模式,即EnableApiVersioningHeader(true),这样对8.*版本支持很好,但在部分7.*会导致错误,此时需要手动关闭兼容模式,即EnableApiVersioningHeader(false)

    service.AddElasticsearchClient("es", option =>
    {
        option.UseNodes("http://localhost:9200")
            .UseConnectionSettings(setting => setting.EnableApiVersioningHeader(false));
    });

为何开启兼容模式?