Search code examples
searchfiltercollectionazure-ai-search

Using a DateTime filter in an Azure AI Search filter


I'm trying to filter on date/time string values that are from emails. The 'metadata_creation_date' email field is defined as an Edm.String. I need to do something like the following:

{ "search": "*", "filter": "metadata_creation_date ge '1-1-2000' and metadata_creation_date le '3-3-2024'", "select": "metadata_subject, metadata_creation_date" }

The filter above does text comparisons rather than DateTime comparisons. Are there filter macros that can be used? Do I have to do my own filtering in my code?


Solution

  • Usually, the eq and ne comparison operators are used on string data, but you are trying to compare the date time values in string type with ge and le which leads to wrong results.

    So, to avoid this and to filter on this date time values you need to create new field of type Edm.DateTimeOffset and search the results.

    For that you need to create custom web api skillset which takes metadata_creation_date as input and gives the output of type Edm.DateTimeOffset.

    Below is the sample output from web api. 2015-01-01T00:00:00.000Z.

    So, to create web api skillset follow this documentation.

    And query on the newly created field like below.

    { "search": "*", 
    "filter": "LastRenovationDate ge 2000-08-25T00:00:00Z and LastRenovationDate le 2015-08-25T00:00:00Z", 
    "select": "HotelName, LastRenovationDate" }