具有功能得分的ElasticSearch查询运行速度慢10倍以上

时间:2016-06-16 23:58:55

标签: performance elasticsearch groovy

这是我没有功能评分的查询:

SELECT t1.Date as ReadingDate, Max(pressure) as Pressure, Max(temperature) as Temperature FROM 
(
  SELECT * FROM 
  (SELECT sensor.Date, "" AS pressure, sensor.value AS temperature
  FROM sensor
  WHERE sensor.topic="temperature") tbl_temp
UNION ALL
  SELECT * FROM 
  (SELECT sensor.Date, sensor.value AS pressure, "" AS temperature
  FROM sensor
  WHERE sensor.topic="pressure") tbl_pressure
) AS t1
GROUP BY t1.Date

此查询在〜250ms内返回响应。

但我需要添加一些提升因子来改善默认评分。我修改了查询以使用函数得分,但在查询后花了太长时间(~3000ms)

这是功能评分查询:

{
  "from": 200,
  "size": 25,
  "query": {
    "bool": {
    "filter": {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "terms": {
                      "cotypes.id": [
                        199
                      ]
                    }
                  },
                  "path": "cotypes"
                }
              },
              {
                "range": {
                  "relevance": {
                    "from": 6,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true
                  }
                }
              }
            ],
            "must_not": {
              "terms": {
                "ontologyId": [
                  1314696,
                  1314691
                ]
              }
            }
          }
        },
      "must": {
        "match": {
          "name.nameStandard": {
            "query": "john smith",
            "type": "boolean",
            "boost": 10
          }
        }
      }
    }
  }
}

calculate-score.groovy脚本如下:

{
  "from": 200,
  "size": 25,
  "query": {
  "function_score": {
    "query": {
    "bool": {
    "filter": {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "terms": {
                      "cotypes.id": [
                        199
                      ]
                    }
                  },
                  "path": "cotypes"
                }
              },
              {
                "range": {
                  "relevance": {
                    "from": 6,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true
                  }
                }
              }
            ],
            "must_not": {
              "terms": {
                "ontologyId": [
                  1314696,
                  1314691
                ]
              }
            }
          }
        },
      "must": {
        "match": {
          "name.nameStandard": {
            "query": "john smith",
            "type": "boolean",
            "boost": 10
          }
        }
      }
    }
    },
    "functions": [
      {
        "script_score": {
          "script": {
            "file": "calculate-score",
            "lang": "groovy",
            "params": {
              "relevance_boost": 0.5
            }
          }
        }
      }
    ],
    "boost_mode": "sum"
  }
}
}

请帮助我让查询效果更好!

提前谢谢!

0 个答案:

没有答案