我目前正在一个项目中,以json格式将来自分子的数据存储在Elasticsearch数据库中。
我已经有一个查询完整公式并返回包含完整公式的文档的功能,事实是当我搜索公式“ C”(例如“ C6H5Cl”)时。我尝试实现“ query_string”,但出现序列化器错误,找不到任何解决方案。
这是搜索功能:
def search_formula(formula_value, page, nbrpp):
"""
get a compound by it's formula
:param formula_value: a string equal to a formula
:param page: number of displayed page of result
:param nbrpp: number of result in a page
:return: json list of result
"""
# connect to elastic search
es_host = settings.ELASTICSEARCH
es = Elasticsearch(hosts=[es_host])
q = Q('bool',
should=[Q('match', data__molecule__formula=formula_value)],
)
s = Search().using(es).query(q)[nbrpp*page-nbrpp:(nbrpp*page)-1]
return _search_to_json(search=s.execute(), nbresult=s.count())
以及带有查询字符串的版本不起作用:
def search_formula(formula_value, page, nbrpp):
"""
get a compound by it's formula
:param formula_value: a string equal to a formula
:param page: number of displayed page of result
:param nbrpp: number of result in a page
:return: json list of result
"""
# connect to elastic search
es_host = settings.ELASTICSEARCH
es = Elasticsearch(hosts=[es_host])
q = Q('query_string', query=[Q(data__molecule__formula=formula_value)])
print()
s = Search().using(es).query(q)[nbrpp*page-nbrpp:(nbrpp*page)-1]
return _search_to_json(search=s.execute(), nbresult=s.count())
我希望所有结果中按重量排序的公式中都包含C。