Applying filters/refinements to an array of json

Hey guys, currently I’m trying to apply a filter to a field in an index, this field is an array populated with JSON objects.

The field looks like so:

dates: [
{startDate: 1520254800000,
endDate: 1520254800000
},
{startDate: 1523455200000,
endDate: 1523455200000
},
{startDate: 1532613600000,
endDate: 1532613600000
},
{startDate: 1537797600000,
endDate: 1537797600000
},
]

When applying the refinement:
(dates.startDate >= 1533736800000 AND dates.endDate <= 1533736800000)

The above example will be returned. I was wondering if it was possible to make the refinement apply to each element in the array such that it satisfies both rather than the entire field having 2 values inside any element that satisfy the refinement.

Sorry about the formatting,

Thanks

Hi Jake,

If I understand your problem correctly, you’re trying to retrieve records when both properties in a date object (single object containing a startDate and an endDate) satisfies your filter. Is that right?

If that’s what you’re trying to achieve, then I would recommend splitting your records into multiple ones.

Let’s take the example of a hotel room with several availability timespans. An index could look like this:

[
  {
    "room": "A",
    "startDate": 1520254800000,
    "endDate": 1520254800000
  },
  {
    "room": "A",
    "startDate": 1523455200000,
    "endDate": 1523455200000
  },
  {
    "room": "A",
    "startDate": 1532613600000,
    "endDate": 1532613600000
  },
  {
    "room": "B",
    "startDate": 1537797600000,
    "endDate": 1537797600000
  }
]

Then, you could use the following filter:

startDate >= 1533736800000 AND endDate <=1533736800000

And you would only retrieve the records where both startDate and endDate match the filter (in this case, the one for the room B).