V8-archive: And or operator causes other filters to break

Created on 10 Sep 2019  路  16Comments  路  Source: directus/v8-archive

馃悶 bug report

Description

I am trying to do some conditional filtering using the and & or operators. Unfortunately after adding one of these operator causes directus to break other requirements / filters. Below I will attach a code + result json.

馃敩 Minimal Reproduction

The below code is parsing the correct data from directus with the correct category

  return client
      .getItems('privileges', {
        fields: ['*', 'image.filename', 'image.data.full_url', 'attachment.*', 'partner.*.*', 'location.*', 'category.*.*', 'type.icon.data.full_url'],
        meta: ['total_count', 'result_count'],
        sort: 'created_on',
        filter: {
          category: {
            'logical': 'and',
            'eq': category
          },
          available_from: {
            'lte': 'now'
          },
          available_until: {
            'gte': 'now'
          }
        },
        limit,
        offset,
      }).then(response => {
        dispatch(getPrivilegesSuccess(key, response))
      }).catch(error => {
        console.log(error);
        dispatch(getPrivilegesFail(key, error))
      });

in contrary the below code response result from directus isn't correct anymore. It is parsing categories with different id's. (also with hardcoding the category id overhere) However the location.city and location.title filter do work as expected. The category filter does not unfortunately.

  return client
      .getItems('privileges', {
        fields: ['*', 'image.filename', 'image.data.full_url', 'attachment.*', 'partner.*.*', 'location.*', 'category.*.*', 'type.icon.data.full_url'],
        meta: ['total_count', 'result_count'],
        sort: 'created_on',
        filter: {
          'location.city': {
            'contains': q
          },
          'location.title': {
            'logical': 'or',
            'contains': q
          },
          category: {
            'logical': 'and',
            'eq': category
          },
          available_from: {
            'lte': 'now'
          },
          available_until: {
            'gte': 'now'
          }
        },
        limit,
        offset,
      }).then(response => {
        dispatch(getPrivilegesSuccess(key, response))
      }).catch(error => {
        console.log(error);
        dispatch(getPrivilegesFail(key, error))
      });

馃敟 Exception or Error

No exception or error, just wrong data being returned by directus api. (data with different category.id

馃實 Your Environment

"@directus/sdk-js": "^5.3.0",

Any help is much appreciated. Already asked it in the slack group but they forwarded me to create an issue for it.

bug

Most helpful comment

If this is the correct param syntax and the API does not return the correct data, then this must be a API issue

All 16 comments

@benhaynes Hi Ben, is this in the pipeline for next release?

Our team has been busy with client work the past few weeks, so I don't think this is resolved. Therefore it probably won't be in the next release... but hopefully the one after.

That's okay ben. Could you just verify it's actually a bug in the core of Directus? We are currently relying on this and can't move forward. Are you guys working in sprints / releases every 2 weeks? :)

To verify that I'll need to have the devs dive-in... which might take a little time. I wish we had more resourcing, but we're slammed with projects that "pay the bills". :)

Normally we release every 2 weeks, but our lack of commits has pushed this back recently. If you debug the issue and find a solution, send it over in a PR and we'll review/merge it for the next release!

Hey @kevinvugts! Can you confirm if the same query works correctly when using the API directly? I'm wondering if this is an issue with the SDK, or an issue in the API itself.

Groetjes!

@rijkvanzanten @benhaynes It looks like it comes from the SDK. When I fire of the same query it will return a result directly from the api with a ~curl command.

The query / API Call

https://api.xclsv500.nl//_/items/privileges?fields%5B0%5D=%2A&fields%5B1%5D=image.filename&fields%5B2%5D=image.data.full_url&fields%5B3%5D=attachment.%2A&fields%5B4%5D=partner.%2A.%2A&fields%5B5%5D=location.%2A&fields%5B6%5D=category.%2A.%2A&fields%5B7%5D=type.icon.data.full_url&meta%5B0%5D=total_count&meta%5B1%5D=result_count&sort=sort&filter%5Blocation.city%5D%5Bcontains%5D=6%20gangen&filter%5Blocation.title%5D%5Blogical%5D=or&filter%5Blocation.title%5D%5Bcontains%5D=6%20gangen&filter%5Bavailable_from%5D%5Blte%5D=now&filter%5Bavailable_until%5D%5Bgte%5D=now&limit=10&offset=10

Is this the API call that the SDK makes, or the correct one you're looking for?


Formatted:

https://api.xclsv500.nl/_/items/privileges
  ?fields[0]=*
  &fields[1]=image.filename
  &fields[2]=image.data.full_url
  &fields[3]=attachment.*
  &fields[4]=partner.*.*
  &fields[5]=location.*
  &fields[6]=category.*.*
  &fields[7]=type.icon.data.full_url
  &meta[0]=total_count
  &meta[1]=result_count
  &sort=sort
  &filter[location.city][contains]=6 gangen
  &filter[location.title][logical]=or
  &filter[location.title][contains]=6 gangen
  &filter[available_from][lte]=now
  &filter[available_until][gte]=now
  &limit=10
  &offset=10

This is generated by the JS SDK but the result set of this is not being received in the SDK but with a curl in terminal.

@bjgajjar Does this look right to you?

?filter[location.city][contains]=value
&filter[location.title][logical]=or
&filter[location.title][contains]=value

Does this look right to you?

Yes

If this is the correct param syntax and the API does not return the correct data, then this must be a API issue

filter: {
          'location.city': {
            'contains': q
          },
          'location.title': {
            'logical': 'or',
            'contains': q
          },
          category: {
            'logical': 'and',
            'eq': category
          },
          available_from: {
            'lte': 'now'
          },
          available_until: {
            'gte': 'now'
          }
        },

@rijkvanzanten - On a quick overview, It seems it's related to https://github.com/directus/api/issues/958

I agree with @bjgajjar, I think this issue is related to the grouping of AND and OR conditions and it seems to be related to #958, #1067

@bjgajjar Whats's the outcome?

@kevinvugts - As Rijk mentioned in the https://github.com/directus/api/issues/1067#issuecomment-510546930 - It's not a bug but a feature that doesn't exist yet. Feel free to open a feature request to discuss possible ways to implement a grouped filter structure.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jwkellyiii picture jwkellyiii  路  3Comments

ondronix picture ondronix  路  3Comments

cdwmhcc picture cdwmhcc  路  3Comments

andgar2010 picture andgar2010  路  3Comments

rijkvanzanten picture rijkvanzanten  路  3Comments