Skip to content

Commit

Permalink
Small speed related changes (#910)
Browse files Browse the repository at this point in the history
* Limit element query lengths

* Enforce type ordering on summary hint scheme

* Fix summary hint ordering
  • Loading branch information
Jason Munro authored Dec 5, 2023
1 parent 2e2ae4d commit eee8240
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def query(
elements: Optional[str] = Query(
None,
description="Query by elements in the material composition as a comma-separated list",
max_length=15,
),
exclude_elements: Optional[str] = Query(
None,
description="Query by excluded elements in the material composition as a comma-separated list",
max_length=15,
),
) -> STORE_PARAMS:
crit = {} # type: dict
Expand Down
19 changes: 17 additions & 2 deletions emmet-api/emmet/api/routes/materials/summary/hint_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ def generate_hints(self, query):
if list(query.get("criteria").keys()) != ["deprecated", "builder_meta.license"]:
pure_params = []
excluded_elements = False
for param, val in query["criteria"].items():

def check(val):
sort_priority = {float: 0, int: 1, str: 2, bool: 3}

if isinstance(val, dict):
val_list = list(val.values())
val = val_list[0] if val_list and val_list[0] else val

return sort_priority.get(type(val), 100)

sorted_raw_params = sorted(
query["criteria"].items(),
key=lambda x: check(x[1]),
)

for param, val in sorted_raw_params:
pure_param = param.split(".")[0]
pure_params.append(pure_param)
if pure_param == "composition_reduced" and val == {"$exists": False}:
Expand All @@ -24,7 +39,7 @@ def generate_hints(self, query):
elif "composition_reduced" in pure_params and not excluded_elements:
hints["count_hint"] = {"composition_reduced.$**": 1}
else:
for param in query["criteria"]:
for param, _ in sorted_raw_params:
if (
param not in ["deprecated", "builder_meta.license"]
and "composition_reduced" not in param
Expand Down

0 comments on commit eee8240

Please sign in to comment.