def parse(self, input): """Parses the input string and returns a Query object/tree. This method may return None if the input string does not result in any valid queries. It may also raise a variety of exceptions if the input string is malformed. :input: the unicode string to parse. """ required = [] optional = [] gramsize = max(self.minchars, min(self.maxchars, len(input))) if gramsize > len(input): return None discardspaces = self.discardspaces for t in self.analyzerclass(gramsize)(input): gram = t.text if " " in gram: if not discardspaces: optional.append(gram) else: required.append(gram) if required: fieldname = self.fieldname andquery = query.And([query.Term(fieldname, g) for g in required]) if optional: orquery = query.Or([query.Term(fieldname, g) for g in optional]) return query.AndMaybe([andquery, orquery]) else: return andquery else: return None
def parse(self, input): required = [] optional = [] gramsize = max(self.minchars, min(self.maxchars, len(input))) if gramsize > len(input): return None discardspaces = self.discardspaces for t in self.analyzerclass(gramsize)(input): gram = t.text if " " in gram: if not discardspaces: optional.append(gram) else: required.append(gram) if required: fieldname = self.fieldname andquery = query.And([query.Term(fieldname, g) for g in required]) if optional: orquery = query.Or( [query.Term(fieldname, g) for g in optional]) return query.AndMaybe([andquery, orquery]) else: return andquery else: return None
def _Toplevel(self, node, fieldname): queries = [self._eval(s, fieldname) for s in node] reqds = [q[0] for q in queries if isinstance(q, tuple)] if reqds: nots = [q for q in queries if isinstance(q, query.Not)] opts = [q for q in queries if not isinstance(q, query.Not) and not isinstance(q, tuple)] return query.AndMaybe([query.And(reqds + nots), query.Or(opts)]) else: return query.Or(queries)
def query(self, parser): assert len(self.tokens) == 2 return query.AndMaybe(self.tokens[0].query(parser), self.tokens[1].query(parser), boost=self.boost)