From 15d6f275f3821de32d323e987917542430660f48 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Wed, 15 Feb 2023 15:10:44 +0100 Subject: [PATCH] removed unused code for depricated text search Signed-off-by: Lorenz Herzberger --- planetmint/backend/localmongodb/query.py | 35 --------- planetmint/backend/query.py | 41 ----------- planetmint/backend/tarantool/query.py | 19 ----- planetmint/lib.py | 12 ---- tests/backend/localmongodb/test_queries.py | 84 ---------------------- tests/db/test_planetmint_api.py | 24 ------- 6 files changed, 215 deletions(-) diff --git a/planetmint/backend/localmongodb/query.py b/planetmint/backend/localmongodb/query.py index feaf145..947cf3e 100644 --- a/planetmint/backend/localmongodb/query.py +++ b/planetmint/backend/localmongodb/query.py @@ -116,41 +116,6 @@ def get_txids_filtered(conn, asset_ids, operation=None, last_tx=None): return (elem["id"] for elem in cursor) -@register_query(LocalMongoDBConnection) -def text_search( - conn, - search, - *, - language="english", - case_sensitive=False, - diacritic_sensitive=False, - text_score=False, - limit=0, - table="assets" -): - cursor = conn.run( - conn.collection(table) - .find( - { - "$text": { - "$search": search, - "$language": language, - "$caseSensitive": case_sensitive, - "$diacriticSensitive": diacritic_sensitive, - } - }, - {"score": {"$meta": "textScore"}, "_id": False}, - ) - .sort([("score", {"$meta": "textScore"})]) - .limit(limit) - ) - - if text_score: - return cursor - - return (_remove_text_score(obj) for obj in cursor) - - def _remove_text_score(asset): asset.pop("score", None) return asset diff --git a/planetmint/backend/query.py b/planetmint/backend/query.py index 2901fe4..a8284c2 100644 --- a/planetmint/backend/query.py +++ b/planetmint/backend/query.py @@ -243,47 +243,6 @@ def get_txids_filtered(connection, asset_id, operation=None): raise NotImplementedError -@singledispatch -def text_search( - conn, - search, - *, - language="english", - case_sensitive=False, - diacritic_sensitive=False, - text_score=False, - limit=0, - table=None -): - """Return all the assets that match the text search. - - The results are sorted by text score. - For more information about the behavior of text search on MongoDB see - https://docs.mongodb.com/manual/reference/operator/query/text/#behavior - - Args: - search (str): Text search string to query the text index - language (str, optional): The language for the search and the rules for - stemmer and tokenizer. If the language is ``None`` text search uses - simple tokenization and no stemming. - case_sensitive (bool, optional): Enable or disable case sensitive - search. - diacritic_sensitive (bool, optional): Enable or disable case sensitive - diacritic search. - text_score (bool, optional): If ``True`` returns the text score with - each document. - limit (int, optional): Limit the number of returned documents. - - Returns: - :obj:`list` of :obj:`dict`: a list of assets - - Raises: - OperationError: If the backend does not support text search - """ - - raise OperationError("This query is only supported when running " "Planetmint with MongoDB as the backend.") - - @singledispatch def get_latest_block(conn): """Get the latest commited block i.e. block with largest height""" diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index f1aa189..a594fe8 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -257,25 +257,6 @@ def get_txids_filtered(connection, asset_ids: list[str], operation: str = "", la return ids -@register_query(TarantoolDBConnection) -def text_search(conn, search, table=TARANT_TABLE_ASSETS, limit=0): - pattern = ".{}.".format(search) - field_no = 1 if table == TARANT_TABLE_ASSETS else 2 # 2 for meta_data - res = conn.run(conn.space(table).call("indexed_pattern_search", (table, field_no, pattern))) - - to_return = [] - - if len(res[0]): # NEEDS BEAUTIFICATION - if table == TARANT_TABLE_ASSETS: - for result in res[0]: - to_return.append({"data": json.loads(result[0])["data"], "id": result[1]}) - else: - for result in res[0]: - to_return.append({TARANT_TABLE_META_DATA: json.loads(result[1]), "id": result[0]}) - - return to_return if limit == 0 else to_return[:limit] - - @register_query(TarantoolDBConnection) def get_owned_ids(connection, owner: str) -> list[DbTransaction]: outputs = connection.run(connection.space(TARANT_TABLE_OUTPUT).select(owner, index="public_keys")) diff --git a/planetmint/lib.py b/planetmint/lib.py index eb1cc4c..661aacb 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -496,18 +496,6 @@ class Planetmint(object): logger.warning("Invalid transaction (%s): %s", type(e).__name__, e) return False - def text_search(self, search, *, limit=0, table="assets"): - """Return an iterator of assets that match the text search - - Args: - search (str): Text search string to query the text index - limit (int, optional): Limit the number of returned documents. - - Returns: - iter: An iterator of assets that match the text search. - """ - return backend.query.text_search(self.connection, search, limit=limit, table=table) - def get_assets(self, asset_ids) -> list[Asset]: """Return a list of assets that match the asset_ids diff --git a/tests/backend/localmongodb/test_queries.py b/tests/backend/localmongodb/test_queries.py index fff5951..4eb717c 100644 --- a/tests/backend/localmongodb/test_queries.py +++ b/tests/backend/localmongodb/test_queries.py @@ -80,90 +80,6 @@ # for asset in assets: # assert query.get_asset(conn, asset['id']) # -# @pytest.mark.skip -# @pytest.mark.parametrize('table', ['assets', 'metadata']) -# def test_text_search(table): -# from planetmint.backend import connect, query -# conn = connect() -# -# # Example data and tests cases taken from the mongodb documentation -# # https://docs.mongodb.com/manual/reference/operator/query/text/ -# objects = [ -# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, -# {'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90}, -# {'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100}, -# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, -# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80}, -# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, -# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} -# ] -# -# # insert the assets -# conn.db[table].insert_many(deepcopy(objects), ordered=False) -# -# # test search single word -# assert list(query.text_search(conn, 'coffee', table=table)) == [ -# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, -# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, -# ] -# -# # match any of the search terms -# assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [ -# {'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90}, -# {'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50}, -# {'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100}, -# {'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5}, -# {'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10} -# ] -# -# # search for a phrase -# assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [ -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, -# ] -# -# # exclude documents that contain a term -# assert list(query.text_search(conn, 'coffee -shop', table=table)) == [ -# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, -# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10}, -# ] -# -# # search different language -# assert list(query.text_search(conn, 'leche', language='es', table=table)) == [ -# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, -# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} -# ] -# -# # case and diacritic insensitive search -# assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [ -# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80}, -# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, -# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10} -# ] -# -# # case sensitive search -# assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [ -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, -# ] -# -# # diacritic sensitive search -# assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [ -# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200}, -# ] -# -# # return text score -# assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [ -# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0}, -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5, 'score': 0.75}, -# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10, 'score': 0.75}, -# ] -# -# # limit search result -# assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [ -# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50}, -# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5}, -# ] # # @pytest.mark.skip # def test_write_metadata(): diff --git a/tests/db/test_planetmint_api.py b/tests/db/test_planetmint_api.py index c1da92d..d9433af 100644 --- a/tests/db/test_planetmint_api.py +++ b/tests/db/test_planetmint_api.py @@ -67,30 +67,6 @@ class TestBigchainApi(object): with pytest.raises(OperationError): b.store_bulk_transactions([tx]) - def test_text_search(self, b, alice): - from planetmint.backend.tarantool.connection import TarantoolDBConnection - - if isinstance(b.connection, TarantoolDBConnection): - warnings.warn(" :::::: This function is used only with :::::: ") - return - - # define the assets - asset1 = {"data": multihash(marshal({"msg": "Planetmint 1"}))} - asset2 = {"data": multihash(marshal({"msg": "Planetmint 2"}))} - asset3 = {"data": multihash(marshal({"msg": "Planetmint 3"}))} - - # create the transactions - tx1 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[asset1]).sign([alice.private_key]) - tx2 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[asset2]).sign([alice.private_key]) - tx3 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[asset3]).sign([alice.private_key]) - - # write the transactions to the DB - b.store_bulk_transactions([tx1, tx2, tx3]) - - # get the assets through text search - assets = list(b.text_search("planetmint")) - assert len(assets) == 0 - @pytest.mark.usefixtures("inputs") def test_non_create_input_not_found(self, b, user_pk): from planetmint_cryptoconditions import Ed25519Sha256