neptune/core/lib/context.py

64 lines
1.8 KiB
Python

def construct_query(index, net, channel, src, num, size, type=None, nicks=None):
# Get the initial query
extra_must = []
extra_should = []
extra_should2 = []
if num:
extra_must.append({"match": {"num": num}})
if net:
extra_must.append({"match": {"net": net}})
if channel:
extra_must.append({"match": {"channel": channel}})
if nicks:
for nick in nicks:
extra_should2.append({"match": {"nick": nick}})
types = ["msg", "notice", "action", "kick", "topic", "mode"]
fields = [
"nick",
"ident",
"host",
"channel",
"ts",
"msg",
"type",
"net",
"src",
]
if index == "int":
fields.append("mtype")
if channel == "*status" or type == "znc":
if {"match": {"channel": channel}} in extra_must:
extra_must.remove({"match": {"channel": channel}})
extra_should2 = []
extra_must.append({"match": {"type": "znc"}})
else:
for ctype in types:
extra_should.append({"match": {"mtype": ctype}})
else:
for ctype in types:
extra_should.append({"match": {"type": ctype}})
query = {
"size": size,
"query": {
"bool": {
"must": [
{"match": {"src": src}},
{
"bool": {
"should": [*extra_should],
}
},
{
"bool": {
"should": [*extra_should2],
}
},
*extra_must,
]
}
},
"fields": fields,
"_source": False,
}
return query