在Strapi(版本4)过滤中复杂的AND和OR组合。

huangapple go评论84阅读模式
英文:

Complex combination of AND and OR in strapi (version 4) filtering

问题

抱歉,我只能为你提供代码的翻译,以下是你提供的代码的翻译:

你好,我想对strapi api应用一个包含AND和OR组合的过滤器,但似乎无法使其工作。

情况:
我想对如下情况进行过滤

(
(tag OR tag2 OR tag3) AND
(
(title 包含 TEXT_FILTER_HERE) OR
(body 包含 TEXT_FILTER_HERE) OR
(introduction 包含 TEXT_FILTER_HERE)
)
)

(请注意,我已经缩写了标签,这也应该是包含i)

我尝试过类似下面链接的东西以及许多其他方法,但是下面的示例会使它们都变成OR情况,并且我会找到太多条目。

{{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

(为了可读性而将其拆分成多行)

在strapi中是否可能实现这个?

我可以使它在多个OR情况下只有一个(1)AND的情况下工作,但不能使用((或 或) AND (或 或)):

...
&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}}

...
&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}}


任何帮助将不胜感激
英文:

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

得分: 1

我实际上认为我已经找到了答案。在文档中找不到它的任何地方(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}}

注意 [NUMBER] 部分分组了 AND 和 OR 的条件...
一旦你了解了它,它看起来很合理 在Strapi(版本4)过滤中复杂的AND和OR组合。

英文:

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 在Strapi(版本4)过滤中复杂的AND和OR组合。

huangapple
  • 本文由 发表于 2023年1月9日 19:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定