1

Hi I want to apply a filter to the strapi api with a combination of AND and OR but I can't seem to get it working.

Situation: I want to filter on a situation like this

(
  (tag OR tag2 OR tag3) AND 
  (
    (title CONTAINSI TEXT_FILTER_HERE) OR 
    (body CONTAINSI TEXT_FILTER_HERE) OR 
    (introduction CONTAINSI TEXT_FILTER_HERE)
  )
)

(note that I have abbreviated the tags as that should also be a containsi)

I have tried something like the link below and many others, but this example below will make all of them an OR situation, and I will find too many entries.

{{host}/api/blog?sort=publishedAt%3Adesc
&populate=Tags.tags&populate=Image
&pagination[page]=1
&pagination[pageSize]=25
&locale=en
&filters[$or][4][Tags][tags][Tag][$containsi]=TAG1
&filters[$or][6][Tags][tags][Tag][$containsi]=TAG2
&filters[$or][8][Tags][tags][Tag][$containsi]=TAG3
&filters[$or][3][Tags][tags][Tag][$containsi]=TAG4
&filters[$or][5][Tags][tags][Tag][$containsi]=TAG5
&filters[$or][7][Tags][tags][Tag][$containsi]=TAG6
&filters[$or][0][title][$containsi]=FILTER_TEXT_HERE
&filters[$or][2][body][$containsi]=FILTER_TEXT_HERE
&filters[$or][1][introduction][$containsi]=FILTER_TEXT_HERE

(made it multiline for readability)

Is it possible to this in strapi?

I can get it to work in an multiple OR situation with just one (1) AND, but not with ((or or) AND (or or)):

...
&filters[$or][4][Tags][tags][Tag][$containsi]={{tag1}}
&filters[$or][6][Tags][tags][Tag][$containsi]={{tag2}}
&filters[$or][8][Tags][tags][Tag][$containsi]={{tag3}}
&filters[$or][3][Tags][tags][Tag][$containsi]={{tag4}}
&filters[$or][5][Tags][tags][Tag][$containsi]={{tag5}}
&filters[$or][7][Tags][tags][Tag][$containsi]={{tag6}}
&filters[$and][0][title][$containsi]={{filter}}

or

...
&filters[$or][4][Tags][tags][Tag][$containsi]={{tag1}}
&filters[$or][6][Tags][tags][Tag][$containsi]={{tag2}}
&filters[$or][8][Tags][tags][Tag][$containsi]={{tag3}}
&filters[$or][3][Tags][tags][Tag][$containsi]={{tag4}}
&filters[$or][5][Tags][tags][Tag][$containsi]={{tag5}}
&filters[$or][7][Tags][tags][Tag][$containsi]={{tag6}}
&filters[title][$containsi]={{filter}}

Any help will be appreciated

1 Answer 1

3

I actually think I have found the answer. Could not find it anywhere in the documentation (https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/filtering-locale-publication.html#complex-filtering)

{{host}}/api/blog?sort=publishedAt%3Adesc
&populate=Tags.tags
&populate=Image
&pagination[page]=1
&pagination[pageSize]=25
&locale=en
&filters[$and][0][$or][0][title][$containsi]={{filter}}
&filters[$and][0][$or][1][introduction][$containsi]={{filter}}
&filters[$and][0][$or][2][body][$containsi]={{filter}}
&filters[$and][1][$or][0][Tags][tags][Tag][$containsi]={{tag1}}
&filters[$and][1][$or][1][Tags][tags][Tag][$containsi]={{tag2}}
&filters[$and][1][$or][2][Tags][tags][Tag][$containsi]={{tag3}}
&filters[$and][1][$or][3][Tags][tags][Tag][$containsi]={{tag4}}
&filters[$and][1][$or][4][Tags][tags][Tag][$containsi]={{tag5}}
&filters[$and][1][$or][5][Tags][tags][Tag][$containsi]={{tag6}}

Note the [NUMBER] sections grouping the ANDs and OR's ... Once you know it, it looks logical :-)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this ! It's crazy how this is not in the documentation...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.