def matcher(self, searcher, context=None): from whoosh.query import Term, SpanNear2 fieldname = self.fieldname if fieldname not in searcher.schema: return matching.NullMatcher() field = searcher.schema[fieldname] if not field.format or not field.format.supports("positions"): raise qcore.QueryError("Phrase search: %r field has no positions" % self.fieldname) terms = [] # Build a list of Term queries from the words in the phrase reader = searcher.reader() for word in self.words: word = field.to_bytes(word) if (fieldname, word) not in reader: # Shortcut the query if one of the words doesn't exist. return matching.NullMatcher() terms.append(Term(fieldname, word)) # Create the equivalent SpanNear2 query from the terms q = SpanNear2(terms, slop=self.slop, ordered=True, mindist=1) # Get the matcher m = q.matcher(searcher, context) if self.boost != 1.0: m = matching.WrappingMatcher(m, boost=self.boost) return m
def matcher(self, searcher, context=None): fieldname = self.fieldname reader = searcher.reader() if fieldname not in searcher.schema: return matching.NullMatcher() field = searcher.schema[fieldname] words = [field.to_bytes(word) for word in self.words] # Shortcut the query if one of the words doesn't exist. for word in words: if (fieldname, word) not in reader: return matching.NullMatcher() if not field.format or not field.format.supports("positions"): raise qcore.QueryError("Phrase search: %r field has no positions" % self.fieldname) # Construct a tree of SpanNear queries representing the words in the # phrase and return its matcher from whoosh.query.spans import SpanNear q = SpanNear.phrase(fieldname, words, slop=self.slop) m = q.matcher(searcher, context) if self.boost != 1.0: m = matching.WrappingMatcher(m, boost=self.boost) return m
def __init__(self, subqueries, boost=1.0): for subq in subqueries: if not isinstance(subq, qcore.Query): raise qcore.QueryError("%r is not a query" % subq) self.subqueries = subqueries self.boost = boost