示例#1
0
文件: test_score.py 项目: Yipit/pyeqs
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
示例#2
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)
示例#3
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)
示例#4
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"]})
示例#5
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"]})
示例#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
    })
示例#7
0
def test_query_string(context):
    """
    Search with query string
    """
    # When I create a queryset with a query string
    qs = QueryString("cheese")
    t = QuerySet("localhost", index="foo", query=qs)

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

    # Then I get a the expected results
    results = t[0:10]
    len(results).should.equal(1)
示例#8
0
def test_query_string_with_additional_parameters(context):
    """
    Search with query string and additional parameters
    """
    # When I create a queryset with parameters
    qs = QueryString("cheese", fields=["bar"], use_dis_max=False, tie_breaker=0.05)
    t = QuerySet("localhost", index="foo", query=qs)

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

    # Then I get a the expected results
    results = t[0:10]
    len(results).should.equal(1)
示例#9
0
def test_simple_search(context):
    """
    Search with match_all query
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I do a search
    results = t[0:1]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "baz"})
示例#10
0
def test_simple_search(context):
    """
    Search with match_all query
    """
    # When create a queryset
    t = QuerySet("localhost", index="foo")

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

    # And I do a search
    results = t[0:1]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "baz"})
示例#11
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})
示例#12
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)
示例#13
0
def test_string_search(context):
    """
    Search with string query
    """
    # When create a queryset
    t = QuerySet("localhost", query="cheese", index="foo")

    # And there are records
    add_document("foo", {"bar": "banana"})
    add_document("foo", {"bar": "cheese"})

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

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "cheese"})
示例#14
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)
示例#15
0
def test_string_search(context):
    """
    Search with string query
    """
    # When create a queryset
    t = QuerySet("localhost", query="cheese", index="foo")

    # And there are records
    add_document("foo", {"bar": "banana"})
    add_document("foo", {"bar": "cheese"})

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

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "cheese"})
示例#16
0
def test_simple_search_with_host_list(context):
    """
    Connect with host list
    """
    # When create a queryset
    connection_info = [{"host": "localhost", "port": 9200}]
    t = QuerySet(connection_info, index="foo")

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

    # And I do a search
    results = t[0:1]

    # Then I get a the expected results
    len(results).should.equal(1)
    results[0]['_source'].should.equal({"bar": "baz"})
示例#17
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"})
示例#18
0
文件: test_terms.py 项目: Yipit/pyeqs
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"])
示例#19
0
def test_search_with_exists(context):
    """
    Search with exists filter
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I add an exists filter
    exists = Exists("baz")
    t.filter(exists)
    results = t[0:10]

    # Then my results only have that field
    len(results).should.equal(1)
    results[0]["_source"]["baz"].should.equal(1)
示例#20
0
def test_search_aggregation(context):
    """
    Search with aggregation
    """
    # 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 an aggregated search
    t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms"))
    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'baz', u'doc_count': 1}, {u'key': u'bazbaz', u'doc_count': 1}])
示例#21
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)
示例#22
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)
示例#23
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})
示例#24
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"})
示例#25
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"})
示例#26
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
示例#27
0
文件: test_terms.py 项目: Yipit/pyeqs
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_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)
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)
示例#30
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)
def test_search_with_missing(context):
    """
    Search with missing
    """
    # 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", {"bar": 3})

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

    # Then my results are filtered correctly
    len(results).should.equal(1)
    results[0]["_source"]["baz"].should.equal(2)
示例#32
0
文件: test_range.py 项目: Yipit/pyeqs
def test_search_with_gt_range(context):
    """
    Search with gt range filter
    """
    # When create a query block
    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 add a range filter
    _type = Range("bar", gt=2)
    t.filter(_type)
    results = t[0:10]

    # Then my results only have that type
    len(results).should.equal(1)
    results[0]["_source"]["bar"].should.equal(3)
示例#33
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"})
def test_search_with_type(context):
    """
    Search with type filter
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I add a type filter
    _type = Type("bar")
    t.filter(_type)
    results = t[0:10]

    # Then my results only have that type
    len(results).should.equal(1)
    results[0]["_source"]["bar"].should.equal(3)
示例#35
0
def test_search_with_scoring_min_score_and_track_scores(context):
    """
    Search with match_all query and scoring with min_score and track_scores
    """
    # 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": "baz", "scoring_field": 3})

    # And I do a search
    score = ScriptScore("final_score = 0 + doc['scoring_field'].value;")
    t.score(score, min_score=2, track_scores=True)
    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": 3})
    results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 2})
示例#36
0
def test_search_with_scoring_min_score_and_track_scores(context):
    """
    Search with match_all query and scoring with min_score and track_scores
    """
    # 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": "baz", "scoring_field": 3})

    # And I do a search
    score = ScriptScore("final_score = 0 + doc['scoring_field'].value;")
    t.score(score, min_score=2, track_scores=True)
    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": 3})
    results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 2})
示例#37
0
def test_search_with_mode_sorting(context):
    """
    Search with descending sorting and mode
    """
    # When create a query block
    t = QuerySet("localhost", index="foo")

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

    # And I add sorting
    s = Sort("bar", order="desc", mode="min")
    t.order_by(s)
    results = t[0:10]

    # Then my results have the proper sorting
    results[0]["_source"]["bar"].should.equal([3, 10])
    results[1]["_source"]["bar"].should.equal([2, 10])
    results[2]["_source"]["bar"].should.equal([1, 10])
示例#38
0
def test_search_with_desc_sorting(context):
    """
    Search with descending sorting
    """
    # When create a query block
    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 add sorting
    s = Sort("bar", order="desc")
    t.order_by(s)
    results = t[0:10]

    # Then my results have the proper sorting
    results[0]["_source"]["bar"].should.equal(3)
    results[1]["_source"]["bar"].should.equal(2)
    results[2]["_source"]["bar"].should.equal(1)
示例#39
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})
示例#40
0
def test_search_with_scoring_and_lang(context):
    """
    Search with custom scoring and language
    """
    # When create a query block
    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 add scoring with a language
    score = ScriptScore("s = 0 + doc['bar'].value", lang="mvel")
    t.score(score)
    results = t[0:10]

    # Then my results are scored correctly
    len(results).should.equal(3)
    results[0]["_source"]["bar"].should.equal(3)
    results[1]["_source"]["bar"].should.equal(2)
    results[2]["_source"]["bar"].should.equal(1)
示例#41
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 list(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)