elasticsearch dsl python 解压 q 查询
elasticsearch dsl python unpack q queries
如何在 elasticsearch dsl python 中动态创建组合 Q 查询?
我经历了 docs and this SO post。我构建了一个包含所有必需信息的 q_query 字典。
ipdb> q_queries
{'queries': [{'status': 'PASS'}, {'status': 'FAIL'}, {'status': 'ABSE'}, {'status': 'WITH'}], 'operation': 'or'}
我要执行以下操作q_query
qq=Q("match", status='PASS') | Q("match", status="FAIL") | Q("match", status="ABSE") | Q("match", status="WITH")
下面的字典列表
ipdb> [Q('match', **z) for z in q_queries['queries']]
[Match(status='PASS'), Match(status='FAIL'), Match(status='ABSE'), Match(status='WITH')]
但是如何用一个或操作符或一个操作符组合多个问题呢?还有上面对应的 elasticsearch raw query
是什么?我尝试了以下操作,因为我必须根据 test_id.
进行过滤
{
"query": {
"bool": {
"must": [
{ "match": { "test_id": "7" }},
{
"range": {
"created": {
"gte": "2016-01-01",
"lte": "2016-01-31"
}
}
}
],
"should": [
{ "match": { "status": "PASS"}},
{ "match": { "status": "FAIL"}}
]
}
}
}
但结果与预期不同 我在没有 should filter
的情况下进行相同的查询并且获得的结果是相同的。所以在我的例子中,过滤器不应该由 elasticsearch 执行。
非常感谢任何帮助。
TIA
在探索 elasticsearch dsl python 一段时间后,this piece of documentation 帮助我解决了上述问题。下面发布的是我为解决此问题而编写的函数。
def create_q_queries(self, q_queries, search_query):
"""
create q queries and chain if multiple q queries.
:param q_queries: Q queries with operation and query params as a dict.
:param search_query: Search() object.
:return: search_query updated with q queries.
"""
if q_queries:
logical_operator_mappings = {'or': 'should', 'and': 'must'}
for query in q_queries:
queries = [Q('match', **query) for query in query['queries']]
search_query = search_query.query(Q('bool', **{
logical_operator_mappings.get(query.get('operation')): queries
}))
return search_query
我更改了 q_queries 的格式以基于 and
、or
等多个运算符执行链接
q_queries = [
{
"operation": "or",
"queries":
[
{"status": "PASS"}, {"status": "FAIL"}, {"status": "ABSE"}, {"status": "WITH"}
]
}
]
如何在 elasticsearch dsl python 中动态创建组合 Q 查询? 我经历了 docs and this SO post。我构建了一个包含所有必需信息的 q_query 字典。
ipdb> q_queries
{'queries': [{'status': 'PASS'}, {'status': 'FAIL'}, {'status': 'ABSE'}, {'status': 'WITH'}], 'operation': 'or'}
我要执行以下操作q_query
qq=Q("match", status='PASS') | Q("match", status="FAIL") | Q("match", status="ABSE") | Q("match", status="WITH")
下面的字典列表
ipdb> [Q('match', **z) for z in q_queries['queries']]
[Match(status='PASS'), Match(status='FAIL'), Match(status='ABSE'), Match(status='WITH')]
但是如何用一个或操作符或一个操作符组合多个问题呢?还有上面对应的 elasticsearch raw query
是什么?我尝试了以下操作,因为我必须根据 test_id.
{
"query": {
"bool": {
"must": [
{ "match": { "test_id": "7" }},
{
"range": {
"created": {
"gte": "2016-01-01",
"lte": "2016-01-31"
}
}
}
],
"should": [
{ "match": { "status": "PASS"}},
{ "match": { "status": "FAIL"}}
]
}
}
}
但结果与预期不同 我在没有 should filter
的情况下进行相同的查询并且获得的结果是相同的。所以在我的例子中,过滤器不应该由 elasticsearch 执行。
非常感谢任何帮助。
TIA
在探索 elasticsearch dsl python 一段时间后,this piece of documentation 帮助我解决了上述问题。下面发布的是我为解决此问题而编写的函数。
def create_q_queries(self, q_queries, search_query):
"""
create q queries and chain if multiple q queries.
:param q_queries: Q queries with operation and query params as a dict.
:param search_query: Search() object.
:return: search_query updated with q queries.
"""
if q_queries:
logical_operator_mappings = {'or': 'should', 'and': 'must'}
for query in q_queries:
queries = [Q('match', **query) for query in query['queries']]
search_query = search_query.query(Q('bool', **{
logical_operator_mappings.get(query.get('operation')): queries
}))
return search_query
我更改了 q_queries 的格式以基于 and
、or
等多个运算符执行链接
q_queries = [
{
"operation": "or",
"queries":
[
{"status": "PASS"}, {"status": "FAIL"}, {"status": "ABSE"}, {"status": "WITH"}
]
}
]