def testSpanNearQuery(self): quick_brown_dog = [self.quick, self.brown, self.dog] snq = SpanNearQuery(quick_brown_dog, 0, True) self.assertNoMatches(snq) self.dumpSpans(snq) snq = SpanNearQuery(quick_brown_dog, 4, True) self.assertNoMatches(snq) self.dumpSpans(snq) snq = SpanNearQuery(quick_brown_dog, 5, True) self.assertOnlyBrownFox(snq) self.dumpSpans(snq) # interesting - even a sloppy phrase query would require # more slop to match snq = SpanNearQuery([self.lazy, self.fox], 3, False) self.assertOnlyBrownFox(snq) self.dumpSpans(snq) pq = PhraseQuery() pq.add(Term("f", "lazy")) pq.add(Term("f", "fox")) pq.setSlop(4) self.assertNoMatches(pq) pq.setSlop(5) self.assertOnlyBrownFox(pq)
def matched(self, phrase, slop): query = PhraseQuery() query.setSlop(slop) for word in phrase: query.add(Term("field", word)) topDocs = self.searcher.search(query, 50) return topDocs.totalHits > 0
def testSearchByAPI(self): tq = TermQuery(Term("content", "hops")) topDocs = self.searcher.search(tq, 50) self.assertEqual(1, topDocs.totalHits) pq = PhraseQuery() pq.add(Term("content", "fox")) pq.add(Term("content", "hops")) topDocs = self.searcher.search(pq, 50) self.assertEquals(1, topDocs.totalHits)
def getFieldQuery_slop(self, field, queryText, slop): orig = super(CustomQueryParser, self).getFieldQuery_slop_super(field, queryText, slop) if not PhraseQuery.instance_(orig): return orig pq = PhraseQuery.cast_(orig) clauses = [SpanTermQuery(term) for term in pq.getTerms()] return SpanNearQuery(clauses, slop, True);
def getFieldQuery(self, field, queryText, slop=None): if slop is None: return super(CustomQueryParser, self).getFieldQuery(field, queryText) # let QueryParser's implementation do the analysis orig = super(CustomQueryParser, self).getFieldQuery(field, queryText, slop) if "PhraseQuery" != orig.getClassName(): return orig pq = PhraseQuery.cast_(orig) clauses = [SpanTermQuery(term) for term in pq.getTerms()] return SpanNearQuery(clauses, slop, True);
def testAgainstOR(self): quickFox = PhraseQuery() quickFox.setSlop(1) quickFox.add(Term("field", "quick")) quickFox.add(Term("field", "fox")) fastFox = PhraseQuery() fastFox.add(Term("field", "fast")) fastFox.add(Term("field", "fox")) query = BooleanQuery() query.add(quickFox, BooleanClause.Occur.SHOULD) query.add(fastFox, BooleanClause.Occur.SHOULD) topDocs = self.searcher.search(query, 10) self.assertEqual(2, topDocs.totalHits)