|
| 1 | +from .commands import SearchCommands |
| 2 | + |
| 3 | + |
| 4 | +class Search(SearchCommands): |
| 5 | + """ |
| 6 | + Create a client for talking to search. |
| 7 | + It abstracts the API of the module and lets you just use the engine. |
| 8 | + """ |
| 9 | + |
| 10 | + class BatchIndexer(object): |
| 11 | + """ |
| 12 | + A batch indexer allows you to automatically batch |
| 13 | + document indexing in pipelines, flushing it every N documents. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, client, chunk_size=1000): |
| 17 | + |
| 18 | + self.client = client |
| 19 | + self.execute_command = client.execute_command |
| 20 | + self.pipeline = client.pipeline(transaction=False, shard_hint=None) |
| 21 | + self.total = 0 |
| 22 | + self.chunk_size = chunk_size |
| 23 | + self.current_chunk = 0 |
| 24 | + |
| 25 | + def __del__(self): |
| 26 | + if self.current_chunk: |
| 27 | + self.commit() |
| 28 | + |
| 29 | + def add_document( |
| 30 | + self, |
| 31 | + doc_id, |
| 32 | + nosave=False, |
| 33 | + score=1.0, |
| 34 | + payload=None, |
| 35 | + replace=False, |
| 36 | + partial=False, |
| 37 | + no_create=False, |
| 38 | + **fields |
| 39 | + ): |
| 40 | + """ |
| 41 | + Add a document to the batch query |
| 42 | + """ |
| 43 | + self.client._add_document( |
| 44 | + doc_id, |
| 45 | + conn=self.pipeline, |
| 46 | + nosave=nosave, |
| 47 | + score=score, |
| 48 | + payload=payload, |
| 49 | + replace=replace, |
| 50 | + partial=partial, |
| 51 | + no_create=no_create, |
| 52 | + **fields |
| 53 | + ) |
| 54 | + self.current_chunk += 1 |
| 55 | + self.total += 1 |
| 56 | + if self.current_chunk >= self.chunk_size: |
| 57 | + self.commit() |
| 58 | + |
| 59 | + def add_document_hash( |
| 60 | + self, |
| 61 | + doc_id, |
| 62 | + score=1.0, |
| 63 | + replace=False, |
| 64 | + ): |
| 65 | + """ |
| 66 | + Add a hash to the batch query |
| 67 | + """ |
| 68 | + self.client._add_document_hash( |
| 69 | + doc_id, |
| 70 | + conn=self.pipeline, |
| 71 | + score=score, |
| 72 | + replace=replace, |
| 73 | + ) |
| 74 | + self.current_chunk += 1 |
| 75 | + self.total += 1 |
| 76 | + if self.current_chunk >= self.chunk_size: |
| 77 | + self.commit() |
| 78 | + |
| 79 | + def commit(self): |
| 80 | + """ |
| 81 | + Manually commit and flush the batch indexing query |
| 82 | + """ |
| 83 | + self.pipeline.execute() |
| 84 | + self.current_chunk = 0 |
| 85 | + |
| 86 | + def __init__(self, client, index_name="idx"): |
| 87 | + """ |
| 88 | + Create a new Client for the given index_name. |
| 89 | + The default name is `idx` |
| 90 | +
|
| 91 | + If conn is not None, we employ an already existing redis connection |
| 92 | + """ |
| 93 | + self.client = client |
| 94 | + self.index_name = index_name |
| 95 | + self.execute_command = client.execute_command |
| 96 | + self.pipeline = client.pipeline |
0 commit comments