Search code examples
azure-cognitive-search

Can I use filters and facets based on the year of an Edm.DateTimeOffset field


I would like to have filtering and faceting based on the year of a DateTimeOffset field called Published.

Something like this:

$filter=year(Published) eq 2019&facet=year(Published)

I believe that I could create the filter using something like

$filter=Published ge 2019-01-01T00:00:00Z ..

This does not provide me with a solution for faceting and it feels messy to use an entire datetime when I'm only interested in the year.


Solution

  • Sorry for the late response. I will go ahead and post an answer for others who might have a similar question.

    DateTimeOffset fields store normalized UTC values as milliseconds. Currently there is no way in the APIs to use components of datetime directly in an filter expression.

    However in this case you don't need such a mechanism as you can just extend your filter expression to use the interval parameter of facet in the API to achieve desired faceting. The expression

    $filter=Published ge 2019-01-01T00:00:00Z&facet=Published,interval:year

    will return all documents published on/after 2019/01/01 and facets for every year.

    Please check the docs for more facet properties that might come handy in dealing with datetime fields.