Exemplo n.º 1
0
def test_datetime():
    dtf = fields.DATETIME(stored=True)
    schema = fields.Schema(id=fields.ID(stored=True), date=dtf)
    st = RamStorage()
    ix = st.create_index(schema)

    w = ix.writer()
    for month in xrange(1, 12):
        for day in xrange(1, 28):
            w.add_document(id=u("%s-%s") % (month, day),
                           date=datetime(2010, month, day, 14, 00, 00))
    w.commit()

    with ix.searcher() as s:
        qp = qparser.QueryParser("id", schema)

        r = s.search(qp.parse("date:20100523"))
        assert_equal(len(r), 1)
        assert_equal(r[0]["id"], "5-23")
        assert r[0]["date"].__class__ is datetime
        assert_equal(r[0]["date"].month, 5)
        assert_equal(r[0]["date"].day, 23)

        r = s.search(qp.parse("date:'2010 02'"))
        assert_equal(len(r), 27)

        q = qp.parse(u("date:[2010-05 to 2010-08]"))
        startdt = datetime(2010, 5, 1, 0, 0, 0, 0)
        enddt = datetime(2010, 8, 31, 23, 59, 59, 999999)
        assert q.__class__ is query.NumericRange
        assert_equal(q.start, times.datetime_to_long(startdt))
        assert_equal(q.end, times.datetime_to_long(enddt))
Exemplo n.º 2
0
def test_datetime():
    dtf = fields.DATETIME(stored=True)
    schema = fields.Schema(id=fields.ID(stored=True), date=dtf)
    st = RamStorage()
    ix = st.create_index(schema)
    
    w = ix.writer()
    for month in xrange(1, 12):
        for day in xrange(1, 28):
            w.add_document(id=u("%s-%s") % (month, day),
                           date=datetime(2010, month, day, 14, 00, 00))
    w.commit()

    with ix.searcher() as s:
        qp = qparser.QueryParser("id", schema)
        
        r = s.search(qp.parse("date:20100523"))
        assert_equal(len(r), 1)
        assert_equal(r[0]["id"], "5-23")
        assert r[0]["date"].__class__ is datetime
        assert_equal(r[0]["date"].month, 5)
        assert_equal(r[0]["date"].day, 23)
        
        r = s.search(qp.parse("date:'2010 02'"))
        assert_equal(len(r), 27)
        
        q = qp.parse(u("date:[2010-05 to 2010-08]"))
        startdt = datetime(2010, 5, 1, 0, 0, 0, 0)
        enddt = datetime(2010, 8, 31, 23, 59, 59, 999999)
        assert q.__class__ is query.NumericRange
        assert_equal(q.start, times.datetime_to_long(startdt))
        assert_equal(q.end, times.datetime_to_long(enddt))
Exemplo n.º 3
0
    def parse_query(self, fieldname, qstring, boost=1.0):
        from whoosh import query
        from whoosh.support.times import is_ambiguous

        at = self._parse_datestring(qstring)
        if is_ambiguous(at):
            startnum = datetime_to_long(at.floor())
            endnum = datetime_to_long(at.ceil())
            return query.NumericRange(fieldname, startnum, endnum)
        else:
            return query.Term(fieldname, self.to_text(at), boost=boost)
Exemplo n.º 4
0
Arquivo: fields.py Projeto: oier/Yaki
    def parse_query(self, fieldname, qstring, boost=1.0):
        from whoosh import query
        from whoosh.support.times import is_ambiguous

        at = self._parse_datestring(qstring)
        if is_ambiguous(at):
            startnum = datetime_to_long(at.floor())
            endnum = datetime_to_long(at.ceil())
            return query.NumericRange(fieldname, startnum, endnum)
        else:
            return query.Term(fieldname, self.to_text(at), boost=boost)
