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.

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码