dpprx26000 2013-11-18 12:53
浏览 99
已采纳

在Elasticsearch和Symfony2中使用日期范围

I have a standard Datetime field in my Doctrine-based entity class:

/**
 * @ORM\Column(type="datetime")
 */
private $occurring;

This generates a DateTime object and works as expected. But a problem occurs when this object is integrated with the FOSElasticaBundle. Due to DateTime objects not supporting the __toString() method, I had to restructure my Elastica config using the properties so that the populate command will run:

mappings:
    id: ~
    occurring:
        properties:
            date: { type: date, format: "yyyy-MM-dd" }

This populates the date correctly but it loads in the default Elasticsearch format and ignores any custom formatting.

The problem is that my range queries based on this date field do not return the expected results. The following filter returns nothing even though there are items in Elasticsearch within this range.

$filteredQuery = new Filtered(
    $mainQuery,
    new Range('occurring', array(
        'gte' => '2013-11-18',
        'lte' => '2014-11-18'
    ))
);

The resulting query when run directly in Elasticsearch via curl returns the same incorrect results.

I did notice that changing the gte param to 2012 returned the expected results in the 2013 date range so I'm wondering if the incorrect date formatting is causing the filter to round up or something similar?

Any ideas? Thanks.

  • 写回答

1条回答 默认 最新

  • duannai1883 2013-11-21 12:52
    关注

    I figured this out with the help of this answer: Elasticsearch date range intersection

    To get date ranges working properly, you have to combine two filters rather than attempt to do it in one:

    $rangeLower = new Filtered(
        $mainQuery,
        new Range('occurring', array(
            'gte' => '2013-11-13'
        ))
    );
    
    $rangeUpper = new Filtered(
        $rangeLower,
        new Range('occurring', array(
            'lte' => '2014-11-14'
        ))
    );
    
    $query = new Query($rangeUpper);
    

    This gives the correct results although I'm sure there's a more elegant way of constructing the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!