def search(self, body, index="", doc_type="", scroll_context_timer="1m", page_size=10000, max_hits=None): """Execute a Search Query on the Elasticsearch Cluster Args: body (json string): The query body index (string): The name of the Index to Query. Default: "" (no index) doc_type (string): The Doc Type to query. Default: "" (no doc_type) scroll_context_timer (string): A string such as "1m" or "5m" that specifies how long to keep the scroll context alive between pages for large queries. Default: "1m" page_size (int): The number of 'hits' per page. Default: 10000 max_hits (int): The maximum number of 'hits' to return. Default: None, all hits will be returned Return: A List of Dicts. Each Dict is the value of the "_source" key for each of the hits. """ convert_to_sec(scroll_context_timer) # check context timer input response = self.client.post( os.path.join( self.url, index, doc_type, "_search?scroll={}&size={}".format(scroll_context_timer, page_size)), data=body, headers={'Content-Type': 'application/json'}) page = response.json() total_hits = page['hits']['total'] total_hits = min(total_hits, max_hits) if max_hits is not None else total_hits results = [d['_source'] for d in page['hits']['hits']] page_size = len(results) while page_size > 0 and len(results) < total_hits: page = self._make_scroll_request(scroll_context_timer, page.get("_scroll_id")).json() page_size = len(page['hits']['hits']) results += [d['_source'] for d in page['hits']['hits']] if total_hits > len(results): raise Exception('Expected {} Results, instead got {}'.format( total_hits, len(results))) else: return results[:total_hits]
def test_typing(self): with pytest.raises(TypeError): convert_to_sec(1) with pytest.raises(TypeError): convert_to_sec([1]) with pytest.raises(TypeError): convert_to_sec({"cat": "dog"}) with pytest.raises(TypeError): convert_to_sec(1.0) assert convert_to_sec("1m") == 60
def test_normal(self): assert convert_to_sec("1s") == 1 assert convert_to_sec("1m") == 60 assert convert_to_sec("5m") == 300 assert convert_to_sec("1h") == 3600 assert convert_to_sec("1d") == 86400 assert convert_to_sec("2w") == 1209600
def test_garbage(self): with pytest.raises(ValueError): convert_to_sec("cactus") with pytest.raises(ValueError): convert_to_sec("1y")