def run_query(query="", offset=0, length=24): """ Performs a search query over all listings. """ # Tokenize input query. words = [entities.fold_query_term(w) for w in query.split()] words = [w for w in words if w] if not words: words = [""] # Retrieve the keys for entities that match all terms. shards = [fetch_shard(word) for word in words] # Extract all elements from each shard. all_shards = set(shards[0]) for shard in shards[1:]: all_shards = all_shards & set(shard) in_order = [x for x in shards[0] if x in all_shards] # Load all listings matching these keys. keys = in_order[offset:offset + length] listings = lookup_listing.batch([([key], {}) for key in keys]) # Filter out old or invalid listings. results = [] for listing in listings: if not listing or not listing.posting_time: continue if any([word and word not in listing.keywords for word in words]): continue results.append(listing) return results
def test_query_folding(): expectations = { "is:Foos": "is:Foos", "CaTs": "cat", "e@mails": "e@mails", "[foo": "foo", } for key, value in expectations.items(): assert entities.fold_query_term(key) == value