本文介绍如何使用索引预热与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