Commit d639b30e authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed bad formula search.

parent 0a9dfaa2
...@@ -173,6 +173,7 @@ def render_es_result(result): ...@@ -173,6 +173,7 @@ def render_es_result(result):
return es_result return es_result
_atom_label_with_number = re.compile("[A-Z][a-z]*[0-9]+")
@api.route('/materials', methods=['POST']) @api.route('/materials', methods=['POST'])
@http_auth.login_required @http_auth.login_required
...@@ -191,7 +192,12 @@ def get_materials(page, per_page): ...@@ -191,7 +192,12 @@ def get_materials(page, per_page):
user_elements = search_by.get('element') user_elements = search_by.get('element')
elements = None elements = None
if user_formula is not None: if user_formula is not None:
elements = reformat_user_formula(user_formula) parts = [element if elementreformat_user_formula(user_formula)
parts = [part if _atom_label_with_number.match(part) else '%s1' % part for part in parts]
if exclusive:
match_query['formula_reduced_keyword'] = dict(query="".join(sort_elements(elements)))
else:
match_query['formula_reduced_terms'] = dict(query=" ".join(elements), operator='and')
elif user_elements is not None: elif user_elements is not None:
try: try:
...@@ -199,7 +205,7 @@ def get_materials(page, per_page): ...@@ -199,7 +205,7 @@ def get_materials(page, per_page):
except Exception: except Exception:
raise InvalidEncRequest("Could not parse given elements %s." % user_elements, 400) raise InvalidEncRequest("Could not parse given elements %s." % user_elements, 400)
if elements is not None: elif elements is not None:
if exclusive: if exclusive:
match_query['atom_labels_keyword'] = dict(query="".join(sort_elements(elements))) match_query['atom_labels_keyword'] = dict(query="".join(sort_elements(elements)))
else: else:
......
...@@ -71,7 +71,7 @@ _numbers = { ...@@ -71,7 +71,7 @@ _numbers = {
def sort_elements(elements): def sort_elements(elements):
return sorted(elements, key=lambda element: _numbers.get(element, 200)) return sorted(elements, key=lambda element: _numbers.get(element.rstrip('0123456789'), 200))
def get_atom_number(symbol): def get_atom_number(symbol):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment