def test_create_queryset_with_scoring_min_score_track_score():
    """
    Create QuerySet with Scoring, Minimum Score and Track Scores
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add scoring
    s = ScriptScore("foo = 0.0")
    t.score(s, min_score=0, track_scores=True)

    # Then I see the appropriate JSON
    results = {
        "min_score": 0,
        "track_scores": True,
        "query": {
            "function_score": {
                "functions": [
                    {
                        "script_score": {
                            "script": "foo = 0.0"
                        }
                    }
                ],
                "query": {"match_all": {}},
                "boost_mode": "replace",
                "score_mode": "multiply"
            }
        }
    }

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

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

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

    homogeneous(t._query, results)
Ejemplo n.º 3
0
def test_search_multiple_scoring(context):
    """
    Search with custom scoring and params
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I add scoring with params
    score = ScriptScore("s = custom_param + doc['bar'].value", params={"custom_param": 1})
    t.score(score)

    boost = {
        "boost_factor": "10",
        "filter": Exists("baz")
    }
    t.score(boost)
    results = t[0:10]

    # Then my results are scored correctly
    len(results).should.equal(2)
    results[0]["_source"]["baz"].should.equal(4)
    ("baz" in results[1]["_source"].keys()).should.be.false
Ejemplo n.º 4
0
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_sorting():
    """
    Create QuerySet with Sorting
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add sorting
    s = Sort("_id", order="asc")
    t.order_by(s)

    # Then I see the appropriate JSON
    results = {
        "query": {
            "match_all": {}
        },
        "sort": [
            {
                "_id": {
                    "order": "asc"
                }
            }
        ]
    }

    homogeneous(t._query, results)
Ejemplo n.º 6
0
def test_search_nested_terms_aggregations_with_size(context):
    """
    Search with nested terms aggregation and a specified size
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are nested records
    add_document("foo", {"child": [{"stuff": "yep", "bazbaz": "type0"}], "foo": "foo"})
    add_document("foo", {"child": [{"stuff": "yep", "bazbaz": "type0"}], "foo": "foo"})
    add_document("foo", {"child": [{"stuff": "nope", "bazbaz": "type1"}], "foo": "foofoo"})
    add_document("foo", {"child": [{"stuff": "nope", "bazbaz": "type2"}], "foo": "foofoo"})

    # And I do a nested
    t.aggregate(aggregation=Aggregations("baz_types", "bazbaz", "terms",
                                         nested_path="child", size=1))
    t[0:10]

    # The I get the expected results
    t.aggregations().should.have.key("child").being.equal({
        u'baz_types': {
            u'buckets': [{u'doc_count': 2, u'key': u'type0'}]
        },
        u'doc_count': 4
    })
Ejemplo n.º 7
0
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
    ss = ScriptScore("foo = 1.0")
    t.score(ss)

    # Then I see the appropriate JSON
    results = {
        "query": {
            "function_score": {
                "query": {"match_all": {}},
                "script_score": {
                    "script": "foo = 1.0"
                },
                "boost_mode": "replace"
            }
        }
    }

    homogeneous(t._query, results)
Ejemplo n.º 8
0
def test_wrappers(context):
    """
    Search with wrapped match_all query
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I do a search
    def wrapper_function(search_results):
        return map(lambda x: x["_source"]["bar"] + 1, search_results)

    t.wrappers(wrapper_function)
    t.order_by(Sort("bar", order="asc"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(3)
    results[0].should.equal(2)
    results[1].should.equal(3)
    results[2].should.equal(4)
Ejemplo n.º 9
0
def test_create_queryset_with_multiple_sorting():
    """
    Create QuerySet with Multiple Sorting
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add sorting
    s = Sort("_id", order="asc")
    t.order_by(s)

    ss = Sort("_id", order="desc")
    t.order_by(ss)

    # Then I see the appropriate JSON
    results = {
        "sort": [
            {
                "_id": {
                    "order": "asc"
                }
            },
            {
                "_id": {
                    "order": "desc"
                }
            }
        ],
        "query": {
            "match_all": {}
        }
    }

    homogeneous(t._query, results)
Ejemplo n.º 10
0
def test_create_queryset_with_scoring():
    """
    Create QuerySet with Scoring, Minimum Score and Track Scores
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add scoring
    s = ScriptScore("foo = 0.0")
    t.score(s, min_score=0, track_scores=True)

    # Then I see the appropriate JSON
    results = {
        "min_score": 0,
        "track_scores": True,
        "query": {
            "function_score": {
                "functions": [
                    {
                        "script_score": {
                            "script": "foo = 0.0"
                        }
                    }
                ],
                "query": {"match_all": {}},
                "boost_mode": "replace",
                "score_mode": "multiply"
            }
        }
    }

    homogeneous(t._query, results)
Ejemplo n.º 11
0
def test_queryset_iteration_with_no_results():
    """
    Fetch results with QuerySet via __iter__ with no results
    """

    # 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 I have no records
    response = {
       "took": 12,
       "hits": {
          "total": 0,
          "max_score": 0,
          "hits": []
       }
    }
    httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search",
                       body=json.dumps(response),
                       content_type="application/json")

    results = []
    for result in t:
        results.append(result)
    len(results).should.equal(0)
    t.count().should.equal(0)
Ejemplo n.º 12
0
def test_search_global_aggregations(context):
    """
    Search with global aggregations
    """
    # With a specific query
    # q = QueryBuilder(query_string=QueryString(query={"match": {"foo_attr": "yes"}}))

    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I do a global
    t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms", global_name="all_bar"))
    t[0:10]

    # I get the expected results
    t.aggregations().should.have.key("all_bar").being.equal({
        u'foo_attrs': {u'buckets': [
            {u'key': u'bazbaz', u'doc_count': 2},
            {u'key': u'baz', u'doc_count': 1}
        ]},
        u'doc_count': 3})
Ejemplo n.º 13
0
def test_post_query_actions(context):
    """
    Search with match_all query with post query actions
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I have a post query action
    global my_global_var
    my_global_var = 1

    def action(self, results, start, stop):
        global my_global_var
        my_global_var += 1

    t.post_query_actions(action)

    # And I do a search
    t.order_by(Sort("bar", order="asc"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(3)
    results[0]["_source"]["bar"].should.equal(1)
    results[1]["_source"]["bar"].should.equal(2)
    results[2]["_source"]["bar"].should.equal(3)
    my_global_var.should.equal(2)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
def test_queryset_count():
    """
    Get QuerySet Count
    """
    # When I create a query block
    t = QuerySet("foobar")

    # Then I get an appropriate Count
    t.count().should.equal(None)
Ejemplo n.º 16
0
def test_queryset_getitem_with_post_query_action():
    """
    Fetch from QuerySet with __getitem__ and post query action
    """
    # When I create a query block
    t = QuerySet("localhost", index="bar")

    # And I have a post query action
    global my_global_var
    my_global_var = 1

    def action(self, results, start, stop):
        global my_global_var
        my_global_var += 1

    t.post_query_actions(action)

    # And I have records
    response = {
        "took": 12,
        "timed_out": False,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "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)

    # Then I see the correct results
    results[0]['_id'].should.equal('1')
    my_global_var.should.equal(2)
Ejemplo n.º 17
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"]})
Ejemplo n.º 18
0
def test_search_filtered_aggregations(context):
    """
    Search with filtered aggregations
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": 0, "foo": "baz"})
    add_document("foo", {"bar": 0, "foo": "baz"})
    add_document("foo", {"bar": 2})
    add_document("foo", {"bar": 3, "foo": "bazbaz"})
    add_document("foo", {"bar": 5, "foo": "foobar"})
    add_document("foo", {"bar": 5})
    add_document("foo", {"bar": 5, "foo": "foobaz"})
    add_document("foo", {"bar": 9})

    # And I do a filtered
    f = Filter().filter(Range("bar", gte=5))
    t.aggregate(aggregation=Aggregations("missing_foo", "foo", "missing",
                                         filter_val=f, filter_name="high_bar"))
    t[0:10]

    # Then I get the expected results
    t.aggregations().should.have.key("high_bar")
    t.aggregations()["high_bar"].should.have.key("missing_foo").being.equal(
        {u'doc_count': 2})
    t.aggregations()["high_bar"]["doc_count"].should.equal(4)
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
def test_create_queryset_with_scoring_and_filtering_from_object():
    """
    Create QuerySet with Scoring and Filter Object
    """
    # When create a query block
    t = QuerySet("foobar")

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

    # And I add filtering
    f = Filter("and").filter(Term("foo", "bar"))
    t.filter(f)

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

    homogeneous(t._query, results)
Ejemplo n.º 21
0
def test_create_queryset_with_only_block():
    """
    Create QuerySet with Only block
    """
    # When create a query block
    t = QuerySet("foobar")

    # And I add an 'only' block
    t.only("_id")

    # Then I see the appropriate JSON
    results = {
        "query": {"match_all": {}},
        "fields": ["_id"]
    }

    homogeneous(t._query, results)
Ejemplo n.º 22
0
def test_geo_distance_search_array(context):
    """
    Search with geo distance filter with array
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"location": {"lat": 1.1, "lon": 2.1}})
    add_document("foo", {"location": {"lat": 40.1, "lon": 80.1}})

    # And I filter for terms
    t.filter(GeoDistance([2.0, 1.0], "20mi"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(1)
Ejemplo n.º 23
0
def test_geo_distance_search_with_field_name(context):
    """
    Search with geo distance filter with field_name
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"foo_loc": {"lat": 1.1, "lon": 2.1}})
    add_document("foo", {"foo_loc": {"lat": 40.1, "lon": 80.1}})

    # And I filter for distance
    t.filter(GeoDistance({"lat": 1.0, "lon": 2.0}, "20mi", field_name="foo_loc"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(1)
Ejemplo n.º 24
0
def test_search_nested_aggregations(context):
    """
    Search with nested aggregations
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are nested records
    add_document("foo", {"child": [{"stuff": "yep", "bazbaz": 10}], "foo": "foo"})
    add_document("foo", {"child": [{"stuff": "nope", "bazbaz": 1}], "foo": "foofoo"})

    # And I do a nested
    t.aggregate(aggregation=Aggregations("best_bazbaz", "bazbaz", "max", nested_path="child"))
    t[0:10]

    # The I get the expected results
    t.aggregations().should.have.key("child").being.equal({'best_bazbaz': {'value': 10.0}, 'doc_count': 2})
Ejemplo n.º 25
0
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)
Ejemplo n.º 26
0
def test_search_histogram_aggregations_with_min_doc_count(context):
    """
    Search with aggregations that have histograms with min_doc_count
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

    # And there are records
    add_document("foo", {"bar": 0, "foo": "baz"})
    add_document("foo", {"bar": 0, "foo": "baz"})
    add_document("foo", {"bar": 2})
    add_document("foo", {"bar": 3, "foo": "bazbaz"})
    add_document("foo", {"bar": 5, "foo": "foobar"})
    add_document("foo", {"bar": 5})
    add_document("foo", {"bar": 5, "foo": "foobaz"})
    add_document("foo", {"bar": 9})

    # When I do a histogram aggregation
    t.aggregate(aggregation=Aggregations("bar_buckets", "bar", "metric", histogram_interval=2,
                                         min_doc_count=2))
    t[0:10]

    # I get the expected results
    t.aggregations().should.have.key("bar_buckets")
    t.aggregations()["bar_buckets"].should.have.key("buckets").being.equal([
        {u'key': 4, u'doc_count': 3},
        {u'key': 2, u'doc_count': 2},
        {u'key': 0, u'doc_count': 2}])
Ejemplo n.º 27
0
def test_search_terms_aggregation_with_order_two(context):
    """
    Search with terms aggregation ordered by count ascending
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I do an aggregated search
    t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms",
                                         order_type="_count", order_dir="asc"))
    t[0:10]

    # Then I get a the expected results
    t.aggregations().should.have.key('foo_attrs')

    t.aggregations()['foo_attrs'].should.have.key("buckets").being.equal([
        {u'key': u'bazbar', u'doc_count': 1},
        {u'key': u'bazbaz', u'doc_count': 2},
        {u'key': u'baz', u'doc_count': 3}])
