Under some circumstances the datasource ID is not set in \Drupal\search_api\Plugin\search_api\processor\Highlight::getFulltextFields. We are using Search API Solr, and are passing the Rendered Item to the index. Even though the server and index are both set up for excerpt and highlight, the $fulltext_fields has an empty key ('') for the rendered item.
I know you need more debugging information, also I am not confident if this is a bug against Search API or Search API Solr, but I'll leave the followup to others who also encounter the issue as right now I don't have time to dig into it further. Attaching a workaround patch which allowed us to use search excerpt with Search API Pages (Views still returns empty results when set to display "search results" instead of rendered entities).
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 2703915-5--highlighting_of_datasource_independent_fields.patch | 1.18 KB | drunken monkey |
Comments
Comment #2
snufkin commentedPatch overrides the fulltext fields array with the values from the entry where the key is not set correctly. This is a hacky workaround and does not solve the underlying issue, and will need more work to debug the root cause. It is here for reference only.
Comment #3
snufkin commentedWe think this is due to the way search api solr implements the highlight plugin than search api, so reassigning to that project.
Comment #4
alxbridge commentedThe patch in #2 works for me, as long as I have another fulltext field defined besides the Rendered Item.
NB I've also encountered this problem with Database search, so I think this belongs to Search API (or possibly both, if it's a problem with the highlight plugin implementation in both).
Comment #5
drunken monkeyThanks for reporting this issue.
I think the problem here is that the code doesn't take datasource-independent fields into account, although they can apply to items of all datasources. Of course, at least in the current architecture, datasource-independent fields cannot be extracted from items, so this will currently only cause any issue for backends like Solr, which (sometimes) return field data.
The attached patch should fix this problem cleanly. Please test
However, for Solr, you can also just use Solr's built-in highlighting support and just disable the "Highlight" processor of the Search API.
Comment #6
snufkin commentedRolling the patch against alpha14, because against -dev our build is failing.
Comment #9
drunken monkeyComment #10
rupertj commentedSnufkin's version of the patch against alpha14 works well for me and fixes the issue. (-dev is currently broken for me too, so I couldn't test that).
Comment #11
drunken monkeyThanks for testing!
However, if you didn't test the version that will actually be committed, please don't mark the issue as RTBC. I need verification of the actual patch – especially with the many major changes the module is currently going through.
Comment #12
drunken monkeyThis has become out-dated now due to #2575003: Make adding new properties/fields more a first-class operation of processors, which does allow the extraction of processor-provided fields (though we should probably try to make this process a bit smoother).
Also, I just realize that it seems this patch won't add the datasource-independent fields to datasources without fulltext fields after all.
So, probably we need more or less a complete rewrite here.
Comment #13
drunken monkeyWill be fixed in #2305021: Test field extraction in processors, I hope.
Please test the patch posted there and see if it really solves your issue!
Comment #14
rgpublicRe #13: I just tried out the new version with the #2305021 fix and it indeed resolves the problem (8.x-1.0-alpha16). For everbody who tries to upgrade:
1) You need to uninstall the search_api module first before reinstalling. Upgrading from alpha15 doesnt work due to some configuration issues. Consequently, you need to recreate you search api server/index.
2) alterPropertyDefinitions has been replaced with getPropertyDefinitions. So if you are defining "virtual" fields to be indexed you need to do it there. See the changes to AddURL class in https://www.drupal.org/files/issues/2575003-24--processor_properties.patch to get an idea of how it's working: It's nearly the same as before, but you need to create instances of ProcessorProperty to define your stuff. You only need to return sth. when $datasource (the only argument of the function) is NOT set. And, finally, IMPORTANT: You need to set "add_properties" = 0 in the stages part of the annotation (comment block at the beginning of your processor class). If you omit that, your shiny new function isnt even called. Took me a while to figure all this out. Hope it helps anyone.
After you do all this, this problem vanishes and you will indeed see the excerpt with your virtual fields included and properly highlighted.