Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top