'Filters (if `neg` == True, filters out) records with
specified protocol/port at required state. Be aware that when
a host has a lot of ports filtered or closed, it will not
report all of them, but only a summary, and thus the filter
might not work as expected. This filter will always work to
find open ports.'

@staticmethod
def searchport(port, protocol='tcp', state='open', neg=False):

    if (port == 'host'):
        return {'ports.port': ({'$gte': 0} if neg else (-1))}
    if (state == 'open'):
        return {('openports.%s.ports' % protocol): ({'$ne': port} if neg else port)}
    if neg:
        return {'$or': [{'ports': {'$elemMatch': {'port': port, 'protocol': protocol, 'state_state': {'$ne': state}}}}, {'ports.port': {'$ne': port}}]}
    return {'ports': {'$elemMatch': {'port': port, 'protocol': protocol, 'state_state': state}}}
