def test_create_queryset_with_filters():
    """
    Create QuerySet with Multiple Filters
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add a filter
    t.filter(Term("foo", "bar"))
    t.filter(Term("foobar", "foobar"))

    # Then I see the appropriate JSON
    results = {
        "query": {
            "filtered": {
                "query": {"match_all": {}},
                "filter": {
                    "and": [
                        {
                            "term": {
                                "foo": "bar"
                            }
                        },
                        {
                            "term": {
                                "foobar": "foobar"
                            }
                        }
                    ]
                }
            }
        }
    }

    homogeneous(t._query, results)
def test_create_queryset_with_filters_and_scoring():
    """
    Create QuerySet with Scoring and Multiple Filters
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add filtering
    t.filter(Term("foo", "bar"))

    # And I add scoring
    s = ScriptScore("foo = 0.0")
    t.score(s)

    # And I add a second filter
    t.filter(Term("foobar", "foobar"))

    # Then I see the appropriate JSON
    results = {
        "query": {
            "function_score": {
                "query": {
                    "filtered": {
                        "query": {"match_all": {}},
                        "filter": {
                            "and": [
                                {
                                    "term": {
                                        "foo": "bar"
                                    }
                                },
                                {
                                    "term": {
                                        "foobar": "foobar"
                                    }
                                }
                            ]
                        }
                    }
                },
                "functions": [
                    {
                        "script_score": {
                            "script": "foo = 0.0"
                        }
                    }
                ],
                "boost_mode": "replace",
                "score_mode": "multiply"
            }
        }
    }

    homogeneous(t._query, results)
Пример #3
0
def test_search_with_filter_and_scoring_and_sorting_and_fields(context):
    """
    Search with match_all query, filter, scoring, sorting, and fields
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz", "scoring_field": 0, "sorting_field": 30})
    add_document("foo", {"bar": "baz", "scoring_field": 1, "sorting_field": 20})
    add_document("foo", {"bar": "baz", "scoring_field": 2, "sorting_field": 10})
    add_document("foo", {"bar": "bazbaz", "scoring_field": 3, "sorting_field": 0})

    # And I do a search
    t.filter(Term("bar", "baz"))
    score = ScriptScore("final_score = 0 + doc['scoring_field'].value;")
    t.score(score)
    sorting = Sort("sorting_field", order="desc")
    t.order_by(sorting)
    t.only(["bar"])
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(3)
    results[0]['fields'].should.equal({"bar": ["baz"]})
    results[1]['fields'].should.equal({"bar": ["baz"]})
    results[2]['fields'].should.equal({"bar": ["baz"]})
Пример #4
0
def test_search_with_filter_block(context):
    """
    Search with Filter Block
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz", "foo": "foo"})
    add_document("foo", {"bar": "bazbaz", "foo": "foo"})
    add_document("foo", {"bar": "bazbaz", "foo": "foofoo"})

    # And I do a filtered search
    f = Filter("or").filter(Term("bar", "baz")).filter(Term("foo", "foo"))
    t.filter(f)
    results = t[0:10]

    # Then I get the appropriate response
    len(results).should.equal(2)
Пример #5
0
def test_search_with_multiple_filters(context):
    """
    Search with multiple filters
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz", "foo": "foo"})
    add_document("foo", {"bar": "bazbaz", "foo": "foo"})
    add_document("foo", {"bar": "bazbaz", "foo": "foofoo"})

    # And I do a filtered search
    t.filter(Term("bar", "bazbaz"))
    t.filter(Term("foo", "foo"))
    results = t[0:10]

    # Then I get the appropriate response
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "bazbaz", "foo": "foo"})
Пример #6
0
def test_search_as_queryset_with_filter(context):
    """
    Search with match_all query and filter on a cloned queryset
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz"})
    add_document("foo", {"bar": "bazbaz"})

    # And I do a filter on my new object
    my_search = t.objects.filter(Term("bar", "baz"))

    # And a different filter on my old object
    t.filter(Term("bar", "bazbaz"))

    # And I do a search
    results = my_search[0:10]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "baz"})
def test_add_filter():
    """
    Create Filter with Block
    """
    # When I create a filter
    t = Filter()

    # And add a block
    t.filter(Term("foo", "bar"))

    # Then I see the appropriate JSON
    results = {"and": [{"term": {"foo": "bar"}}]}

    homogeneous(t, results)
Пример #8
0
def test_copy_queryset_with_filters():
    """
    Copy Queryset object and ensure distinct filters
    """
    # When create a queryset
    t = QuerySet("http://foobar:9200")

    # With filters
    t.filter(Term("foo", "bar"))

    # And I clone the queryset
    new_object = t.objects

    # And add new filters
    new_object.filter(Term("bar", "baz"))

    # Then the new object is not the same object as the queryset
    assert (new_object is not t)

    # And is not the same query object
    assert (new_object._query is not t._query)

    # But it is has the same properties
    heterogeneous(new_object._query, t._query)
def test_create_queryset_with_multiple_scoring():
    """
    Create QuerySet with Multiple Scoring
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add scoring
    s = ScriptScore("foo = 0.0")
    t.score(s)

    # And I add more scoring
    boost = {
        "boost_factor": "3",
        "filter": Term("foo", "bar")
    }
    t.score(boost)

    # Then I see the appropriate JSON
    results = {
        "query": {
            "function_score": {
                "query": {"match_all": {}},
                "functions": [
                    {
                        "script_score": {
                            "script": "foo = 0.0"
                        }
                    },
                    {
                        "boost_factor": "3",
                        "filter": {
                            "term": {
                                "foo": "bar"
                            }
                        }
                    }
                ],
                "boost_mode": "replace",
                "score_mode": "multiply"
            }
        }
    }

    homogeneous(t._query, results)
Пример #10
0
def test_simple_search_with_filter(context):
    """
    Search with filter
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz"})
    add_document("foo", {"bar": "bazbaz"})

    # And I do a filtered search
    t.filter(Term("bar", "baz"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "baz"})
Пример #11
0
def test_search_with_filter_and_scoring(context):
    """
    Search with match_all query, filter and scoring
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": "baz", "scoring_field": 1})
    add_document("foo", {"bar": "baz", "scoring_field": 2})
    add_document("foo", {"bar": "bazbaz", "scoring_field": 3})

    # And I do a search
    t.filter(Term("bar", "baz"))
    score = ScriptScore("final_score = 0 + doc['scoring_field'].value;")
    t.score(score)
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(2)
    results[0]['_source'].should.equal({"bar": "baz", "scoring_field": 2})
    results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 1})