Score:0

JsonAPI does not work with "and" and "or" group

hn flag

A website search accesses http://api.example.com/de/jsonapi/node/product. The current fetch request looks like this.

http://api.example.com/de/jsonapi/node/product
?filter[or-group][group][conjunction]=OR
&filter[title-filter][condition][path]=title
&filter[title-filter][condition][operator]=CONTAINS
&filter[title-filter][condition][value]=${query}
&filter[title-filter][condition][memberOf]=or-group
&filter[sku-filter][condition][path]=field_sku
&filter[sku-filter][condition][operator]=CONTAINS
&filter[sku-filter][condition][value]=${query}
&filter[sku-filter][condition][memberOf]=or-group
&filter[desc-filter][condition][path]=field_description.value
&filter[desc-filter][condition][operator]=CONTAINS
&filter[desc-filter][condition][value]=${query}
&filter[desc-filter][condition][memberOf]=or-group
&include=field_images
&fields[file--file]=uri,url

${query} is searched in the title, the SKU field, and the description.

If searched for "pfeffer", the displayed products are correct and look like this.

{
  "products": [
    {
      "id": "213431-12342235-32525",
      "type": "product",
      "title": "Pfeffer schwarz, ganz",
      "image": "https://static.example.com/files/images/imagename.png",
      "text": "Lorem ipsum",
      "url": "/produkte/gewuerze/schwarzer-pfeffer-ganz-lose"
    },
    {
      "id": "213431-12342235-32525",
      "type": "product",
      "title": "Pfefferminze",
      "image": "https://static.example.com/files/images/imagename.png",
      "text": "Lorem ipsum",
      "url": "/produkte/tee/pfefferminze"
    },
    {
      "id": "213431-12342235-32525",
      "type": "product",
      "title": "Cayennepfeffer, gemahlen",
      "image": "https://static.example.com/files/images/imagename.png",
      "text": "Lorem ipsum",
      "url": "/produkte/gewuerze/cayennepfeffer-gemahlen"
    }
  ]
}

If searched for "pepper", the displayed products are exactly the same like the above. BUT they do NOT contain this query in the DE language variant, but they do in the EN variant.

The expected results for "pepper" would be

{
  "products": []
}

Several tests have shown that it seems to search in all languages in the JsonAPI, although it is located under '/de/jsonapi/' and only language specific pentands are returned, even if the hit is in a different language.

Adding the field "langcode" for filtering, to get only language specific results, is the solution. An example (without OR group) works correctly -> no products are found.

http://api.example.com/de/jsonapi/node/product
?filter[langcode-filter][condition][path]=langcode
&filter[langcode-filter][condition][value]=de
&filter[title-filter][condition][path]=title
&filter[title-filter][condition][operator]=CONTAINS
&filter[title-filter][condition][value]=pepper
&include=field_images
&fields[file--file]=uri,url

As described in the documentation under #6 Grouping grouped filters the solution should be built using the following logic.

WHERE (langcode = en) AND (title CONTAINS ${query} OR field_sku CONTAINS ${query} OR field_description.value CONTAINS ${query})

Analogous to the documentation thus as follows:

// Create an AND and an OR GROUP
filter[and-group][group][conjunction]=AND
filter[or-group][group][conjunction]=OR

// Put the OR group into the AND GROUP
filter[or-group][group][memberOf]=and-group

// Create the langcode filter and put it in the AND GROUP
filter[langcode-filter][condition][path]=langcode
filter[langcode-filter][condition][value]=de
filter[langcode-filter][condition][memberOf]=and-group

// Create the title filter and put it in the OR GROUP
filter[title-filter][condition][path]=title
filter[title-filter][condition][operator]=CONTAINS
filter[title-filter][condition][value]=${query}
filter[title-filter][condition][memberOf]=or-group

// Create the sku filter and put it in the OR GROUP
filter[sku-filter][condition][path]=field_sku
filter[sku-filter][condition][operator]=CONTAINS
filter[sku-filter][condition][value]=${query}
filter[sku-filter][condition][memberOf]=or-group

// Create the description filter and put it in the OR GROUP
filter[desc-filter][condition][path]=field_description.value
filter[desc-filter][condition][operator]=CONTAINS
filter[desc-filter][condition][value]=${query}
filter[desc-filter][condition][memberOf]=or-group

// Adding the rest of the filter without group
include=field_images
fields[file--file]=uri,url

Putting it all together, it looks like this.

http://api.example.com/de/jsonapi/node/product
?filter[and-group][group][conjunction]=AND
&filter[or-group][group][conjunction]=OR
&filter[or-group][group][memberOf]=and-group
&filter[langcode-filter][condition][path]=langcode
&filter[langcode-filter][condition][value]=de
&filter[langcode-filter][condition][memberOf]=and-group
&filter[title-filter][condition][path]=title
&filter[title-filter][condition][operator]=CONTAINS
&filter[title-filter][condition][value]=${query}
&filter[title-filter][condition][memberOf]=or-group
&filter[sku-filter][condition][path]=field_sku
&filter[sku-filter][condition][operator]=CONTAINS
&filter[sku-filter][condition][value]=${query}
&filter[sku-filter][condition][memberOf]=or-group
&filter[desc-filter][condition][path]=field_description.value
&filter[desc-filter][condition][operator]=CONTAINS
&filter[desc-filter][condition][value]=${query}
&filter[desc-filter][condition][memberOf]=or-group
&include=field_images
&fields[file--file]=uri,url

So still wrong results are output! German results are shown even if searched for englisch terms, like in the given example above.

What am I doing wrong?

cn flag
"So still wrong results are output!" --> What is wrong about the results you are getting? You show the query but you don't describe how the results are incorrect.
jaykobo avatar
hn flag
@PatrickKenny Thats right, I edited my question and added the current response and expected results. Hope that clear things up.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.