Collection<ScoreDocPtr> search() { // create PhraseQuery "term1 term2" and search PhraseQueryPtr pq = newLucene<PhraseQuery>(); pq->add(newLucene<Term>(field, term1)); pq->add(newLucene<Term>(field, term2)); return searcher->search(pq, FilterPtr(), 1000)->scoreDocs; }
PhraseQueryPtr makePhraseQuery(const String& terms) { PhraseQueryPtr query = newLucene<PhraseQuery>(); Collection<String> tokens = StringUtils::split(terms, L" +"); for (int32_t i = 0; i < tokens.size(); ++i) query->add(newLucene<Term>(L"f", tokens[i])); return query; }
PhraseWeight::PhraseWeight(PhraseQueryPtr query, SearcherPtr searcher) { this->query = query; this->similarity = query->getSimilarity(searcher); this->value = 0.0; this->idf = 0.0; this->queryNorm = 0.0; this->queryWeight = 0.0; this->idfExp = similarity->idfExplain(query->terms, searcher); idf = idfExp->getIdf(); }
double checkPhraseQuery(DocumentPtr doc, PhraseQueryPtr query, int32_t slop, int32_t expectedNumResults) { query->setSlop(slop); RAMDirectoryPtr ramDir = newLucene<RAMDirectory>(); WhitespaceAnalyzerPtr analyzer = newLucene<WhitespaceAnalyzer>(); IndexWriterPtr writer = newLucene<IndexWriter>(ramDir, analyzer, IndexWriter::MaxFieldLengthUNLIMITED); writer->addDocument(doc); writer->close(); IndexSearcherPtr searcher = newLucene<IndexSearcher>(ramDir, true); TopDocsPtr td = searcher->search(query, FilterPtr(), 10); BOOST_CHECK_EQUAL(expectedNumResults, td->totalHits); searcher->close(); ramDir->close(); return td->maxScore; }