MCPcopy
hub / github.com/redis/redis-py / createIndex

Method createIndex

tests/test_asyncio/test_search.py:101–138  ·  view source on GitHub ↗
(decoded_r, num_docs=100, definition=None)

Source from the content-addressed store, hash-verified

99
100 @staticmethod
101 async def createIndex(decoded_r, num_docs=100, definition=None):
102 try:
103 await decoded_r.create_index(
104 (
105 TextField("play", weight=5.0),
106 TextField("txt"),
107 NumericField("chapter"),
108 ),
109 definition=definition,
110 )
111 except redis.ResponseError:
112 await decoded_r.dropindex(delete_documents=True)
113 return await AsyncSearchTestsBase.createIndex(
114 decoded_r, num_docs=num_docs, definition=definition
115 )
116
117 chapters = {}
118 bzfp = TextIOWrapper(bz2.BZ2File(WILL_PLAY_TEXT), encoding="utf8")
119
120 r = csv.reader(bzfp, delimiter=";")
121 for n, line in enumerate(r):
122 play, chapter, _, text = line[1], line[2], line[4], line[5]
123
124 key = f"{play}:{chapter}".lower()
125 d = chapters.setdefault(key, {})
126 d["play"] = play
127 d["txt"] = d.get("txt", "") + " " + text
128 d["chapter"] = int(chapter or 0)
129 if len(chapters) == num_docs:
130 break
131
132 indexer = decoded_r.batch_indexer(chunk_size=50)
133 assert isinstance(indexer, AsyncSearch.BatchIndexer)
134 assert 50 == indexer.chunk_size
135
136 for key, doc in chapters.items():
137 await indexer.client.client.hset(key, mapping=doc)
138 await indexer.commit()
139
140
141class TestBaseSearchFunctionality(AsyncSearchTestsBase):

Callers 2

test_clientMethod · 0.45
test_summarizeMethod · 0.45

Calls 8

TextFieldClass · 0.90
NumericFieldClass · 0.90
create_indexMethod · 0.80
dropindexMethod · 0.80
batch_indexerMethod · 0.80
hsetMethod · 0.80
getMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected