背景:近期使用ES摸索到一些优化方式
1:API使用,不需要打分推荐使用filter
Query context 查询上下文
这种语句在执行时既要计算文档是否匹配,还要计算文档相对于其他文档的匹配度有多高,匹配度越高,_score
分数就越高
Filter context 过滤上下文
过滤上下文中的语句在执行时只关心文档是否和查询匹配,不会计算匹配度,也就是得分。query
参数表示整个语句是处于 query context 中bool
和match
语句被用在 query context 中,也就是说它们会计算每个文档的匹配度(_score)filter
参数则表示这个子查询处于 filter context 中filter
语句中的term
和range
语句用在 filter context 中,它们只起到过滤的作用,并不会计算文档的得分。
filter示例:
GET test_index/_search
{
"from": 0,
"size": 50,
"query": {
"bool": {
"filter": [{
"terms": {
"name": ["zhangsan"],
"boost": 1.0
}
},{
"terms": {
"id": [ "0"],
"boost": 1.0
}
}, {
"terms": {
"date": [20200413],
"boost": 1.0
}
}],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"version": true
}
2:限制segment合并
3:查看缓存命中情况(适用filter)
GET _stats/query_cache
"my_index" : {
"uuid" : "xAm5VgvfT2qZBiMnmQZiYg",
"primaries" : {
"query_cache" : {
"memory_size_in_bytes" : 0,
"total_count" : 0,
"hit_count" : 0,
"miss_count" : 0,
"cache_size" : 0,
"cache_count" : 0,
"evictions" : 0
}
},
"total" : {
"query_cache" : {
"memory_size_in_bytes" : 0,
"total_count" : 0,
"hit_count" : 0,
"miss_count" : 0,
"cache_size" : 0,
"cache_count" : 0,
"evictions" : 0
}
}
}
4:查看缓存命中情况(适用request)
GET _stats/request_cache
API searchRequest.requestCache(true);
"total" : {
"request_cache" : {
"memory_size_in_bytes" : 5594,
"evictions" : 0,
"hit_count" : 5201,
"miss_count" : 4
}
}
5:性能分析
"profile": true ,"explain": true,
GET test_index/_search
{
"profile": true ,
"explain": true,
"from": 0,
"size": 50,
"query": {
"bool": {
"filter": [{
"terms": {
"name": ["zhangsan"],
"boost": 1.0
}
},{
"terms": {
"id": [ "0"],
"boost": 1.0
}
}, {
"terms": {
"date": [20200413],
"boost": 1.0
}
}],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"version": true
}
6:常用命令(kibana)
GET _settings
GET _cluster/settings
PUT /_cluster/settings
{
"persistent": {
"xpack.monitoring.history.duration":"1d",
"xpack.monitoring.collection.interval":"10s"
}
}
GET _cat/thread_pool
GET ad_index/_stats