Indexing and parsing dates/times

索引日期

whoosh允许您使用 whoosh.fields.DATETIME 字段类型。而不是在 add_document() ,你用的是 Python datetime.datetime 对象:

from datetime import datetime, timedelta
from whoosh import fields, index

schema = fields.Schema(title=fields.TEXT, content=fields.TEXT,
                       date=fields.DATETIME)
ix = index.create_in("indexdir", schema)

w = ix.writer()
w.add_document(title="Document 1", content="Rendering images from the command line",
               date=datetime.utcnow())
w.add_document(title="Document 2", content="Creating shaders using a node network",
               date=datetime.utcnow() + timedelta(days=1))
w.commit()

正在分析日期查询

一旦你有了索引 DATETIME 字段,您可以使用包含在 whoosh.qparser.dateparse.DateParserPlugin ::

from whoosh import index
from whoosh.qparser import QueryParser
from whoosh.qparser.dateparse import DateParserPlugin

ix = index.open_dir("indexdir")

# Instatiate a query parser
qp = QueryParser("content", ix.schema)

# Add the DateParserPlugin to the parser
qp.add_plugin(DateParserPlugin())

DateParserPlugin , users can use date queries such as:

20050912
2005 sept 12th
june 23 1978
23 mar 2005
july 1985
sep 12
today
yesterday
tomorrow
now
next friday
last tuesday
5am
10:25:54
23:12
8 PM
4:46 am oct 31 2010
last tuesday to today
today to next friday
jan 2005 to feb 2008
-1 week to now
now to +2h
-1y6mo to +2 yrs 23d

通常,与其他包含空格的查询类型一样,用户需要使用 单引号引用包含空格的日期查询:

render date:'last tuesday' command
date:['last tuesday' to 'next friday']

如果您使用 free 论据 DateParserPlugin ,插件将尝试分析日期字段前缀后未加引号的文本中的日期::

qp.add_plugin(DateParserPlugin(free=True))

这允许用户键入日期查询,在日期字段名称和冒号后面加上空格和特殊字符。日期查询可以与不带引号的其他类型查询混合使用::

date:last tuesday
render date:oct 15th 2001 5:20am command

如果你不使用 DateParserPlugin ,用户仍然可以使用简单的数字形式搜索日期时间字段。 YYYY[MM[DD[hh[mm[ss]]]]] 它是内置于 DATETIME 领域:

from whoosh import index
from whoosh.qparser import QueryParser

ix = index.open_dir("indexdir")
qp = QueryParser("content", schema=ix.schema)

# Find all datetimes in 2005
q = qp.parse(u"date:2005")

# Find all datetimes on June 24, 2005
q = qp.parse(u"date:20050624")

# Find all datetimes from 1am-2am on June 24, 2005
q = qp.parse(u"date:2005062401")

# Find all datetimes from Jan 1, 2005 to June 2, 2010
q = qp.parse(u"date:[20050101 to 20100602]")

关于时区和基准时间

处理时区的最佳方法是始终索引 datetime 以本机UTC格式。任何 tzinfo 属性 datetime 对象是 ignored 通过索引器。如果使用的是本地日期时间,则应在索引前将其转换为本地UTC日期时间。

日期分析器注释

请注意,日期解析器仍然有点实验性。

设置基准日期时间

当你创建 DateParserPlugin 你可以通过 datetime 对象到 basedate argument to set the datetime against which relative queries (such as last tuesday-2 hours )被测量。默认情况下,基准日期为 datetime.utcnow() 此时插件被实例化::

qp.add_plugin(DateParserPlugin(basedate=my_datetime))

注册错误回调

为了避免用户查询在应用程序中引起异常,当日期分析器无法解析日期查询时,它会尝试以静默方式失败。但是,您可以注册一个回调函数来通知解析失败,这样您就可以向用户显示反馈。回调函数的参数是无法分析的日期文本(这是一个实验性功能,在将来的版本中可能会更改)::

errors = []
def add_error(msg):
    errors.append(msg)
qp.add_plugin(DateParserPlug(callback=add_error))

q = qp.parse(u"date:blarg")
# errors == [u"blarg"]

使用自由解析

free 选项对用户来说更容易,它可能导致模棱两可。例如,如果要在文档中查找包含对2005年3月和数字2的引用的文档,可以键入:

date:2005 march 2

在以下情况下,此查询将正确解释为日期查询和两个术语查询: free=False ,但作为单一日期查询 free=True . 在这种情况下,用户可以用单引号限制日期分析器的范围:

date:'2005' march 2

可分析格式

日期解析器支持广泛的日期和时间格式,但是我并不想尝试支持 所有 人类可读日期的类型(例如 ten to five the friday after next )最好的办法可能是选择一种有效的日期格式,并尝试对用户进行培训,如果他们使用其他同样有效的格式之一,那就认为这是一个愉快的意外。

局限性

  • 因为它是基于python的 datetime.datetime 对象 DATETIME Field共享该类别的所有限制,例如不支持无神论公历1年前的日期。这个 DATETIME 字段支持几乎不受限制的日期,因此如果 datetime 对象是它所能支持的每一个改进。另一种可能是添加对 mxDateTime 有一天会有物体。

  • 这个 DateParserPlugin 目前只支持英文日期。该体系结构支持为其他语言创建解析器,我希望很快为其他语言添加示例。

  • DATETIME 字段当前不支持开放范围。您可以通过使用远于过去或将来的端点来模拟开放式范围。