GraphQL Tag search using CONTAINS_NOT | Community
Skip to main content
Level 2
July 8, 2024
Solved

GraphQL Tag search using CONTAINS_NOT

  • July 8, 2024
  • 3 replies
  • 1420 views

Hi folks,

 

I have a (simplified) query for searching Content Fragments where sometimes I want to exclude certain tags:

 

query getHelpFaqSearch(
$value1: String!,
$operator1: StringOperator = CONTAINS) {
helpFaqList (
filter: {
_tags: {
_logOp: AND
_expressions: [
{
value: $value1,
_operator: $operator1
}
]
}
}
) {
items {
question
answer {
html
}
lastUpdated
tags
_path
}
}
}

However; if I use the VARs:
{
     "value1": "domain:digital/booking/make",
     "operator1": "CONTAINS_NOT"
}

 

I get back results like:

 

...

"tags": [
    "domain:digital/booking/make",
    "domain:digital/booking/connections",
    "domain:digital/travel/day-of-travel",
    "domain:digital/travel/get-ready-to-travel"
],

...

 

This entry doesn't return:

...

"tags": [
    "domain:digital/booking/make"
],
...

 

If on the second example I add another tag then that results returns also. So the result is getting hit because of the new tag.

 

Is there anyway of excluding a CF if a tag exists in both cases?

 

Thanks,

Graham

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by h_kataria

If I understood your requirement correctly then I guess your query is not working with CONTAINS_NOT when the content fragment has more than 1 tags attached to it. You can try using _apply attribute maybe like below and check if you get desired results. 

_expressions: [ { value: $value1, _operator: $operator1, _apply:ALL } ]




3 replies

TarunKumar
Community Advisor
Community Advisor
July 10, 2024

Hi @chebwin ,

Can you try to do it with filter attribute. 
I have provided the sample code:

filter: {tags: {elemMatch: {slug: {eq: "haka"}}}},

 

 

-Tarun

h_kataria
Community Advisor
h_katariaCommunity AdvisorAccepted solution
Community Advisor
July 10, 2024

If I understood your requirement correctly then I guess your query is not working with CONTAINS_NOT when the content fragment has more than 1 tags attached to it. You can try using _apply attribute maybe like below and check if you get desired results. 

_expressions: [ { value: $value1, _operator: $operator1, _apply:ALL } ]




chebwinAuthor
Level 2
July 10, 2024

This looks promising; of course I have another high profile bug to test first but will update this ticket later this week.

chebwinAuthor
Level 2
July 17, 2024

Apologies project work pulled me away. I had to modify it a bit so that you match AT_LEAST_ONCE for CONTAINS and ALL for CONTAINS_NOT:

query getHelpFaqSearch( $value1: String!, $operator1: StringOperator = CONTAINS, $apply1: ArrayMode = AT_LEAST_ONCE, $value2: String = "", $operator2: StringOperator = CONTAINS, $apply2: ArrayMode = AT_LEAST_ONCE) { helpFaqList ( filter: { tags: { _logOp: AND _expressions: [ { value: $value1, _operator: $operator1, _apply: $apply1 }, { value: $value2, _operator: $operator2, _apply: $apply2 } ] } } ) { items { question answer { html } lastUpdated tags _path } } } VARS { "value1": "domain:digital/contact-us/ba", "operator1": "CONTAINS", "value2": "domain:digital/booking/voluntary-change", "operator2": "CONTAINS_NOT", "apply2": "ALL" }
kautuk_sahni
Community Manager
Community Manager
July 16, 2024

@chebwin Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni