def test_search_with_iterator(context): """ Search using an iterator """ # When create a queryset t = QuerySet("localhost", index="foo") # And set iterator fetching to a small size t._per_request = 2 # And there are records add_document("foo", {"bar": 0}) add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) add_document("foo", {"bar": 4}) # And I do a filter on my new object # And a different filter on my old object t.order_by(Sort("bar", order="asc")) # Then I get the expected results for (counter, result) in enumerate(t): result['_source'].should.equal({"bar": counter}) len(t).should.equal(5) t.count().should.equal(5) t.max_score().should_not.be(None)
def test_search_with_iterator(context): """ Search using an iterator """ # When create a queryset t = QuerySet("localhost", index="foo") # And set iterator fetching to a small size t._per_request = 2 # And there are records add_document("foo", {"bar": 0}) add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) add_document("foo", {"bar": 4}) # And I do a filter on my new object # And a different filter on my old object t.order_by(Sort("bar", order="asc")) # Then I get the expected results for (counter, result) in enumerate(t): result['_source'].should.equal({"bar": counter}) len(t).should.equal(5) t.count().should.equal(5)
def test_queryset_iteration_with_multiple_cache_fetches(): """ Fetch results with QuerySet via __iter__ with multiple cache fetches """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And we lower the per request to force multiple fetches t._per_request = 2 # And I have records first_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] }, { "_index": "bar", "_type": "baz", "_id": "2", "_score": 10, "_source": { "foo": "barbar" }, "sort": [ 1395687078000 ] } ] } } second_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "3", "_score": 10, "_source": { "foo": "barbarbar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", responses=[ httpretty.Response(body=json.dumps(first_response), content_type="application/json"), httpretty.Response(body=json.dumps(second_response), content_type="application/json"), ]) # Then I should eventually get all records results = [] for result in t: results.append(result) len(results).should.equal(3) t.count().should.equal(3) results[0].should.equal("1") results[1].should.equal("2") results[2].should.equal("3")
def test_queryset_iteration_with_multiple_cache_fetches(): """ Fetch results with QuerySet via __iter__ with multiple cache fetches """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And we lower the per request to force multiple fetches t._per_request = 2 # And I have records first_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] }, { "_index": "bar", "_type": "baz", "_id": "2", "_score": 10, "_source": { "foo": "barbar" }, "sort": [ 1395687078000 ] } ] } } second_response = { "took": 12, "hits": { "total": 3, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "3", "_score": 10, "_source": { "foo": "barbarbar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", responses=[ httpretty.Response(body=json.dumps(first_response), content_type="application/json"), httpretty.Response(body=json.dumps(second_response), content_type="application/json"), ]) # Then I should eventually get all records results = [] for result in t: results.append(result) len(results).should.equal(3) t.count().should.equal(3) results[0].should.equal("1") results[1].should.equal("2") results[2].should.equal("3")