When multiple aggregated fields are configured on the same data source, the values from the aggregated fields are overwritten by the value from the first aggregated field.
This became apparent to me when using the "Highlight" plugin on an aggregated field.
The issue is somewhat related to https://www.drupal.org/project/search_api/issues/2927748.
In the FieldsHelper class in the extractItemValues method, it is not possible to pass an array of required properties containing multiple aggregated fields because the "aggregated_field" key collides.
I took a stab at solving this. Patch is working for me, but I think a closer look at the extractItemValues method is needed, as well as maybe provide a way to get the propertyPath including the field identifier from a Field item. Perhaps building on the existing Utility where methods as 'createCombinedId' already exist.
If someone with better understanding on the matter could have a look and point me in the right (or wanted) direction, that would we great.
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 3110348-15--highlighter_multiple_versions_of_configurable_property.patch | 11.73 KB | drunken monkey |
Issue fork search_api-3110348
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
wtrv commentedComment #3
wtrv commentedComment #4
wtrv commentedNote: Tests are failing because at the moment fake field Id's are being used in the tests. The "extractItemValues" method is designed around extracting values by property path instead of field identifier. This is however not working properly as stated above in case of colliding property paths (eg. aggregated_field).
Tests can be fixed/adjusted once a decision has been made on how to tackle this issue in general.
Comment #5
wtrv commentedComment #6
wtrv commentedComment #7
drunken monkeyThanks a lot for reporting this problem and already providing a patch! My apologies that you had to wait so long for a response – didn’t manage to have much time for contrib in the last two months.
You are right, it makes sense that this is not only a problem for Views, but also for other places where we extract property values. (Though, at a glance, at least
FieldsHelper::extractItemValues()only seems to be used for highlighting and for aggregated fields in such a broad manner.)Your approach for solving this problem also seems sensible to me. As currently no code should be passing property paths containing
|characters toFieldsHelper::extractItemValues(), this should also not break anything. We should just clearly document this capability in the method’s documentation. (Also, we might even want to create a change record for this? Might be overkill, though, for such a detail, that also shouldn’t break anything.)However, when you already pass the field ID as part of the property path, you should just also use it in
FieldsHelper::extractItemValues()– patch revision attached. This attached revision also takes care to catch any exception thrown in the newly addedHighlightcode.In any case, as said, this looks like a sensible approach to me. However, more feedback from others would be very much appreciated. (Maybe I’ll add a hint to the next release notes …)
Also, tests for this new functionality are probably necessary before I can commit this – optimally both for
FieldsHelperdirectly and for the usage inHighlight. Would you be able to work on that? (Tests should already exist for both methods/classes, you’d just have to add appropriate new data sets or test methods, probably with a bit of additional setup.)Also, of course, please test/review my patch to see if it still works for you! (Sorry, no interdiff, as it was basically useless with all the whitespace changes.)
(Side note: By cirumventing the UI and just placing the appropriately suffixed property paths directly into the processor config via YAML (or similar), it seems this patch would even support selecting a specific field when aggregating configurable properties in aggregated fields.)
Comment #8
drunken monkey(Setting back to NW for the tests.)
Comment #9
drunken monkeyComment #10
malte.koelle commentedI have tested the #7 patch. And it might be me but the highlighting didn't work like it used to.
After a lot of debugging, it seems like if the property path does not contain the separator, the variable property_path will be null and the field_id gets the value of the propery_path.
list ($property_path, $field_id) = Utility::splitPropertyPath($property_path, TRUE, '|');I changed the arrangement of the property_path and the field_id, to prevent an empty property_path.
list ($field_id, $property_path) = Utility::splitPropertyPath($property_path, TRUE, '|');I'm not sure if this is the correct solution, but the highlighting works for me with this approach.
I have also fixed some tests in the HighlightTest class. They should now pass again, except the testFieldExtraction-Test. I was not able to fix it but I will have a look on it again.
I would appreciate any feedback and suggestions for improvement.
Best Regards
Comment #12
drunken monkeyThanks a lot for chiming in here and helping polish this patch!
You’re right that this line was buggy, thanks for finding that! I’m afraid, though, that your solution isn’t quite correct, either. I think I’ve got it now, though – please check the attached patch.
I also tried fixing the
HighlightTest::testFieldExtraction()test, but didn’t manage it completely. It seems it gets aNULLvalue somewhere while drilling into thebar:foonested property. Could you please debug further?Still, in any case, this will also need an additional test (or another property in
testFieldExtraction()) to make sure that the problem we’re trying to solve here is actually fixed. (Probably another method, though, to keep things clearly separated.testFieldExtraction()is complicated enough as it is.Comment #13
ejseguinte commentedI was able to get the patch to mostly apply to the current version of 8.x-1.35 and it seemed to have resolved the issue.
Comment #14
ejseguinte commentedAttaching the patch that I updated. There are some changes to the FieldHelper utility that didn't apply cleanly but it looks like everything is working. So I'm not sure if another issue might of incorporated some of those changes.
Comment #16
drunken monkeyI think this is the correctly rebased version of the patch.
Anyways, we use MRs for development now, so I created an MR containing that patch.
Still needs tests.
Comment #17
tuwebo commentedHello,
I am wondering whether it makes sense to add the item language for the missing fields. Otherwise there might be uses cases were the returned values correspond to the "original" translation.
For example (Solr scenario):
This:
Becomes that:
Since i am working using a custom patch combining #3031390: Excerpt shows all fields even when only one field has the searchterm and this issue's patch, I wonder if using $item->getLanguage() for missing fields can break some code that I am not aware of.
Comment #18
tuwebo commentedI've added the logger service in the setUpMockContainer() along with a method in the HighlightTest.php for creating the needed FieldItemDataDefinition.
So now we have this function createFieldItemDataDefinition for testFieldExtraction.php, which avoids the error:
I've added the missing field data definition for the 'entity:test2', avoiding the error:
This is the configuration for the test:
Creating the entity:test2, along with the the logger service, is needed and it will solve the error:
Which it is expected in the Highlight.php since we will have this configuration:
And then:
I have "rebased" the branch with the latest changes (it was around ~57 commits behind).
The only test failing is the one related to the issue #3536089: Replace deprecated REQUIREMENT_ERROR constant in search_api_db_defaults_requirements() for Drupal 11+ compatibility.
And my only concern is whether or not we should add the item language to when $missing fields (see my previous comment):
The issue is ready for review as soon as we decide whether or not to include the $item->getLanguage(), which I think we should to it.
Comment #19
drunken monkeyThanks a lot for your work on this, your changes look good to me.
Did you also test the MR on an actual site or did you just fix the tests? If you can confirm this works for you I think this would be RTBC.
However, in any case we have to wait for #3536089: Replace deprecated REQUIREMENT_ERROR constant in search_api_db_defaults_requirements() for Drupal 11+ compatibility (and possible #3523808: Fix failing pipelines?) to fix the pipelines.
Comment #20
drunken monkeyIf someone could confirm that the latest version of the MR works for them then I could merge this.
Comment #21
tuwebo commentedSorry about the delay,
I've tested it in an actual site and it worked for me.
Comment #22
drunken monkeyNo worries, thanks for reporting back. And good to hear it works for you, too.
Merged.
Thanks again, everyone!