当我在查询中使用random_score时,我发现大约需要150毫秒才能得到查询结果(如果不使用random_score,大约需要250毫秒才能得到结果)。
原始查询
{
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"sound_title",
"album_title"
],
"query": "0"
}
}
]
}
},
"functions": [
{
"filter": {
"bool": {
"must": [
{
"term": {
"sound_chapters": 1
}
}
]
}
},
"weight": 1.2
},
{
"field_value_factor": {
"field": "album_playcount",
"modifier": "log",
"missing": "100"
}
}
],
"score_mode": "sum"
}
}
}
使用 random_score 进行查询
{
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"sound_title",
"album_title"
],
"query": "0"
}
}
]
}
},
"functions": [
{
"filter": {
"bool": {
"must": [
{
"term": {
"sound_chapters": 1
}
}
]
}
},
"weight": 1.2
},
{
"field_value_factor": {
"field": "album_playcount",
"modifier": "log",
"missing": "100"
}
},
{
"random_score": {
"seed": "123"
}
}
],
"score_mode": "sum"
}
}
}
有什么方法可以优化查询以提高更好的性能吗?
最佳答案
如果您要向任何 ElasticSearch
查询添加更多函数,ES
将需要一些处理时间才能将该函数的分数包含到主分数中,从而增加总体时间。
您可以阅读有关 elasticsearch 中的随机评分 here 的更多信息.
提高查询性能的最常见方法是使用过滤器
而不是查询
,因为过滤器会被缓存。但如果您的服务器中有足够的内存,则应该这样做。
您可以从博客 here 获得更多想法.
关于使用 random_score 的 elasticsearch 会降低性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717156/