At least in the case of a solr backend translated entities are always rendered in the default language instead of the translation return by the index.
The issue is cause by missing meta data in the faked "search api table information" for the SQL flavored architecture of the views module.
I prepared a first (somewhat hackish) patch to fix the entity and field translation rendering of views rows generated via search_api.
With the patch applied you can create translation aware views including exposed language language filters.
To demonstrate the patch I created 3 articles: node 1 consists of an English and a Dutsch translation. Node 2 only exists in Dutch, node 3 only in English.






Original bug report:
I created two content Article in two languages EN(English) and NL(Dtuch). Indexed the same on to Solr. Everything is fine till this time.
Now When I am using views to show indexed data it is only showing English content even if language chosen in NL. But when I cross verified content at Solr everything is indexed.
Can anyone suggest where am I wrong or did I miss any configurations?
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | 2668898-28.patch | 9.98 KB | drunken monkey |
| #28 | 2668898-28--tests_only.patch | 5.9 KB | drunken monkey |
Comments
Comment #2
chera.jaswinder commentedComment #3
mkalkbrennerCan you export your search_api_* configs as well? this will speed up my debugging.
Comment #4
chera.jaswinder commentedAdded required files.
Comment #5
mkalkbrennerIt seems that the issue is not related to search_api_solr_multilingual itself.
The rendering of search results search_api_solr seems to be erroneous in search_api.
That needs some more debugging ...
Comment #6
chera.jaswinder commentedYes even to me it seems to be not related to search_api_solr_multilingual itself.
Yesterday while looking into views settings I was able to figure out, if we unchecked option "Use entity field rendering" appearing during field configuration, which says "If checked, Drupal's built-in field rendering mechanism will be used for rendering this field's values, which requires the entity to be loaded. If unchecked, a type-specific, entity-independent rendering mechanism will be used."
So after making this option unchecked data value started appearing from Solr as required. I would highly recommend for views to make it by default unchecked for now it appears as checked by default which should not happen.
Also uploaded the correct views configuration file.
Comment #7
mkalkbrennerI discovered the same behavior yesterday and discussed it in irc with @berdir.
But this is just a workaround because search_api's views plugin doesn't contain a specific renderer for every field type.
Think about a "translated" image field, having a different image for every translation of an article. I guess we want to have the right image displayed in the search result view.
I already continued my debugging and found the reason why "Use entity field rendering" doesn't display the right translation. I hope to have a patch later today ...
Comment #8
chera.jaswinder commentedGreat, thanks for all your help.
Comment #9
mkalkbrennerOK, I got it to work basically. All the trouble is caused by search_api and it's views integration itself.
Comment #12
mkalkbrennerComment #13
drunken monkeyThanks for creating this issue, Jaswinder, and thanks a lot for your detailed analysis, Markus!
However, I fear your current approach is much too hack-y to be committed to the module. It might work reasonably well in some scenarios, but that's of course not enough.
It would be great if you could find a way to fix this that won't require as many assumptions, and will fix this for as many scenarios (i.e., as generically) as possible.
My problems with the patch in detail:
As the @todo already explains, this is much too fragile to be committed.
If the entity field renderer needs it for translations to work, can we not just override the entity field renderer class we use? It seems insane, if we already know the language, to add a wrong entity type to the Views table just so Views can get it with its default code.
Seems like this should use dependency injection.
You can't do that, almost none of the item ID's structure is in any way guaranteed. This will work for our own entity datasources, yes, but will break in more or less interesting and/or horrible ways for all others.
Instead, you can get the entity type by calling the datasource's getEntityTypeId() method (which might return NULL). For the language code, it seems something like I suggested in #2641392: Review our language/translation support would be needed – then you could just call $item->getLanguage().
I might have time to look into this in the next weeks, if you don't have the time or get stuck. Just pretty busy at the moment clearing away my backlog of new and updated issues from two months of D8-only.
However, what would be very helpful in any case, is writing a failing test that demonstrates this problem (and could then be used to verify it is fixed).
Comment #14
mkalkbrennerComment #15
drunken monkeyThe attached solution seems to work fine, too, and is much cleaner.
The only remaining problem (though a major one) is this:
So we should probably get going with #2641392: Review our language/translation support and either postpone this issue or commit it with the hack-y workaround and then come back later to fix it.
Comment #16
drunken monkeyComment #17
drunken monkeyThe other issue was committed, so re-rolled the patch here and removed our hack-y workaround from it.
However, we should also definitely add a test for this, so this isn't ready to be committed. (Just setting to NR for the test bot.)
Comment #18
borisson_Back to NW for tests, I have no issues about the rest of the code.
Comment #19
mkalkbrennerIt seems that search_api_language always has to be part of the search result. I recently added it for search_api_solr_multilingual. But it's not part of search_api_solr's result. Looks like I have to move the code.
In general, are there any other fields that are expected to be part of the search result. Beside a test for this feature here, a test for the existence for all required search result fields is required.
Comment #20
drunken monkeyNo, that's not what's happening here.
search_api_languageis set on the Views result row in\Drupal\search_api\Plugin\views\query\SearchApiQuery::addResults(). It being present in the returned fields might even break this. (It's a reserved field ID, so I don't think it should ever be present.)Comment #21
mkalkbrennerIn search_api_solr_multilingual we require to have search_api_language to be part of the raw search result.
Do we have to remove it before it is turned into a search_api result?
Comment #22
mkalkbrennerDue to the refactoring that happened throughout the last month, this comment is now obsolete. So let's continue with Thomas' proposed solution.
Comment #23
drunken monkeyFinally managed to get the tests working, I hope.
Comment #25
mkalkbrennerUnder which circumstances will there be a result row without a search_api_language property?
Depending on the answer I wonder if
getCurrentLanguage(LanguageInterface::TYPE_CONTENT)won't be the better fallback.Comment #26
drunken monkeyNone, I guess, unless someone overrides the query plugin or manually adds result rows. So, this is just very defensive coding on my part, and the fallback shouldn't really matter. We can still change it, though, if you think that one's more appropriate. (If the current just gives you the site-wide default language, that does seem to be the case.)
Comment #27
borisson_This makes this array go over 80 cols, let's reflow it.
Otherwise this looks good, so RTBC from my pov.
Comment #28
drunken monkeyNeeded a re-roll, so I fixed both of these small issues. If the tests pass, I'll commit it.
Comment #30
borisson_Comment #32
drunken monkeyGreat, committed.
Thanks again, everyone!