Home > Net >  Directus filtering with OR operator in the REST URL
Directus filtering with OR operator in the REST URL

Time:10-25

I have the following url

/items/bus_stops_bus_drivers?filter[bus_drivers_id][_eq]=2&filter[bus_stops_id][_eq]=3

I use & to connect two filters with an AND. What character should I use for OR operator?

CodePudding user response:

Generally speaking about REST and URI queries:

There is not standard query language, you need to invent your own and write a parser for it. Normally URI query is just an array of filters, in your case:

{
    filter: {
        bus_drivers_id: {_eq: 2},
        bus_stops_id: {_eq: 3}
    }
}

I suggest you to use the array structure rather than a custom query structure, because there is an already existing URI query parser. So instead of a or b just do something like or(a,b).

{
    filter: {
        _or: [
            {
                bus_drivers_id: {_eq: 2},
                bus_stops_id: {_eq: 3}
            },
            {
                bus_drivers_id: {_eq: 4},
            }
        ]
    }
}

Which translates to

/items/bus_stops_bus_drivers?
filter[_or][1][bus_drivers_id][_eq]=2
&filter[_or][1][bus_stops_id][_eq]=3
&filter[_or][2][bus_drivers_id][_eq]=4

Another option is using an already existing URI query language like RQL: https://github.com/persvr/rql Though in that case it is better to send plain JSON instead of struggling with this, something like

/items/bus_stops_bus_drivers?filter={URI-encoded-JSON}

Another option is using a standard like OData if you need something really complex: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31360954 Or GraphQL is a non-REST alternative.

The Directus case:

The case of Directus is interesting, they use logical AND by default if you give multiple parameters and they have a special way to give logical OR according to this github issue: https://github.com/directus/v8-archive/issues/1402 And currently it appears to contain a bug as well.

This is their example query:

{ENDPOINT}}/_/items/banner_item?
filter[content_max_age][0][null]
&filter[content_max_age][1][logical]=or
&filter[content_max_age][1][gte]=2020-02-19

If we translate this to JSON:

{
    filter: {
        content_max_age: [
            {_null: ""},
            {logical: "or", _gte: "2020-02-19"}
        ]
    }
}

While normally we would expect something like:

{
    filter: {
        content_max_age: {
            _or: [
                {_null: ""},
                {_gte: "2020-02-19"}
            ]
        }
    }
}

Which in theory should work too.

Directus operators are here, but they are poorly documented from this perspective, I mean I am not even sure how to use the _null based on this documentation, I just guessed it in the upper code: https://docs.directus.io/reference/filter-rules.html

I don't think you can send the JSON type filter in the URI query with the REST API, not sure though why it isn't supported. You need to use GraphQL and POST for that according to this article: https://learndirectus.com/how-to-filter-the-directus-api/ Though if you insist to REST, then the upper URI query solution should work.

  • Related