ElasticsearchCRUD使用(十九)【索引热身】

本文介绍如何使用索引预热与ElasticsearchCRUD。 预热对于最常用的查询很有用,可以帮助您提高性能。 在创建索引时,可以添加一个预热器,在索引之后添加或从索引中删除。 还可以在全局或每种索引类型中添加预热器。

创建索引时添加预热器

在创建索引时,可以加一个预热器。 IndexCreate方法为此提供了一个Warmers属性函数。 Warmers列表可以接受查询或聚合。

_context.IndexCreate<FastestAnimal>(
new IndexDefinition
{
    IndexWarmers = new IndexWarmers
    {
        Warmers = new List<IndexWarmer>
        {
            new IndexWarmer("filter_mph")
            {
                Query= new Query(
                    new Filtered(
                        new Filter(
                            new RangeFilter("speeddoublemph")
                            {
                                GreaterThanOrEqualTo= 50.0
                            }
                        )
                    )
                )
            }
        }
    }
}
);

在HTTP PUT请求中索引设置预热器到Elasticsearch

PUT http://localhost:9200/fastestanimals HTTP/1.1
Content-Type: application/json
Host: localhost:9200
Content-Length: 198
Expect: 100-continue

{
    "settings": {
        "analysis": {

        },
        "number_of_shards": 5,
        "number_of_replicas": 1
    },
    "aliases": {

    },
    "warmers": {
        "filter_mph": {
            "source": {
                "query": { "filtered": { "filter": { "range": { "speeddoublemph": { "gte": 50.0 } } } } } }
        }
    }
}

为现有索引创建一个预热器

可以使用WarmerCreate方法将预热器添加到索引中。 如果index参数被定义,那么它是和所有名称的索引。

_context.WarmerCreate(new Warmer("all")
{
    Query = new Query(
        new MatchAllQuery()
    )
}, "fastestanimals");

预热器使用HTTP PUT请求添加到索引

PUT http://localhost:9200/fastestanimals/_warmer/all HTTP/1.1
Content-Type: text/plain; charset=utf-8
Host: localhost:9200
Content-Length: 26
Expect: 100-continue

{"query":{"match_all":{}}}

删除预热器

也可以从索引中删除预热器:

context.WarmerDelete("all", "fastestanimals");

发送如下:

DELETE http://localhost:9200/fastestanimals/_warmer/all HTTP/1.1
Host: localhost:9200
Content-Length: 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值