Ejemplo n.º 1
0
    def multitoken_query(self, spec, texts, fieldname, termclass, boost):
        """Returns a query for multiple texts. This method implements the
        intention specified in the field's ``multitoken_query`` attribute,
        which specifies what to do when strings that look like single terms
        to the parser turn out to yield multiple tokens when analyzed.

        :param spec: a string describing how to join the text strings into a
            query. This is usually the value of the field's
            ``multitoken_query`` attribute.
        :param texts: a list of token strings.
        :param fieldname: the name of the field.
        :param termclass: the query class to use for single terms.
        :param boost: the original term's boost in the query string, should be
            applied to the returned query object.
        """

        spec = spec.lower()
        if spec == "first":
            # Throw away all but the first token
            return termclass(fieldname, texts[0], boost=boost)
        elif spec == "phrase":
            # Turn the token into a phrase
            return self.phraseclass(fieldname, texts, boost=boost)
        else:
            if spec == "default":
                qclass = self.group.qclass
            elif spec == "and":
                qclass = query.And
            elif spec == "or":
                qclass = query.Or
            else:
                raise QueryParserError("Unknown multitoken_query value %r"
                                       % spec)
            return qclass([termclass(fieldname, t, boost=boost)
                           for t in texts])
Ejemplo n.º 2
0
    def parse_range(self,
                    fieldname,
                    start,
                    end,
                    startexcl,
                    endexcl,
                    boost=1.0):
        from whoosh import query
        from whoosh.qparser.common import QueryParserError

        try:
            if start is not None:
                start = self.from_text(self.to_text(start))
            if end is not None:
                end = self.from_text(self.to_text(end))
        except Exception:
            e = sys.exc_info()[1]
            raise QueryParserError(e)

        return query.NumericRange(fieldname,
                                  start,
                                  end,
                                  startexcl,
                                  endexcl,
                                  boost=boost)
Ejemplo n.º 3
0
    def parse_range(self, fieldname, start, end, startexcl, endexcl,
                    boost=1.0):
        from whoosh import query
        from whoosh.qparser.common import QueryParserError

        if start is not None:
            if not self.is_valid(start):
                raise QueryParserError("Range start %r is not a valid number"
                                       % start)
            start = self.prepare_number(start)
        if end is not None:
            if not self.is_valid(end):
                raise QueryParserError("Range end %r is not a valid number"
                                       % end)
            end = self.prepare_number(end)
        return query.NumericRange(fieldname, start, end, startexcl, endexcl,
                                  boost=boost)
Ejemplo n.º 4
0
    def parse_query(self, fieldname, qstring, boost=1.0):
        from whoosh import query
        from whoosh.qparser.common import QueryParserError

        if qstring == "*":
            return query.Every(fieldname, boost=boost)

        if not self.is_valid(qstring):
            raise QueryParserError("%r is not a valid number" % qstring)

        token = self.to_bytes(qstring)
        return query.Term(fieldname, token, boost=boost)
Ejemplo n.º 5
0
 def __len__(self):
     raise QueryParserError("dummy parsing failure")