def construct_query(net, channel, src, num, size): # Get the initial query extra_must = [] if num: extra_must.append({"match": {"num": num}}) types = ["msg", "notice", "action", "kick", "topic", "mode"] query_should = [{"match": {"type": x}} for x in types] query = { "size": size, "query": { "bool": { "must": [ {"match": {"net": net}}, {"match": {"channel": channel}}, {"match": {"src": src}}, { "bool": { "should": query_should, } }, *extra_must, ] } }, "fields": [ "nick", "ident", "host", "channel", "ts", "msg", "type", "net", "src", ], "_source": False, } return query