neptune/core/lib/context.py

88 lines
3.0 KiB
Python
Raw Normal View History

2022-08-15 16:59:09 +00:00
def construct_query(index, net, channel, src, num, size, type=None, nicks=None):
2022-08-09 06:20:30 +00:00
# Get the initial query
extra_must = []
2022-08-03 06:20:30 +00:00
extra_should = []
extra_should2 = []
2022-08-09 06:20:30 +00:00
if num:
2023-02-01 07:20:31 +00:00
extra_must.append({"match_phrase": {"num": num}})
2022-08-09 06:20:30 +00:00
if net:
2022-09-06 10:53:32 +00:00
extra_must.append({"match_phrase": {"net": net}})
2022-08-09 06:20:30 +00:00
if channel:
extra_must.append({"match": {"channel": channel}})
2022-08-03 06:20:30 +00:00
if nicks:
for nick in nicks:
extra_should2.append({"match": {"nick": nick}})
2022-08-09 06:20:30 +00:00
types = ["msg", "notice", "action", "kick", "topic", "mode"]
2022-08-09 06:20:30 +00:00
fields = [
"nick",
"ident",
"host",
"channel",
"ts",
"msg",
"type",
"net",
"src",
2022-08-16 18:43:55 +00:00
"tokens",
2022-08-09 06:20:30 +00:00
]
2022-09-05 06:20:30 +00:00
if index == "internal":
fields.append("mtype")
2022-08-15 16:59:09 +00:00
if channel == "*status" or type == "znc":
if {"match": {"channel": channel}} in extra_must:
extra_must.remove({"match": {"channel": channel}})
extra_should2 = []
# Type is one of msg or notice
2022-08-15 18:39:26 +00:00
# extra_should.append({"match": {"mtype": "msg"}})
# extra_should.append({"match": {"mtype": "notice"}})
extra_should.append({"match": {"type": "znc"}})
extra_should.append({"match": {"type": "self"}})
extra_should2.append({"match": {"type": "znc"}})
2022-08-15 18:28:52 +00:00
extra_should2.append({"match": {"nick": channel}})
elif type == "auth":
if {"match": {"channel": channel}} in extra_must:
extra_must.remove({"match": {"channel": channel}})
extra_should2 = []
extra_should2.append({"match": {"nick": channel}})
2022-08-15 18:39:26 +00:00
# extra_should2.append({"match": {"mtype": "msg"}})
# extra_should2.append({"match": {"mtype": "notice"}})
extra_should.append({"match": {"type": "query"}})
extra_should2.append({"match": {"type": "self"}})
extra_should.append({"match": {"nick": channel}})
2022-08-15 16:59:09 +00:00
else:
for ctype in types:
2023-02-01 07:20:31 +00:00
extra_should.append({"match": {"mtype": ctype}})
else:
for ctype in types:
extra_should.append({"match": {"type": ctype}})
2022-08-09 06:20:30 +00:00
query = {
2022-09-06 10:53:32 +00:00
"index": index,
"limit": size,
2022-08-09 06:20:30 +00:00
"query": {
"bool": {
"must": [
2022-09-06 10:53:32 +00:00
# {"equals": {"src": src}},
# {
# "bool": {
# "should": [*extra_should],
# }
# },
# {
# "bool": {
# "should": [*extra_should2],
# }
# },
*extra_must,
2022-08-09 06:20:30 +00:00
]
}
},
2022-08-09 06:20:30 +00:00
"fields": fields,
2022-09-06 10:53:32 +00:00
# "_source": False,
2022-08-09 06:20:30 +00:00
}
2022-09-06 10:53:32 +00:00
if extra_should:
query["query"]["bool"]["must"].append({"bool": {"should": [*extra_should]}})
if extra_should2:
query["query"]["bool"]["must"].append({"bool": {"should": [*extra_should2]}})
return query