Exemplo n.º 5
0
 def __init__(self, fieldname, start, end, startexcl=False, endexcl=False,
              boost=1.0, constantscore=True):
     self.startdate = start
     self.enddate = end
     if start:
         start = datetime_to_long(start)
     if end:
         end = datetime_to_long(end)
     super(DateRange, self).__init__(fieldname, start, end,
                                     startexcl=startexcl, endexcl=endexcl,
                                     boost=boost,
                                     constantscore=constantscore)
Exemplo n.º 6
0
    def parse_range(self, fieldname, start, end, startexcl, endexcl, boost=1.0):
        from whoosh import query

        if start is None and end is None:
            return query.Every(fieldname, boost=boost)

        if start is not None:
            startdt = self._parse_datestring(start).floor()
            start = datetime_to_long(startdt)

        if end is not None:
            enddt = self._parse_datestring(end).ceil()
            end = datetime_to_long(enddt)

        return query.NumericRange(fieldname, start, end, boost=boost)
Exemplo n.º 7
0
    def to_text(self, x, shift=0):
        if isinstance(x, datetime.datetime):
            x = datetime_to_long(x)
        elif not isinstance(x, (int, long)):
            raise ValueError("DATETIME.to_text field doesn't know what to do " "with %r" % x)

        return super(DATETIME, self).to_text(x, shift=shift)
Exemplo n.º 8
0
Arquivo: fields.py Projeto: oier/Yaki
    def to_text(self, x, shift=0):
        if isinstance(x, datetime.datetime):
            x = datetime_to_long(x)
        elif not isinstance(x, (int, long)):
            raise ValueError("DATETIME.to_text field doesn't know what to do "
                             "with %r" % x)

        return super(DATETIME, self).to_text(x, shift=shift)
Exemplo n.º 9
0
 def __init__(self,
              fieldname,
              start,
              end,
              startexcl=False,
              endexcl=False,
              boost=1.0,
              constantscore=True):
     self.startdate = start
     self.enddate = end
     if start:
         start = datetime_to_long(start)
     if end:
         end = datetime_to_long(end)
     super(DateRange, self).__init__(fieldname,
                                     start,
                                     end,
                                     startexcl=startexcl,
                                     endexcl=endexcl,
                                     boost=boost,
                                     constantscore=constantscore)
Exemplo n.º 10
0
    def parse_range(self,
                    fieldname,
                    start,
                    end,
                    startexcl,
                    endexcl,
                    boost=1.0):
        from whoosh import query

        if start is None and end is None:
            return query.Every(fieldname, boost=boost)

        if start is not None:
            startdt = self._parse_datestring(start).floor()
            start = datetime_to_long(startdt)

        if end is not None:
            enddt = self._parse_datestring(end).ceil()
            end = datetime_to_long(enddt)

        return query.NumericRange(fieldname, start, end, boost=boost)
Exemplo n.º 11
0
    def to_text(self, x, shift=0):
        from whoosh.support.times import floor
        try:
            if isinstance(x, string_type):
                # For indexing, support same strings as for query parsing
                x = self._parse_datestring(x)
                x = floor(x)  # this makes most sense (unspecified = lowest)
            if isinstance(x, datetime.datetime):
                x = datetime_to_long(x)
            elif not isinstance(x, integer_types):
                raise TypeError()
        except Exception:
            raise ValueError("DATETIME.to_text can't convert from %r" % (x,))

        return super(DATETIME, self).to_text(x, shift=shift)
Exemplo n.º 12
0
    def to_text(self, x, shift=0):
        from whoosh.support.times import floor
        try:
            if isinstance(x, string_type):
                # For indexing, support same strings as for query parsing
                x = self._parse_datestring(x)
                x = floor(x)  # this makes most sense (unspecified = lowest)
            if isinstance(x, datetime.datetime):
                x = datetime_to_long(x)
            elif not isinstance(x, integer_types):
                raise TypeError()
        except Exception:
            raise ValueError("DATETIME.to_text can't convert from %r" % (x, ))

        return super(DATETIME, self).to_text(x, shift=shift)