Returns - : the return value of the _find_documents_with_idx method Input - str: a string of words called `search_term` --------- - retrieve the reverse index - use the words contained in the search term to find all the idxs that conta
(self, search_term)
| 88 | return res.lastrowid |
| 89 | |
| 90 | def find_documents(self, search_term): |
| 91 | """ |
| 92 | Returns - <class method>: the return value of the _find_documents_with_idx method |
| 93 | Input - str: a string of words called `search_term` |
| 94 | --------- |
| 95 | - retrieve the reverse index |
| 96 | - use the words contained in the search term to find all the idxs |
| 97 | that contain the word |
| 98 | - use idxs to call the _find_documents_with_idx method |
| 99 | - return the result of the called method |
| 100 | """ |
| 101 | cur = self.conn.cursor() |
| 102 | reverse_idx = cur.execute( |
| 103 | "SELECT value FROM WordToId WHERE name='index'" |
| 104 | ).fetchone()[0] |
| 105 | reverse_idx = json.loads(reverse_idx) |
| 106 | search_term = search_term.split(" ") |
| 107 | all_docs_with_search_term = [] |
| 108 | for term in search_term: |
| 109 | if term in reverse_idx: |
| 110 | all_docs_with_search_term.append(reverse_idx[term]) |
| 111 | |
| 112 | if not all_docs_with_search_term: # the search term does not exist |
| 113 | return [] |
| 114 | |
| 115 | common_idx_of_docs = set(all_docs_with_search_term[0]) |
| 116 | for idx in all_docs_with_search_term[1:]: |
| 117 | common_idx_of_docs.intersection_update(idx) |
| 118 | |
| 119 | if not common_idx_of_docs: # the search term does not exist |
| 120 | return [] |
| 121 | |
| 122 | return self._find_documents_with_idx(common_idx_of_docs) |
| 123 | |
| 124 | def _find_documents_with_idx(self, idxs): |
| 125 | """ |
no test coverage detected