def find_similar(self, doc, min_score=<NUM_LIT:0.0>, max_results=<NUM_LIT:100>):
logger.debug("<STR_LIT>" % doc)<EOL>if self.is_locked():<EOL><INDENT>msg = "<STR_LIT>"<EOL>logger.error(msg)<EOL>raise RuntimeError(msg)<EOL><DEDENT>sims_opt, sims_fresh = None, None<EOL>for index in [self.fresh_index, self.opt_index]:<EOL><INDENT>if index is not None:<EOL><INDENT>index.topsims = max_results<EOL><DEDENT><DEDENT>if isinstance(doc, str):<EOL><INDENT>docid = doc<EOL>if self.opt_index is not None and docid in self.opt_index:<EOL><INDENT>sims_opt = self.opt_index.sims_by_id(docid)<EOL>if self.fresh_index is not None:<EOL><INDENT>vec = self.opt_index.vec_by_id(docid)<EOL>sims_fresh = self.fresh_index.sims_by_vec(vec, normalize=False)<EOL><DEDENT><DEDENT>elif self.fresh_index is not None and docid in self.fresh_index:<EOL><INDENT>sims_fresh = self.fresh_index.sims_by_id(docid)<EOL>if self.opt_index is not None:<EOL><INDENT>vec = self.fresh_index.vec_by_id(docid)<EOL>sims_opt = self.opt_index.sims_by_vec(vec, normalize=False)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % docid)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if '<STR_LIT>' in doc:<EOL><INDENT>vec = gensim.matutils.any2sparse(doc['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>vec = self.model.doc2vec(doc) <EOL><DEDENT>if self.opt_index is not None:<EOL><INDENT>sims_opt = self.opt_index.sims_by_vec(vec)<EOL><DEDENT>if self.fresh_index is not None:<EOL><INDENT>sims_fresh = self.fresh_index.sims_by_vec(vec)<EOL><DEDENT><DEDENT>merged = merge_sims(sims_opt, sims_fresh)<EOL>logger.debug("<STR_LIT>" %<EOL>(len(merged), max_results, min_score))<EOL>result = []<EOL>for docid, score in merged:<EOL><INDENT>if score < min_score or <NUM_LIT:0> < max_results <= len(result):<EOL><INDENT>break<EOL><DEDENT>result.append((docid, float(score), self.payload.get(docid, None)))<EOL><DEDENT>return result<EOL>
Find `max_results` most similar articles in the index, each having similarity
score of at least `min_score`. The resulting list may be shorter than `max_results`,
in case there are not enough matching documents.

`doc` is either a string (=document id, previously indexed) or a
dict containing a 'tokens' key. These tokens are processed to produce a
vector, which is then used as a query against the index.

The similar documents are returned in decreasing similarity order, as
`(doc_id, similarity_score, doc_payload)` 3-tuples. The payload returned
is identical to what was supplied for this document during indexing.