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

Method createIndex

tests/test_search.py:130–167  ·  view source on GitHub ↗
(client, num_docs=100, definition=None)

Source from the content-addressed store, hash-verified

128
129 @staticmethod
130 def createIndex(client, num_docs=100, definition=None):
131 try:
132 client.create_index(
133 (
134 TextField("play", weight=5.0),
135 TextField("txt"),
136 NumericField("chapter"),
137 ),
138 definition=definition,
139 )
140 except redis.ResponseError:
141 client.dropindex(delete_documents=True)
142 return SearchTestsBase.createIndex(
143 client, num_docs=num_docs, definition=definition
144 )
145
146 chapters = {}
147 bzfp = TextIOWrapper(bz2.BZ2File(WILL_PLAY_TEXT), encoding="utf8")
148
149 r = csv.reader(bzfp, delimiter=";")
150 for n, line in enumerate(r):
151 play, chapter, _, text = line[1], line[2], line[4], line[5]
152
153 key = f"{play}:{chapter}".lower()
154 d = chapters.setdefault(key, {})
155 d["play"] = play
156 d["txt"] = d.get("txt", "") + " " + text
157 d["chapter"] = int(chapter or 0)
158 if len(chapters) == num_docs:
159 break
160
161 indexer = client.batch_indexer(chunk_size=50)
162 assert isinstance(indexer, Search.BatchIndexer)
163 assert 50 == indexer.chunk_size
164
165 for key, doc in chapters.items():
166 indexer.client.client.hset(key, mapping=doc)
167 indexer.commit()
168
169 @pytest.fixture
170 def client(self, request, stack_url):

Callers 5

test_clientMethod · 0.45
test_summarizeMethod · 0.45
test_index_definitionMethod · 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