Ejemplo n.º 1
0
def _setup_fixture(connection=None):
    """
    setup, then index, content on a server,
    
    TODO: purge *all* collections from db to avoid clashes
    
    Call directly if you don't want to use the one-off test server
    """
    global _collection
    global _database
    global _connection

    if connection is None:
        connection = _connection

    _database = connection["test"]
    _database["system.js"].remove()
    _collection = _database["items"]
    _collection.remove()
    util.load_all_server_functions(_database)
    util.load_fixture("jstests/_fixture-basic.json", _collection)
Ejemplo n.º 2
0
def test_simple_search():
    collection = _database["search_works"]
    collection.remove()
    stdout, stderr = util.load_fixture("jstests/_fixture-basic.json", collection)
    conf = _database["search_.config"]
    conf.remove()
    conf.insert(
        {
            "collection_name": "search_works",
            "indexes": {"default_": {"fields": {"title": 5, "content": 1}}, "title": {"fields": {"title": 1}}},
        }
    )

    stdout, stderr = mongo_search.ensure_text_index(collection)

    results = mongo_search.raw_search(collection, u"fish")

    assert_equals(
        list(results.find()),
        [{u"_id": 1.0, u"value": 0.72150482058559517}, {u"_id": 3.0, u"value": 0.32510310522208458}],
    )

    nice_results = mongo_search.search(collection, u"fish")

    assert_equals(
        list(nice_results),
        [
            {
                u"_id": 1.0,
                u"value": {
                    u"content": u"groupers like John Dory",
                    u"_id": 1.0,
                    u"score": 0.72150482058559517,
                    u"title": u"fish",
                    u"category": u"A",
                },
            },
            {
                u"_id": 3.0,
                u"value": {
                    u"content": u"whippets kick groupers",
                    u"_id": 3.0,
                    u"score": 0.32510310522208458,
                    u"title": u"dogs and fish",
                    u"category": u"B",
                },
            },
        ],
    )
Ejemplo n.º 3
0
def test_per_field_search():
    collection = mongo_search.SearchableCollection(_database["oo_per_field_works"])
    collection.remove()
    stdout, stderr = util.load_fixture("jstests/_fixture-per_field.json", collection)
    collection.configure_text_index_fields({"title": 5, "content": 1})
    collection.configure_text_index_fields({"title": 1}, "title")
    collection.configure_text_index_fields({"content": 1}, "content_idx")

    stdout, stderr = collection.ensure_text_index()

    assert_equals(
        list(collection.search(u"dog")),
        [
            {
                u"_id": 3,
                u"title": u"dogs & fish",
                u"content": u"whippets kick groupers",
                u"category": u"B",
                u"score": 0.68680281974344504,
            },
            {
                u"_id": 2,
                u"title": u"dogs",
                u"content": u"whippets kick mongrels and no fish are involved",
                u"category": u"B",
                u"score": 0.65447153370732369,
            },
            {
                u"_id": 1,
                u"title": u"fish",
                u"content": u"groupers like John Dory are not dogs",
                u"category": u"A",
                u"score": 0.13203025163465576,
            },
        ],
    )

    assert_equals(list(collection.search(u"dog")), list(collection.search({u"default_": "dog"})))

    assert_equals(
        list(collection.search({u"title": u"dog"})),
        [
            {
                u"_id": 2,
                u"title": u"dogs",
                u"content": u"whippets kick mongrels and no fish are involved",
                u"category": u"B",
                u"score": 1,
            },
            {
                u"_id": 3,
                u"title": u"dogs & fish",
                u"content": u"whippets kick groupers",
                u"category": u"B",
                u"score": 0.7071067811865475,
            },
        ],
    )

    assert_equals(
        list(collection.search({u"content_idx": u"dogs"})),
        [
            {
                u"_id": 1,
                u"title": u"fish",
                u"content": u"groupers like John Dory are not dogs",
                u"category": u"A",
                u"score": 0.1757748711858504,
            }
        ],
    )

    assert_equals(
        list(collection.search({u"title": u"fish"}, {"category": "B"})),
        [
            {
                u"_id": 3,
                u"title": u"dogs & fish",
                u"content": u"whippets kick groupers",
                u"category": u"B",
                u"score": 0.7071067811865475,
            }
        ],
    )

    assert_equals(
        list(collection.search({u"title": u"fish dog"})),
        [
            {
                u"_id": 3,
                u"title": u"dogs & fish",
                u"content": u"whippets kick groupers",
                u"category": u"B",
                u"score": 0.99999999999999989,
            }
        ],
    )

    assert_equals(list(collection.search({u"title": u"dog"}, spec={u"category": u"Z"})), [])
    assert_equals(list(collection.search({u"content_idx": u"whippet"}, skip=2, spec={u"category": u"Z"})), [])
    assert_equals(list(collection.search({u"content_idx": u"spurgle"}, limit=10)), [])
    assert_equals(list(collection.search({u"title": u"dogs whippet"})), [])

    assert_raises(mongo_search.SearchIndexNotConfiguredException, collection.search, {u"not_index_name": "dog"})
Ejemplo n.º 4
0
def test_oo_search():
    collection = mongo_search.SearchableCollection(_database["oo_search_works"])
    collection.remove()
    stdout, stderr = util.load_fixture("jstests/_fixture-basic.json", collection)

    collection.configure_text_index_fields({u"title": 5, u"content": 1})
    collection.configure_text_index_fields({u"title": 1}, u"title")

    stdout, stderr = collection.ensure_text_index()

    # NO raw search for now
    # we could do this, but we would want to use a fields param like with .find()

    # results = collection._search(u'fish')
    #
    # assert_equals(
    #   list(results.find()),
    #   [{u'_id': 1, u'value': 0.72150482058559517},
    #    {u'_id': 3, u'value': 0.32510310522208458}]
    # )

    assert_equals(
        list(collection.search(u"fish")),
        [
            {
                u"content": u"groupers like John Dory",
                u"_id": 1.0,
                u"score": 0.72150482058559517,
                u"title": u"fish",
                u"category": u"A",
            },
            {
                u"content": u"whippets kick groupers",
                u"_id": 3.0,
                u"score": 0.32510310522208458,
                u"title": u"dogs and fish",
                u"category": u"B",
            },
        ],
    )

    assert_equals(
        list(collection.search(u"fish", spec={u"category": u"A"})),
        [
            {
                u"content": u"groupers like John Dory",
                u"_id": 1.0,
                u"score": 0.72150482058559517,
                u"title": u"fish",
                u"category": u"A",
            }
        ],
    )

    assert_equals(
        list(collection.search(u"dog whippet", limit=1)),
        [
            {
                u"content": u"whippets kick groupers",
                u"_id": 3.0,
                u"score": 0.27585913234480763,
                u"title": u"dogs and fish",
                u"category": u"B",
            }
        ],
    )
    assert_equals(
        list(collection.search(u"whippets", skip=1, spec={u"category": u"B"})),
        [
            {
                u"content": u"whippets kick mongrels",
                u"_id": 2.0,
                u"score": 0.1706438640480763,
                u"title": u"dogs",
                u"category": u"B",
            }
        ],
    )
    assert_equals(list(collection.search(u"whippet", spec={u"category": u"Z"})), [])
    assert_equals(list(collection.search(u"whippet", skip=2, spec={u"category": u"Z"})), [])
    assert_equals(list(collection.search(u"spurgle", limit=10)), [])

    cursor = collection.search(u"dog whippet")
    assert_equals(cursor.count(), 2)
    cursor.skip(1)
    cursor.limit(1)
    assert_equals(
        cursor[0],
        {
            u"content": u"whippets kick mongrels",
            u"category": u"B",
            u"_id": 2,
            u"score": 0.72398060061762026,
            u"title": u"dogs",
        },
    )
    assert_equals(len(list(cursor)), 1)

    cursor = collection.search(u"dog")
    cursor.limit(10)
    assert_equals(cursor.count(), 2)
    assert_equals(
        list(cursor),
        [
            {
                u"content": u"whippets kick mongrels",
                u"_id": 2.0,
                u"score": 0.8532193202403815,
                u"title": u"dogs",
                u"category": u"B",
            },
            {
                u"content": u"whippets kick groupers",
                u"_id": 3.0,
                u"score": 0.32510310522208458,
                u"title": u"dogs and fish",
                u"category": u"B",
            },
        ],
    )

    cursor = collection.search(u"kick", skip=1, limit=5)
    assert_equals(
        list(cursor),
        [
            {
                u"content": u"whippets kick mongrels",
                u"_id": 2.0,
                u"score": 0.1706438640480763,
                u"title": u"dogs",
                u"category": u"B",
            }
        ],
    )
    assert_equals(cursor.count(), 2)