Ejemplo n.º 1
0
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_queryset_max_score():
    """
    Get QuerySet Max Score
    """
    # When I create a query block
    t = QuerySet("foobar")

    # Then I get an appropriate max score
    t.max_score().should.equal(None)
def test_queryset_getitem_with_wrapper():
    """
    Fetch from QuerySet with __getitem__ and wrapper
    """
    # When I create a query block
    t = QuerySet("localhost", index="bar")
    wrapper = lambda y: list(map(lambda x: x['_id'], y))
    t.wrappers(wrapper)

    # And I have records
    response = {
       "took": 12,
       "hits": {
          "total": 1,
          "max_score": 10,
          "hits": [
             {
                "_index": "bar",
                "_type": "baz",
                "_id": "1",
                "_score": 10,
                "_source": {
                   "foo": "bar"
                },
                "sort": [
                   1395687078000
                ]
             }
          ]
       }
    }
    httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search",
                       body=json.dumps(response),
                       content_type="application/json")

    results = t[0:1]
    len(results).should.equal(1)
    t.count().should.equal(1)
    t.max_score().should.equal(10)
    int(results[0]).should.equal(1)