【问题标题】:Sphinx Search How can I search above the certain date狮身人面像搜索如何在特定日期之上进行搜索
【发布时间】:2014-10-06 21:40:02
【问题描述】:

我有一个代码:

def set_date_range_filter(self,attribute = None,start_date = None , end_date = None):
    if attribute is None:
        return
    #Make sure set the passing start date and end date
    if not start_date or not end_date :
        return 
    if isinstance(start_date, str) :
        start_date = datetime.strptime(start_date, "%Y-%m-%d")
    if isinstance(start_date, unicode) :
        start_date = datetime.strptime(str(start_date), "%Y-%m-%d")
    if isinstance(end_date ,str):
        end_date = datetime.strptime(end_date, "%Y-%m-%d")
    if isinstance(end_date ,unicode):
        end_date = datetime.strptime(str(end_date), "%Y-%m-%d")

    # Shphnx Range Filter ,start_date and end_date must be integers that define the acceptable attribute values range

    start_date = int(time.mktime(start_date.timetuple()))
    end_date = int(time.mktime(end_date.timetuple()))
    if start_date > end_date :
        return 
    self.sphinx.SetFilterRange(str(attribute),start_date,end_date)

我想更新此代码以仅接受“start_date”或仅“end_date”或两者兼而有之。

就像我想要 2014-01-01 的所有日期,或者我想要 2014-01-01 之后的所有数据 或者说我想要从 2014-01-01 到 2014-09-01 的所有数据。我该如何存档??

【问题讨论】:

    标签: python search-engine sphinx


    【解决方案1】:

    而不是

    if not start_date or not end_date :
        return 
    

    替换为说

    if not start_date:
        start_date = '1971-01-01'
    if not end_date:
        end_date = '2037-01-01'
    

    或类似的。如果缺少任何一个,则只需使用非常早或非常晚的日期(超出您的数据范围)。以上示例日期在 unix 时间戳范围内选择。

    (然后会通过strptime变成合适的日期对象)

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多