diff options
author | dvora-h <67596500+dvora-h@users.noreply.github.com> | 2022-10-24 17:44:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 17:44:13 +0300 |
commit | 9a460c8e4f5e22f3ba92bf77b016aaad0b5621fb (patch) | |
tree | de5bdf59518d928f0e720a380db81cb745cf907c | |
parent | d95e004328666948753985620f636dc33ee21022 (diff) | |
download | redis-py-9a460c8e4f5e22f3ba92bf77b016aaad0b5621fb.tar.gz |
Deprecate `add_doccument` (FT.ADD) (#2425)
* deprecate add_doccuments
* linters
* linters
* async tests
-rw-r--r-- | redis/commands/search/commands.py | 8 | ||||
-rw-r--r-- | tests/test_asyncio/test_search.py | 241 | ||||
-rw-r--r-- | tests/test_search.py | 298 |
3 files changed, 178 insertions, 369 deletions
diff --git a/redis/commands/search/commands.py b/redis/commands/search/commands.py index ba9d9bc..ceca20f 100644 --- a/redis/commands/search/commands.py +++ b/redis/commands/search/commands.py @@ -2,6 +2,8 @@ import itertools import time from typing import Dict, Optional, Union +from deprecated import deprecated + from redis.client import Pipeline from ..helpers import parse_to_dict @@ -236,6 +238,9 @@ class SearchCommands: return self.execute_command(*args) + @deprecated( + version="2.0.0", reason="deprecated since redisearch 2.0, call hset instead" + ) def add_document( self, doc_id, @@ -289,6 +294,9 @@ class SearchCommands: **fields, ) + @deprecated( + version="2.0.0", reason="deprecated since redisearch 2.0, call hset instead" + ) def add_document_hash(self, doc_id, score=1.0, language=None, replace=False): """ Add a hash document to the index. diff --git a/tests/test_asyncio/test_search.py b/tests/test_asyncio/test_search.py index 862c21b..88c80d3 100644 --- a/tests/test_asyncio/test_search.py +++ b/tests/test_asyncio/test_search.py @@ -85,7 +85,7 @@ async def createIndex(modclient, num_docs=100, definition=None): assert 50 == indexer.chunk_size for key, doc in chapters.items(): - await indexer.add_document(key, **doc) + await indexer.client.client.hset(key, mapping=doc) await indexer.commit() @@ -189,7 +189,7 @@ async def test_client(modclient: redis.Redis): assert 167 == (await modclient.ft().search(Query("henry king").slop(100))).total # test delete document - await modclient.ft().add_document("doc-5ghs2", play="Death of a Salesman") + await modclient.hset("doc-5ghs2", mapping={"play": "Death of a Salesman"}) res = await modclient.ft().search(Query("death of a salesman")) assert 1 == res.total @@ -198,36 +198,19 @@ async def test_client(modclient: redis.Redis): assert 0 == res.total assert 0 == await modclient.ft().delete_document("doc-5ghs2") - await modclient.ft().add_document("doc-5ghs2", play="Death of a Salesman") + await modclient.hset("doc-5ghs2", mapping={"play": "Death of a Salesman"}) res = await modclient.ft().search(Query("death of a salesman")) assert 1 == res.total await modclient.ft().delete_document("doc-5ghs2") @pytest.mark.redismod -@skip_ifmodversion_lt("2.2.0", "search") -async def test_payloads(modclient: redis.Redis): - await modclient.ft().create_index((TextField("txt"),)) - - await modclient.ft().add_document("doc1", payload="foo baz", txt="foo bar") - await modclient.ft().add_document("doc2", txt="foo bar") - - q = Query("foo bar").with_payloads() - res = await modclient.ft().search(q) - assert 2 == res.total - assert "doc1" == res.docs[0].id - assert "doc2" == res.docs[1].id - assert "foo baz" == res.docs[0].payload - assert res.docs[1].payload is None - - -@pytest.mark.redismod @pytest.mark.onlynoncluster async def test_scores(modclient: redis.Redis): await modclient.ft().create_index((TextField("txt"),)) - await modclient.ft().add_document("doc1", txt="foo baz") - await modclient.ft().add_document("doc2", txt="foo bar") + await modclient.hset("doc1", mapping={"txt": "foo baz"}) + await modclient.hset("doc2", mapping={"txt": "foo bar"}) q = Query("foo ~bar").with_scores() res = await modclient.ft().search(q) @@ -240,34 +223,11 @@ async def test_scores(modclient: redis.Redis): @pytest.mark.redismod -async def test_replace(modclient: redis.Redis): - await modclient.ft().create_index((TextField("txt"),)) - - await modclient.ft().add_document("doc1", txt="foo bar") - await modclient.ft().add_document("doc2", txt="foo bar") - await waitForIndex(modclient, "idx") - - res = await modclient.ft().search("foo bar") - assert 2 == res.total - await ( - modclient.ft().add_document("doc1", replace=True, txt="this is a replaced doc") - ) - - res = await modclient.ft().search("foo bar") - assert 1 == res.total - assert "doc2" == res.docs[0].id - - res = await modclient.ft().search("replaced doc") - assert 1 == res.total - assert "doc1" == res.docs[0].id - - -@pytest.mark.redismod async def test_stopwords(modclient: redis.Redis): stopwords = ["foo", "bar", "baz"] await modclient.ft().create_index((TextField("txt"),), stopwords=stopwords) - await modclient.ft().add_document("doc1", txt="foo bar") - await modclient.ft().add_document("doc2", txt="hello world") + await modclient.hset("doc1", mapping={"txt": "foo bar"}) + await modclient.hset("doc2", mapping={"txt": "hello world"}) await waitForIndex(modclient, "idx") q1 = Query("foo bar").no_content() @@ -285,11 +245,13 @@ async def test_filters(modclient: redis.Redis): ) ) await ( - modclient.ft().add_document( - "doc1", txt="foo bar", num=3.141, loc="-0.441,51.458" + modclient.hset( + "doc1", mapping={"txt": "foo bar", "num": 3.141, "loc": "-0.441,51.458"} ) ) - await modclient.ft().add_document("doc2", txt="foo baz", num=2, loc="-0.1,51.2") + await ( + modclient.hset("doc2", mapping={"txt": "foo baz", "num": 2, "loc": "-0.1,51.2"}) + ) await waitForIndex(modclient, "idx") # Test numerical filter @@ -322,26 +284,15 @@ async def test_filters(modclient: redis.Redis): @pytest.mark.redismod -async def test_payloads_with_no_content(modclient: redis.Redis): - await modclient.ft().create_index((TextField("txt"),)) - await modclient.ft().add_document("doc1", payload="foo baz", txt="foo bar") - await modclient.ft().add_document("doc2", payload="foo baz2", txt="foo bar") - - q = Query("foo bar").with_payloads().no_content() - res = await modclient.ft().search(q) - assert 2 == len(res.docs) - - -@pytest.mark.redismod async def test_sort_by(modclient: redis.Redis): await ( modclient.ft().create_index( (TextField("txt"), NumericField("num", sortable=True)) ) ) - await modclient.ft().add_document("doc1", txt="foo bar", num=1) - await modclient.ft().add_document("doc2", txt="foo baz", num=2) - await modclient.ft().add_document("doc3", txt="foo qux", num=3) + await modclient.hset("doc1", mapping={"txt": "foo bar", "num": 1}) + await modclient.hset("doc2", mapping={"txt": "foo baz", "num": 2}) + await modclient.hset("doc3", mapping={"txt": "foo qux", "num": 3}) # Test sort q1 = Query("foo").sort_by("num", asc=True).no_content() @@ -385,10 +336,12 @@ async def test_example(modclient: redis.Redis): ) # Indexing a document - await modclient.ft().add_document( + await modclient.hset( "doc1", - title="RediSearch", - body="Redisearch impements a search engine on top of redis", + mapping={ + "title": "RediSearch", + "body": "Redisearch impements a search engine on top of redis", + }, ) # Searching with complex parameters: @@ -461,11 +414,13 @@ async def test_no_index(modclient: redis.Redis): ) ) - await modclient.ft().add_document( - "doc1", field="aaa", text="1", numeric="1", geo="1,1", tag="1" + await modclient.hset( + "doc1", + mapping={"field": "aaa", "text": "1", "numeric": "1", "geo": "1,1", "tag": "1"}, ) - await modclient.ft().add_document( - "doc2", field="aab", text="2", numeric="2", geo="2,2", tag="2" + await modclient.hset( + "doc2", + mapping={"field": "aab", "text": "2", "numeric": "2", "geo": "2,2", "tag": "2"}, ) await waitForIndex(modclient, "idx") @@ -503,53 +458,6 @@ async def test_no_index(modclient: redis.Redis): @pytest.mark.redismod -async def test_partial(modclient: redis.Redis): - await ( - modclient.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) - ) - await modclient.ft().add_document("doc1", f1="f1_val", f2="f2_val") - await modclient.ft().add_document("doc2", f1="f1_val", f2="f2_val") - await modclient.ft().add_document("doc1", f3="f3_val", partial=True) - await modclient.ft().add_document("doc2", f3="f3_val", replace=True) - await waitForIndex(modclient, "idx") - - # Search for f3 value. All documents should have it - res = await modclient.ft().search("@f3:f3_val") - assert 2 == res.total - - # Only the document updated with PARTIAL should still have f1 and f2 values - res = await modclient.ft().search("@f3:f3_val @f2:f2_val @f1:f1_val") - assert 1 == res.total - - -@pytest.mark.redismod -async def test_no_create(modclient: redis.Redis): - await ( - modclient.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) - ) - await modclient.ft().add_document("doc1", f1="f1_val", f2="f2_val") - await modclient.ft().add_document("doc2", f1="f1_val", f2="f2_val") - await modclient.ft().add_document("doc1", f3="f3_val", no_create=True) - await modclient.ft().add_document("doc2", f3="f3_val", no_create=True, partial=True) - await waitForIndex(modclient, "idx") - - # Search for f3 value. All documents should have it - res = await modclient.ft().search("@f3:f3_val") - assert 2 == res.total - - # Only the document updated with PARTIAL should still have f1 and f2 values - res = await modclient.ft().search("@f3:f3_val @f2:f2_val @f1:f1_val") - assert 1 == res.total - - with pytest.raises(redis.ResponseError): - await ( - modclient.ft().add_document( - "doc3", f2="f2_val", f3="f3_val", no_create=True - ) - ) - - -@pytest.mark.redismod async def test_explain(modclient: redis.Redis): await ( modclient.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) @@ -640,11 +548,11 @@ async def test_alias_basic(modclient: redis.Redis): index1 = getClient(modclient).ft("testAlias") await index1.create_index((TextField("txt"),)) - await index1.add_document("doc1", txt="text goes here") + await index1.client.hset("doc1", mapping={"txt": "text goes here"}) index2 = getClient(modclient).ft("testAlias2") await index2.create_index((TextField("txt"),)) - await index2.add_document("doc2", txt="text goes here") + await index2.client.hset("doc2", mapping={"txt": "text goes here"}) # add the actual alias and check await index1.aliasadd("myalias") @@ -674,8 +582,8 @@ async def test_tags(modclient: redis.Redis): tags = "foo,foo bar,hello;world" tags2 = "soba,ramen" - await modclient.ft().add_document("doc1", txt="fooz barz", tags=tags) - await modclient.ft().add_document("doc2", txt="noodles", tags=tags2) + await modclient.hset("doc1", mapping={"txt": "fooz barz", "tags": tags}) + await modclient.hset("doc2", mapping={"txt": "noodles", "tags": tags2}) await waitForIndex(modclient, "idx") q = Query("@tags:{foo}") @@ -718,8 +626,8 @@ async def test_alter_schema_add(modclient: redis.Redis): await modclient.ft().alter_schema_add(TextField("body")) # Indexing a document - await modclient.ft().add_document( - "doc1", title="MyTitle", body="Some content only in the body" + await modclient.hset( + "doc1", mapping={"title": "MyTitle", "body": "Some content only in the body"} ) # Searching with parameter only in the body (the added field) @@ -735,11 +643,11 @@ async def test_spell_check(modclient: redis.Redis): await modclient.ft().create_index((TextField("f1"), TextField("f2"))) await ( - modclient.ft().add_document( - "doc1", f1="some valid content", f2="this is sample text" + modclient.hset( + "doc1", mapping={"f1": "some valid content", "f2": "this is sample text"} ) ) - await modclient.ft().add_document("doc2", f1="very important", f2="lorem ipsum") + await modclient.hset("doc2", mapping={"f1": "very important", "f2": "lorem ipsum"}) await waitForIndex(modclient, "idx") # test spellcheck @@ -793,8 +701,8 @@ async def test_dict_operations(modclient: redis.Redis): @pytest.mark.redismod async def test_phonetic_matcher(modclient: redis.Redis): await modclient.ft().create_index((TextField("name"),)) - await modclient.ft().add_document("doc1", name="Jon") - await modclient.ft().add_document("doc2", name="John") + await modclient.hset("doc1", mapping={"name": "Jon"}) + await modclient.hset("doc2", mapping={"name": "John"}) res = await modclient.ft().search(Query("Jon")) assert 1 == len(res.docs) @@ -804,8 +712,8 @@ async def test_phonetic_matcher(modclient: redis.Redis): await modclient.flushdb() await modclient.ft().create_index((TextField("name", phonetic_matcher="dm:en"),)) - await modclient.ft().add_document("doc1", name="Jon") - await modclient.ft().add_document("doc2", name="John") + await modclient.hset("doc1", mapping={"name": "Jon"}) + await modclient.hset("doc2", mapping={"name": "John"}) res = await modclient.ft().search(Query("Jon")) assert 2 == len(res.docs) @@ -817,12 +725,14 @@ async def test_phonetic_matcher(modclient: redis.Redis): async def test_scorer(modclient: redis.Redis): await modclient.ft().create_index((TextField("description"),)) - await modclient.ft().add_document( - "doc1", description="The quick brown fox jumps over the lazy dog" + await modclient.hset( + "doc1", mapping={"description": "The quick brown fox jumps over the lazy dog"} ) - await modclient.ft().add_document( + await modclient.hset( "doc2", - description="Quick alice was beginning to get very tired of sitting by her quick sister on the bank, and of having nothing to do.", # noqa + mapping={ + "description": "Quick alice was beginning to get very tired of sitting by her quick sister on the bank, and of having nothing to do." # noqa + }, ) # default scorer is TFIDF @@ -851,19 +761,19 @@ async def test_get(modclient: redis.Redis): assert [None] == await modclient.ft().get("doc1") assert [None, None] == await modclient.ft().get("doc2", "doc1") - await modclient.ft().add_document( - "doc1", f1="some valid content dd1", f2="this is sample text ff1" + await modclient.hset( + "doc1", mapping={"f1": "some valid content dd1", "f2": "this is sample text f1"} ) - await modclient.ft().add_document( - "doc2", f1="some valid content dd2", f2="this is sample text ff2" + await modclient.hset( + "doc2", mapping={"f1": "some valid content dd2", "f2": "this is sample text f2"} ) assert [ - ["f1", "some valid content dd2", "f2", "this is sample text ff2"] + ["f1", "some valid content dd2", "f2", "this is sample text f2"] ] == await modclient.ft().get("doc2") assert [ - ["f1", "some valid content dd1", "f2", "this is sample text ff1"], - ["f1", "some valid content dd2", "f2", "this is sample text ff2"], + ["f1", "some valid content dd1", "f2", "this is sample text f1"], + ["f1", "some valid content dd2", "f2", "this is sample text f2"], ] == await modclient.ft().get("doc1", "doc2") @@ -894,26 +804,32 @@ async def test_aggregations_groupby(modclient: redis.Redis): ) # Indexing a document - await modclient.ft().add_document( + await modclient.hset( "search", - title="RediSearch", - body="Redisearch impements a search engine on top of redis", - parent="redis", - random_num=10, - ) - await modclient.ft().add_document( + mapping={ + "title": "RediSearch", + "body": "Redisearch impements a search engine on top of redis", + "parent": "redis", + "random_num": 10, + }, + ) + await modclient.hset( "ai", - title="RedisAI", - body="RedisAI executes Deep Learning/Machine Learning models and managing their data.", # noqa - parent="redis", - random_num=3, - ) - await modclient.ft().add_document( + mapping={ + "title": "RedisAI", + "body": "RedisAI executes Deep Learning/Machine Learning models and managing their data.", # noqa + "parent": "redis", + "random_num": 3, + }, + ) + await modclient.hset( "json", - title="RedisJson", - body="RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.", # noqa - parent="redis", - random_num=8, + mapping={ + "title": "RedisJson", + "body": "RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.", # noqa + "parent": "redis", + "random_num": 8, + }, ) req = aggregations.AggregateRequest("redis").group_by("@parent", reducers.count()) @@ -1074,15 +990,14 @@ async def test_withsuffixtrie(modclient: redis.Redis): async def test_search_commands_in_pipeline(modclient: redis.Redis): p = await modclient.ft().pipeline() p.create_index((TextField("txt"),)) - p.add_document("doc1", payload="foo baz", txt="foo bar") - p.add_document("doc2", txt="foo bar") + p.hset("doc1", mapping={"txt": "foo bar"}) + p.hset("doc2", mapping={"txt": "foo bar"}) q = Query("foo bar").with_payloads() await p.search(q) res = await p.execute() - assert res[:3] == ["OK", "OK", "OK"] + assert res[:3] == ["OK", True, True] assert 2 == res[3][0] assert "doc1" == res[3][1] assert "doc2" == res[3][4] - assert "foo baz" == res[3][2] assert res[3][5] is None assert res[3][3] == res[3][6] == ["txt", "foo bar"] diff --git a/tests/test_search.py b/tests/test_search.py index abaa908..08c76f7 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -93,7 +93,7 @@ def createIndex(client, num_docs=100, definition=None): assert 50 == indexer.chunk_size for key, doc in chapters.items(): - indexer.add_document(key, **doc) + indexer.client.client.hset(key, mapping=doc) indexer.commit() @@ -196,7 +196,7 @@ def test_client(client): assert 167 == client.ft().search(Query("henry king").slop(100)).total # test delete document - client.ft().add_document("doc-5ghs2", play="Death of a Salesman") + client.hset("doc-5ghs2", mapping={"play": "Death of a Salesman"}) res = client.ft().search(Query("death of a salesman")) assert 1 == res.total @@ -205,36 +205,19 @@ def test_client(client): assert 0 == res.total assert 0 == client.ft().delete_document("doc-5ghs2") - client.ft().add_document("doc-5ghs2", play="Death of a Salesman") + client.hset("doc-5ghs2", mapping={"play": "Death of a Salesman"}) res = client.ft().search(Query("death of a salesman")) assert 1 == res.total client.ft().delete_document("doc-5ghs2") @pytest.mark.redismod -@skip_ifmodversion_lt("2.2.0", "search") -def test_payloads(client): - client.ft().create_index((TextField("txt"),)) - - client.ft().add_document("doc1", payload="foo baz", txt="foo bar") - client.ft().add_document("doc2", txt="foo bar") - - q = Query("foo bar").with_payloads() - res = client.ft().search(q) - assert 2 == res.total - assert "doc1" == res.docs[0].id - assert "doc2" == res.docs[1].id - assert "foo baz" == res.docs[0].payload - assert res.docs[1].payload is None - - -@pytest.mark.redismod @pytest.mark.onlynoncluster def test_scores(client): client.ft().create_index((TextField("txt"),)) - client.ft().add_document("doc1", txt="foo baz") - client.ft().add_document("doc2", txt="foo bar") + client.hset("doc1", mapping={"txt": "foo baz"}) + client.hset("doc2", mapping={"txt": "foo bar"}) q = Query("foo ~bar").with_scores() res = client.ft().search(q) @@ -247,31 +230,10 @@ def test_scores(client): @pytest.mark.redismod -def test_replace(client): - client.ft().create_index((TextField("txt"),)) - - client.ft().add_document("doc1", txt="foo bar") - client.ft().add_document("doc2", txt="foo bar") - waitForIndex(client, getattr(client.ft(), "index_name", "idx")) - - res = client.ft().search("foo bar") - assert 2 == res.total - client.ft().add_document("doc1", replace=True, txt="this is a replaced doc") - - res = client.ft().search("foo bar") - assert 1 == res.total - assert "doc2" == res.docs[0].id - - res = client.ft().search("replaced doc") - assert 1 == res.total - assert "doc1" == res.docs[0].id - - -@pytest.mark.redismod def test_stopwords(client): client.ft().create_index((TextField("txt"),), stopwords=["foo", "bar", "baz"]) - client.ft().add_document("doc1", txt="foo bar") - client.ft().add_document("doc2", txt="hello world") + client.hset("doc1", mapping={"txt": "foo bar"}) + client.hset("doc2", mapping={"txt": "hello world"}) waitForIndex(client, getattr(client.ft(), "index_name", "idx")) q1 = Query("foo bar").no_content() @@ -284,8 +246,10 @@ def test_stopwords(client): @pytest.mark.redismod def test_filters(client): client.ft().create_index((TextField("txt"), NumericField("num"), GeoField("loc"))) - client.ft().add_document("doc1", txt="foo bar", num=3.141, loc="-0.441,51.458") - client.ft().add_document("doc2", txt="foo baz", num=2, loc="-0.1,51.2") + client.hset( + "doc1", mapping={"txt": "foo bar", "num": 3.141, "loc": "-0.441,51.458"} + ) + client.hset("doc2", mapping={"txt": "foo baz", "num": 2, "loc": "-0.1,51.2"}) waitForIndex(client, getattr(client.ft(), "index_name", "idx")) # Test numerical filter @@ -318,22 +282,11 @@ def test_filters(client): @pytest.mark.redismod -def test_payloads_with_no_content(client): - client.ft().create_index((TextField("txt"),)) - client.ft().add_document("doc1", payload="foo baz", txt="foo bar") - client.ft().add_document("doc2", payload="foo baz2", txt="foo bar") - - q = Query("foo bar").with_payloads().no_content() - res = client.ft().search(q) - assert 2 == len(res.docs) - - -@pytest.mark.redismod def test_sort_by(client): client.ft().create_index((TextField("txt"), NumericField("num", sortable=True))) - client.ft().add_document("doc1", txt="foo bar", num=1) - client.ft().add_document("doc2", txt="foo baz", num=2) - client.ft().add_document("doc3", txt="foo qux", num=3) + client.hset("doc1", mapping={"txt": "foo bar", "num": 1}) + client.hset("doc2", mapping={"txt": "foo baz", "num": 2}) + client.hset("doc3", mapping={"txt": "foo qux", "num": 3}) # Test sort q1 = Query("foo").sort_by("num", asc=True).no_content() @@ -375,10 +328,12 @@ def test_example(client): client.ft().create_index((TextField("title", weight=5.0), TextField("body"))) # Indexing a document - client.ft().add_document( + client.hset( "doc1", - title="RediSearch", - body="Redisearch impements a search engine on top of redis", + mapping={ + "title": "RediSearch", + "body": "Redisearch impements a search engine on top of redis", + }, ) # Searching with complex parameters: @@ -450,11 +405,13 @@ def test_no_index(client): ) ) - client.ft().add_document( - "doc1", field="aaa", text="1", numeric="1", geo="1,1", tag="1" + client.hset( + "doc1", + mapping={"field": "aaa", "text": "1", "numeric": "1", "geo": "1,1", "tag": "1"}, ) - client.ft().add_document( - "doc2", field="aab", text="2", numeric="2", geo="2,2", tag="2" + client.hset( + "doc2", + mapping={"field": "aab", "text": "2", "numeric": "2", "geo": "2,2", "tag": "2"}, ) waitForIndex(client, getattr(client.ft(), "index_name", "idx")) @@ -492,45 +449,6 @@ def test_no_index(client): @pytest.mark.redismod -def test_partial(client): - client.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) - client.ft().add_document("doc1", f1="f1_val", f2="f2_val") - client.ft().add_document("doc2", f1="f1_val", f2="f2_val") - client.ft().add_document("doc1", f3="f3_val", partial=True) - client.ft().add_document("doc2", f3="f3_val", replace=True) - waitForIndex(client, getattr(client.ft(), "index_name", "idx")) - - # Search for f3 value. All documents should have it - res = client.ft().search("@f3:f3_val") - assert 2 == res.total - - # Only the document updated with PARTIAL should still have f1 and f2 values - res = client.ft().search("@f3:f3_val @f2:f2_val @f1:f1_val") - assert 1 == res.total - - -@pytest.mark.redismod -def test_no_create(client): - client.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) - client.ft().add_document("doc1", f1="f1_val", f2="f2_val") - client.ft().add_document("doc2", f1="f1_val", f2="f2_val") - client.ft().add_document("doc1", f3="f3_val", no_create=True) - client.ft().add_document("doc2", f3="f3_val", no_create=True, partial=True) - waitForIndex(client, getattr(client.ft(), "index_name", "idx")) - - # Search for f3 value. All documents should have it - res = client.ft().search("@f3:f3_val") - assert 2 == res.total - - # Only the document updated with PARTIAL should still have f1 and f2 values - res = client.ft().search("@f3:f3_val @f2:f2_val @f1:f1_val") - assert 1 == res.total - - with pytest.raises(redis.ResponseError): - client.ft().add_document("doc3", f2="f2_val", f3="f3_val", no_create=True) - - -@pytest.mark.redismod def test_explain(client): client.ft().create_index((TextField("f1"), TextField("f2"), TextField("f3"))) res = client.ft().explain("@f3:f3_val @f2:f2_val @f1:f1_val") @@ -618,11 +536,11 @@ def test_alias_basic(client): index1 = getClient(client).ft("testAlias") index1.create_index((TextField("txt"),)) - index1.add_document("doc1", txt="text goes here") + index1.client.hset("doc1", mapping={"txt": "text goes here"}) index2 = getClient(client).ft("testAlias2") index2.create_index((TextField("txt"),)) - index2.add_document("doc2", txt="text goes here") + index2.client.hset("doc2", mapping={"txt": "text goes here"}) # add the actual alias and check index1.aliasadd("myalias") @@ -647,36 +565,6 @@ def test_alias_basic(client): @pytest.mark.redismod -def test_tags(client): - client.ft().create_index((TextField("txt"), TagField("tags"))) - tags = "foo,foo bar,hello;world" - tags2 = "soba,ramen" - - client.ft().add_document("doc1", txt="fooz barz", tags=tags) - client.ft().add_document("doc2", txt="noodles", tags=tags2) - waitForIndex(client, getattr(client.ft(), "index_name", "idx")) - - q = Query("@tags:{foo}") - res = client.ft().search(q) - assert 1 == res.total - - q = Query("@tags:{foo bar}") - res = client.ft().search(q) - assert 1 == res.total - - q = Query("@tags:{foo\\ bar}") - res = client.ft().search(q) - assert 1 == res.total - - q = Query("@tags:{hello\\;world}") - res = client.ft().search(q) - assert 1 == res.total - - q2 = client.ft().tagvals("tags") - assert (tags.split(",") + tags2.split(",")).sort() == q2.sort() - - -@pytest.mark.redismod def test_textfield_sortable_nostem(client): # Creating the index definition with sortable and no_stem client.ft().create_index((TextField("txt", sortable=True, no_stem=True),)) @@ -696,8 +584,8 @@ def test_alter_schema_add(client): client.ft().alter_schema_add(TextField("body")) # Indexing a document - client.ft().add_document( - "doc1", title="MyTitle", body="Some content only in the body" + client.hset( + "doc1", mapping={"title": "MyTitle", "body": "Some content only in the body"} ) # Searching with parameter only in the body (the added field) @@ -712,8 +600,10 @@ def test_alter_schema_add(client): def test_spell_check(client): client.ft().create_index((TextField("f1"), TextField("f2"))) - client.ft().add_document("doc1", f1="some valid content", f2="this is sample text") - client.ft().add_document("doc2", f1="very important", f2="lorem ipsum") + client.hset( + "doc1", mapping={"f1": "some valid content", "f2": "this is sample text"} + ) + client.hset("doc2", mapping={"f1": "very important", "f2": "lorem ipsum"}) waitForIndex(client, getattr(client.ft(), "index_name", "idx")) # test spellcheck @@ -767,8 +657,8 @@ def test_dict_operations(client): @pytest.mark.redismod def test_phonetic_matcher(client): client.ft().create_index((TextField("name"),)) - client.ft().add_document("doc1", name="Jon") - client.ft().add_document("doc2", name="John") + client.hset("doc1", mapping={"name": "Jon"}) + client.hset("doc2", mapping={"name": "John"}) res = client.ft().search(Query("Jon")) assert 1 == len(res.docs) @@ -778,8 +668,8 @@ def test_phonetic_matcher(client): client.flushdb() client.ft().create_index((TextField("name", phonetic_matcher="dm:en"),)) - client.ft().add_document("doc1", name="Jon") - client.ft().add_document("doc2", name="John") + client.hset("doc1", mapping={"name": "Jon"}) + client.hset("doc2", mapping={"name": "John"}) res = client.ft().search(Query("Jon")) assert 2 == len(res.docs) @@ -791,12 +681,14 @@ def test_phonetic_matcher(client): def test_scorer(client): client.ft().create_index((TextField("description"),)) - client.ft().add_document( - "doc1", description="The quick brown fox jumps over the lazy dog" + client.hset( + "doc1", mapping={"description": "The quick brown fox jumps over the lazy dog"} ) - client.ft().add_document( + client.hset( "doc2", - description="Quick alice was beginning to get very tired of sitting by her quick sister on the bank, and of having nothing to do.", # noqa + mapping={ + "description": "Quick alice was beginning to get very tired of sitting by her quick sister on the bank, and of having nothing to do." # noqa + }, ) # default scorer is TFIDF @@ -823,19 +715,19 @@ def test_get(client): assert [None] == client.ft().get("doc1") assert [None, None] == client.ft().get("doc2", "doc1") - client.ft().add_document( - "doc1", f1="some valid content dd1", f2="this is sample text ff1" + client.hset( + "doc1", mapping={"f1": "some valid content dd1", "f2": "this is sample text f1"} ) - client.ft().add_document( - "doc2", f1="some valid content dd2", f2="this is sample text ff2" + client.hset( + "doc2", mapping={"f1": "some valid content dd2", "f2": "this is sample text f2"} ) assert [ - ["f1", "some valid content dd2", "f2", "this is sample text ff2"] + ["f1", "some valid content dd2", "f2", "this is sample text f2"] ] == client.ft().get("doc2") assert [ - ["f1", "some valid content dd1", "f2", "this is sample text ff1"], - ["f1", "some valid content dd2", "f2", "this is sample text ff2"], + ["f1", "some valid content dd1", "f2", "this is sample text f1"], + ["f1", "some valid content dd2", "f2", "this is sample text f2"], ] == client.ft().get("doc1", "doc2") @@ -866,26 +758,32 @@ def test_aggregations_groupby(client): ) # Indexing a document - client.ft().add_document( + client.hset( "search", - title="RediSearch", - body="Redisearch impements a search engine on top of redis", - parent="redis", - random_num=10, + mapping={ + "title": "RediSearch", + "body": "Redisearch impements a search engine on top of redis", + "parent": "redis", + "random_num": 10, + }, ) - client.ft().add_document( + client.hset( "ai", - title="RedisAI", - body="RedisAI executes Deep Learning/Machine Learning models and managing their data.", # noqa - parent="redis", - random_num=3, + mapping={ + "title": "RedisAI", + "body": "RedisAI executes Deep Learning/Machine Learning models and managing their data.", # noqa + "parent": "redis", + "random_num": 3, + }, ) - client.ft().add_document( + client.hset( "json", - title="RedisJson", - body="RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.", # noqa - parent="redis", - random_num=8, + mapping={ + "title": "RedisJson", + "body": "RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.", # noqa + "parent": "redis", + "random_num": 8, + }, ) req = aggregations.AggregateRequest("redis").group_by("@parent", reducers.count()) @@ -1134,7 +1032,7 @@ def test_index_definition(client): @pytest.mark.redismod @pytest.mark.onlynoncluster @skip_if_redis_enterprise() -def testExpire(client): +def test_expire(client): client.ft().create_index((TextField("txt", sortable=True),), temporary=4) ttl = client.execute_command("ft.debug", "TTL", "idx") assert ttl > 2 @@ -1143,20 +1041,9 @@ def testExpire(client): ttl = client.execute_command("ft.debug", "TTL", "idx") time.sleep(0.01) - # add document - should reset the ttl - client.ft().add_document("doc", txt="foo bar", text="this is a simple test") - ttl = client.execute_command("ft.debug", "TTL", "idx") - assert ttl > 2 - try: - while True: - ttl = client.execute_command("ft.debug", "TTL", "idx") - time.sleep(0.5) - except redis.exceptions.ResponseError: - assert ttl == 0 - @pytest.mark.redismod -def testSkipInitialScan(client): +def test_skip_initial_scan(client): client.hset("doc1", "foo", "bar") q = Query("@foo:bar") @@ -1165,23 +1052,23 @@ def testSkipInitialScan(client): @pytest.mark.redismod -def testSummarizeDisabled_nooffset(client): +def test_summarize_disabled_nooffset(client): client.ft().create_index((TextField("txt"),), no_term_offsets=True) - client.ft().add_document("doc1", txt="foo bar") + client.hset("doc1", mapping={"txt": "foo bar"}) with pytest.raises(Exception): client.ft().search(Query("foo").summarize(fields=["txt"])) @pytest.mark.redismod -def testSummarizeDisabled_nohl(client): +def test_summarize_disabled_nohl(client): client.ft().create_index((TextField("txt"),), no_highlight=True) - client.ft().add_document("doc1", txt="foo bar") + client.hset("doc1", mapping={"txt": "foo bar"}) with pytest.raises(Exception): client.ft().search(Query("foo").summarize(fields=["txt"])) @pytest.mark.redismod -def testMaxTextFields(client): +def test_max_text_fields(client): # Creating the index definition client.ft().create_index((TextField("f0"),)) for x in range(1, 32): @@ -1337,10 +1224,10 @@ def test_synupdate(client): ) client.ft().synupdate("id1", True, "boy", "child", "offspring") - client.ft().add_document("doc1", title="he is a baby", body="this is a test") + client.hset("doc1", mapping={"title": "he is a baby", "body": "this is a test"}) client.ft().synupdate("id1", True, "baby") - client.ft().add_document("doc2", title="he is another baby", body="another test") + client.hset("doc2", mapping={"title": "he is another baby", "body": "another test"}) res = client.ft().search(Query("child").expander("SYNONYM")) assert res.docs[0].id == "doc2" @@ -1579,9 +1466,9 @@ def test_text_params(modclient): modclient.flushdb() modclient.ft().create_index((TextField("name"),)) - modclient.ft().add_document("doc1", name="Alice") - modclient.ft().add_document("doc2", name="Bob") - modclient.ft().add_document("doc3", name="Carol") + modclient.hset("doc1", mapping={"name": "Alice"}) + modclient.hset("doc2", mapping={"name": "Bob"}) + modclient.hset("doc3", mapping={"name": "Carol"}) params_dict = {"name1": "Alice", "name2": "Bob"} q = Query("@name:($name1 | $name2 )").dialect(2) @@ -1597,9 +1484,9 @@ def test_numeric_params(modclient): modclient.flushdb() modclient.ft().create_index((NumericField("numval"),)) - modclient.ft().add_document("doc1", numval=101) - modclient.ft().add_document("doc2", numval=102) - modclient.ft().add_document("doc3", numval=103) + modclient.hset("doc1", mapping={"numval": 101}) + modclient.hset("doc2", mapping={"numval": 102}) + modclient.hset("doc3", mapping={"numval": 103}) params_dict = {"min": 101, "max": 102} q = Query("@numval:[$min $max]").dialect(2) @@ -1616,9 +1503,9 @@ def test_geo_params(modclient): modclient.flushdb() modclient.ft().create_index((GeoField("g"))) - modclient.ft().add_document("doc1", g="29.69465, 34.95126") - modclient.ft().add_document("doc2", g="29.69350, 34.94737") - modclient.ft().add_document("doc3", g="29.68746, 34.94882") + modclient.hset("doc1", mapping={"g": "29.69465, 34.95126"}) + modclient.hset("doc2", mapping={"g": "29.69350, 34.94737"}) + modclient.hset("doc3", mapping={"g": "29.68746, 34.94882"}) params_dict = {"lat": "34.95126", "lon": "29.69465", "radius": 1000, "units": "km"} q = Query("@g:[$lon $lat $radius $units]").dialect(2) @@ -1634,16 +1521,15 @@ def test_geo_params(modclient): def test_search_commands_in_pipeline(client): p = client.ft().pipeline() p.create_index((TextField("txt"),)) - p.add_document("doc1", payload="foo baz", txt="foo bar") - p.add_document("doc2", txt="foo bar") + p.hset("doc1", mapping={"txt": "foo bar"}) + p.hset("doc2", mapping={"txt": "foo bar"}) q = Query("foo bar").with_payloads() p.search(q) res = p.execute() - assert res[:3] == ["OK", "OK", "OK"] + assert res[:3] == ["OK", True, True] assert 2 == res[3][0] assert "doc1" == res[3][1] assert "doc2" == res[3][4] - assert "foo baz" == res[3][2] assert res[3][5] is None assert res[3][3] == res[3][6] == ["txt", "foo bar"] |