Remove 'include_model_definition', support ES ML <7.8

This commit is contained in:
Seth Michael Larson 2020-09-28 15:56:10 -05:00 committed by Seth Michael Larson
parent 4d96ad39fd
commit b206b851bf

View File

@ -195,6 +195,15 @@ class MLModel:
@property @property
def model_type(self) -> str: def model_type(self) -> str:
# Legacy way of finding model_type from the model definition.
if "inference_config" not in self._trained_model_config:
trained_model = self._trained_model_config["definition"]["trained_model"]
if "tree" in trained_model:
target_type = trained_model["tree"]["target_type"]
else:
target_type = trained_model["ensemble"]["target_type"]
return cast(str, target_type)
inference_config = self._trained_model_config["inference_config"] inference_config = self._trained_model_config["inference_config"]
if "classification" in inference_config: if "classification" in inference_config:
return TYPE_CLASSIFICATION return TYPE_CLASSIFICATION
@ -208,6 +217,8 @@ class MLModel:
@property @property
def results_field(self) -> str: def results_field(self) -> str:
if "inference_config" not in self._trained_model_config:
return "predicted_value"
return cast( return cast(
str, str,
self._trained_model_config["inference_config"][self.model_type][ self._trained_model_config["inference_config"][self.model_type][
@ -414,9 +425,7 @@ class MLModel:
Check if the model already exists in Elasticsearch Check if the model already exists in Elasticsearch
""" """
try: try:
self._client.ml.get_trained_models( self._client.ml.get_trained_models(model_id=self._model_id)
model_id=self._model_id, include_model_definition=False
)
except elasticsearch.NotFoundError: except elasticsearch.NotFoundError:
return False return False
return True return True
@ -425,7 +434,15 @@ class MLModel:
def _trained_model_config(self) -> Dict[str, Any]: def _trained_model_config(self) -> Dict[str, Any]:
"""Lazily loads an ML models 'trained_model_config' information""" """Lazily loads an ML models 'trained_model_config' information"""
if self._trained_model_config_cache is None: if self._trained_model_config_cache is None:
resp = self._client.ml.get_trained_models(model_id=self._model_id)
# In Elasticsearch 7.7 and earlier you can't get
# target type without pulling the model definition
# so we check the version first.
kwargs = {}
if es_version(self._client) < (7, 8):
kwargs["include_model_definition"] = True
resp = self._client.ml.get_trained_models(model_id=self._model_id, **kwargs)
if resp["count"] > 1: if resp["count"] > 1:
raise ValueError(f"Model ID {self._model_id!r} wasn't unambiguous") raise ValueError(f"Model ID {self._model_id!r} wasn't unambiguous")
elif resp["count"] == 0: elif resp["count"] == 0: