有没有办法在Elasticsearch中优化查询?我正在使用以下查询。它的平均值为15-20s
,有时甚至更快4-5s
。
我的服务器配置: - Centos 6.3,8核心16GB RAM
{
"fields": [
"_id",
"aff_id",
"post_uri",
"blog_cat",
"cat_score",
"secondary_cat",
"secondary_cat_score",
"title",
"_score"
],
"min_score": 0.0134,
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"title"
],
"query": "Archery OR Athletics OR Badminton OR Basketball OR Beach Volleyball OR Boxing OR Canoe Slalom OR Canoe Sprint OR Cycling BMX OR Cycling Mountain Bike OR Cycling Road OR Cycling Track OR Diving OR Equestrian / Dressage OR Equestrian / Eventing OR Equestrian / Jumping OR Fencing OR Football OR Golf OR Gymnastics Artistic"
}
}
],
"must_not": [],
"should": []
}
}
我读过有关Elasticsearch查询优化的文章
https://speakerdeck.com/elasticsearch/query-optimization-go-more-faster-better
尝试过如下所示的解决方案更改查询,但没有任何区别。
{
"fields": [
"aff_id",
"post_uri",
"blog_cat",
"cat_score",
"secondary_cat",
"secondary_cat_score",
"title"
],
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"url.cat": "sports"
}
},
{
"range": {
"main_cat.sports": {
"gte": ".15"
}
}
}
]
}
},
"filter": {
"query": {
"query_string": {
"fields": [
"body",
"title"
],
"query": "Archery OR Athletics OR Badminton OR Basketball OR Beach Volleyball OR Boxing OR Canoe Slalom OR Canoe Sprint OR Cycling BMX OR Cycling Mountain Bike OR Cycling Road OR Cycling Track OR Diving OR Equestrian / Dressage OR Equestrian / Eventing OR Equestrian / Jumping OR Fencing OR Football OR Golf OR Gymnastics Artistic"
}
}
}
}
},
"from": 0,
"size": 1000
}
注意:我使用默认分析器我还没有定义自定义分析器。
答案 0 :(得分:0)
以下是查询的优化版本: -
更改: -
1)在查询中使用filters
,缓存filter
2)默认情况下不会缓存query_string/query
过滤器,但我们可以通过设置_cache: true
来启用它。
{
"fields": [
"aff_id",
"post_uri",
"blog_cat",
"cat_score",
"secondary_cat",
"secondary_cat_score",
"title"
],
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"url.cat": "sports"
}
},
{
"range": {
"main_cat.sports": {
"gte": ".15"
}
}
},
{
"fquery": {
"query": {
"query_string": {
"fields": [
"body",
"title"
],
"query": "Archery OR Athletics OR Badminton OR Basketball OR Beach Volleyball OR Boxing OR Canoe Slalom OR Canoe Sprint OR Cycling BMX OR Cycling Mountain Bike OR Cycling Road OR Cycling Track OR Diving OR Equestrian / Dressage OR Equestrian / Eventing OR Equestrian / Jumping OR Fencing OR Football OR Golf OR Gymnastics Artisti"
}
},
"_cache": true
}
}
]
}
}
}
},
"from": 0,
"size": 1000,
"sort": [
{
"_uid": "desc"
}
]
}