Ejemplo n.º 28
0
def test_search_with_missing_existence_null_value(context):
    """
    Search with missing via non-existence or a null value
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I add filters
    t.filter(Missing("bar", existence=True, null_value=True))
    results = t[0:10]

    # Then my results are filtered correctly
    len(results).should.equal(2)
Ejemplo n.º 29
0
def test_terms_search_with_execution(context):
    """
    Search with terms filter with execution
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I filter for terms
    t.filter(Terms("foo", ["foo", "bar"], execution="and"))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]["_source"]["foo"].should.equal(["foo", "bar"])
Ejemplo n.º 30
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"})
Ejemplo n.º 31
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)
Ejemplo n.º 32
0
def test_match_all_search(context):
    """
    Search with match all filter
    """
    # When create a queryset
    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": "baz", "foo": "foofoo"})
    add_document("foo", {"bar": "baz", "foo": "foofoofoo"})

    # And I filter match_all
    t.filter(MatchAll())
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(4)
Ejemplo n.º 33
0
def test_terms_search(context):
    """
    Search with terms filter
    """
    # When create a queryset
    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": "baz", "foo": "foofoo"})
    add_document("foo", {"bar": "baz", "foo": "foofoofoo"})

    # And I filter for terms
    t.filter(Terms("foo", ["foo", "foofoo"]))
    results = t[0:10]

    # Then I get a the expected results
    len(results).should.equal(3)
def test_create_queryset():
    """
    Create Default QuerySet
    """
    # When create a queryset
    t = QuerySet("foobar")

    # Then I see the appropriate JSON
    results = {
        "query": {"match_all": {}}
    }

    homogeneous(t._query, results)