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?