ElasticsearchCRUD使用(十五)【批量插入数据】

本文介绍如何使用别名和bulk插入大量文档来设置Elasticsearch索引。 批量插入大量文档时,可以通过关闭刷新间隔(RefreshInterval="-1")并关闭复制来提高性能。 插入完成后,根据您的要求将这些设置设置为所需的值。

要创建索引,使用TestDto类。 这被映射到testdtos_v1索引和testdto类型。 然后将一个别名testdtos添加到索引中。 这是非常有用的,如果需要实时索引,或者数据类型更改。 搜索客户端使用别名。创建索引时,将设置RefreshIntervalNumberOfReplicas属性。 这些值被设置为使得刷新被关闭并且复制也被关闭。 这些建议可以在Elasticsearch文档中找到。

public void CreateIndexWithAlias()
{       
    IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
    elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(TestDto), new ElasticsearchMappingTestDto());
    using (var context = new ElasticsearchContext( ConnectionString, new ElasticsearchSerializerConfiguration(elasticsearchMappingResolver, true, true)))
    {
        context.TraceProvider = new ConsoleTraceProvider();

        context.IndexCreate<TestDto>(
            new IndexDefinition
            {
                IndexAliases = new IndexAliases
                {
                    Aliases = new List<IndexAlias>
                    {
                        // alais maps to default index name
                        new IndexAlias("testdtos")
                    }
                }, 
                IndexSettings = new IndexSettings{RefreshInterval="-1", NumberOfReplicas = 0}
            }
        );
    }
}

索引的映射可以通过实现ElasticsearchMapping类从默认映射更改。 任何索引或类型可以在这里为任何DTO类设置,然后将映射添加到上下文中。 只要上下文存在,该映射就被用于该类。 只需要使用ElasticsearchMappingTestDto映射来创建索引,否则使用别名,它与ElasticsearchCRUD中的默认设置相匹配。

public class ElasticsearchMappingTestDto : ElasticsearchMapping
{
    public override string GetIndexForType(Type type)
    {
        return "testdtos_v1";
    }
}

可以使用_settings API在Elasticsearch中查看设置:

http://localhost:9200/_settings

"refresh_interval":"-1""number_of_replicas":"0"已根据需要进行了更改。

{
    "testdtos_v1": {
        "settings": {
            "index": {
                "creation_date": "1422473302831",
                "uuid": "luOwcuQiRyqxX3IvTTovWg",
                "number_of_replicas": "0",
                "number_of_shards": "5",
                "refresh_interval": "-1",
                "version": {
                    "created": "1040299" }
            }
        }
    }
}

TestDto类具有以下映射:

{
    "testdtos_v1": {
        "mappings": {
            "testdto": {
                "properties": {
                    "description": { "type": "string" },
                    "id": { "type": "long" },
                    "info": { "type": "string" } }
            }
        }
    }
}

现在创建了索引,100个批量HTTP请求中添加了一百万个文档。 批量请求的最佳大小以及每个批量请求中文档的最佳数量取决于每个文档和Elasticsearch安装的大小。 SaveChanges方法发送批量请求。

public void DoBulkInsert()
{
    // Add a million records
    long id = 1;
    for (int i = 0; i < 100; i++)
    {
        for (int t = 0; t < 10000; t++)
        {
            var item = new TestDto
            {
                Id = id,
                Description = "this is cool",
                Info = "info"
            };
            _elasticsearchContext.AddUpdateDocument(item, item.Id);
            id++;
        }
        // add data ...
        _elasticsearchContext.SaveChanges();
        Console.WriteLine("Saved:" + (i + 1) * 10000 + " items");
    }
}

批量插入完成后,将重新启动刷新,并将复制激活到所需的量。

public void UpdateIndexRefreshIntervalTo1S()
{
    _elasticsearchContext.IndexUpdateSettings
    (
        new IndexUpdateSettings
        {
            RefreshInterval = "1s",
            NumberOfReplicas = 1
        }
    );
}

然后可以查看设置,并更新两个属性值。

http://localhost:9200/_settings
{
    "testdtos_v1": {
        "settings": {
            "index": {
                "creation_date": "1422473302831",
                "uuid": "luOwcuQiRyqxX3IvTTovWg",
                "number_of_replicas": "1",
                "number_of_shards": "5",
                "refresh_interval": "1s",
                "version": {
                    "created": "1040299" }
            }
        }
    }
}

现在,使用别名的索引可以进行搜索或任何操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值