PhraseCompiler* PhraseCompiler_init(PhraseCompiler *self, PhraseQuery *parent, Searcher *searcher, float boost) { PhraseCompilerIVARS *const ivars = PhraseCompiler_IVARS(self); PhraseQueryIVARS *const parent_ivars = PhraseQuery_IVARS(parent); Schema *schema = Searcher_Get_Schema(searcher); Similarity *sim = Schema_Fetch_Sim(schema, parent_ivars->field); Vector *terms = parent_ivars->terms; // Try harder to find a Similarity if necessary. if (!sim) { sim = Schema_Get_Similarity(schema); } // Init. Compiler_init((Compiler*)self, (Query*)parent, searcher, sim, boost); // Store IDF for the phrase. ivars->idf = 0; for (uint32_t i = 0, max = Vec_Get_Size(terms); i < max; i++) { Obj *term = Vec_Fetch(terms, i); int32_t doc_max = Searcher_Doc_Max(searcher); int32_t doc_freq = Searcher_Doc_Freq(searcher, parent_ivars->field, term); ivars->idf += Sim_IDF(sim, doc_freq, doc_max); } // Calculate raw weight. ivars->raw_weight = ivars->idf * ivars->boost; return self; }
PhraseCompiler* PhraseCompiler_init(PhraseCompiler *self, PhraseQuery *parent, Searcher *searcher, float boost) { Schema *schema = Searcher_Get_Schema(searcher); Similarity *sim = Schema_Fetch_Sim(schema, parent->field); VArray *terms = parent->terms; uint32_t i, max; // Try harder to find a Similarity if necessary. if (!sim) { sim = Schema_Get_Similarity(schema); } // Init. Compiler_init((Compiler*)self, (Query*)parent, searcher, sim, boost); // Store IDF for the phrase. self->idf = 0; for (i = 0, max = VA_Get_Size(terms); i < max; i++) { Obj *term = VA_Fetch(terms, i); int32_t doc_max = Searcher_Doc_Max(searcher); int32_t doc_freq = Searcher_Doc_Freq(searcher, parent->field, term); self->idf += Sim_IDF(sim, doc_freq, doc_max); } // Calculate raw weight. self->raw_weight = self->idf * self->boost; // Make final preparations. PhraseCompiler_Normalize(self); return self; }
uint32_t PolySearcher_Doc_Freq_IMP(PolySearcher *self, String *field, Obj *term) { PolySearcherIVARS *const ivars = PolySearcher_IVARS(self); uint32_t doc_freq = 0; for (uint32_t i = 0, max = VA_Get_Size(ivars->searchers); i < max; i++) { Searcher *searcher = (Searcher*)VA_Fetch(ivars->searchers, i); doc_freq += Searcher_Doc_Freq(searcher, field, term); } return doc_freq; }
TermCompiler* TermCompiler_init(TermCompiler *self, Query *parent, Searcher *searcher, float boost) { TermCompilerIVARS *const ivars = TermCompiler_IVARS(self); TermQueryIVARS *const parent_ivars = TermQuery_IVARS((TermQuery*)parent); Schema *schema = Searcher_Get_Schema(searcher); Similarity *sim = Schema_Fetch_Sim(schema, parent_ivars->field); // Try harder to get a Similarity if necessary. if (!sim) { sim = Schema_Get_Similarity(schema); } // Init. Compiler_init((Compiler*)self, parent, searcher, sim, boost); ivars->normalized_weight = 0.0f; ivars->query_norm_factor = 0.0f; // Derive. int32_t doc_max = Searcher_Doc_Max(searcher); uint32_t doc_freq = Searcher_Doc_Freq(searcher, parent_ivars->field, parent_ivars->term); ivars->idf = Sim_IDF(sim, (int32_t)doc_freq, doc_max); /* The score of any document is approximately equal to: * * (tf_d * idf_t / norm_d) * (tf_q * idf_t / norm_q) * * Here we add in the first IDF, plus user-supplied boost. * * The second clause is factored in by the call to Normalize(). * * tf_d and norm_d can only be added by the Matcher, since they are * per-document. */ ivars->raw_weight = ivars->idf * ivars->boost; return self; }