Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions eland/ml/ltr/ltr_model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,61 @@ def feature_index(self, feature_name: str) -> int:
"Returns the index of the feature in the feature lists."

return self.feature_names.index(feature_name)

@classmethod
def from_dict(cls, d: Mapping[str, Any]) -> "LTRModelConfig":
"""
Create an LTRModelConfig from a dict.

Parameters
----------
d: Mapping[str, Any]
Dict representing the LTR model config.

Examples
--------
>>> from eland.ml.ltr import LTRModelConfig

>>> ltr_model_config_dict = {
... "learning_to_rank": {
... "feature_extractors": [
... {
... "query_extractor": {
... "feature_name": "title_bm25",
... "query": { "match": { "title": "{{query}}" } }
... }
... },
... {
... "query_extractor": {
... "feature_name": "description_bm25",
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a spelling error in the example - 'description_bm25' should match the variable name 'descritption_bm25' used in the PR description, or the PR description should be corrected to use 'description_bm25'.

Copilot uses AI. Check for mistakes.
... "query": { "match": { "description": "{{query}}" } }
... }
... }
... ]
... }
... }

>>> ltr_model_config = LTRModelConfig.from_dict(ltr_model_config_dict)
"""
if TYPE_LEARNING_TO_RANK not in d:
raise ValueError(
f"Invalid LTR model config, missing '{TYPE_LEARNING_TO_RANK}' key"
)

feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
Comment on lines +198 to +200
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing validation for the 'feature_extractors' key. If the dictionary doesn't contain this key, a KeyError will be raised. Add a check to ensure 'feature_extractors' exists in the nested dictionary.

Suggested change
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
if "feature_extractors" not in d[TYPE_LEARNING_TO_RANK]:
raise ValueError(
f"Invalid LTR model config, missing 'feature_extractors' key in '{TYPE_LEARNING_TO_RANK}'"
)
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:

Copilot uses AI. Check for mistakes.
fe = feature_extractor["query_extractor"]
feature_extractors.append(
QueryFeatureExtractor(
feature_name=fe["feature_name"],
query=fe["query"],
default_score=fe.get("default_score"),
)
)
else:
raise ValueError(
f"Unknown feature extractor type: {list(feature_extractor.keys())}"
)

return cls(feature_extractors=feature_extractors)