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 = [] # Type is one of msg or notice #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"}}) extra_should2.append({"match": {"channel": 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}}) #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}}) 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