Make rating average indexable on Search API

Created on 16 September 2022, over 2 years ago
Updated 3 October 2023, over 1 year ago

Problem/Motivation

I tried to index a field rating to a Solr Search API index, but the field only contains the last vote.
This issue Integrate with Entity API (1256572) β†’ allows to shows the average in a normal view, not on a indexed one.

Field rating value:

mysql> select * from node__field_rating where entity_id = 231976;
+--------+---------+-----------+-------------+----------+-------+---------------------+---------------------+
| bundle | deleted | entity_id | revision_id | langcode | delta | field_rating_rating | field_rating_target |
+--------+---------+-----------+-------------+----------+-------+---------------------+---------------------+
| recipe |       0 |    231976 |      889441 | en       |     0 |                  60 |                NULL |
+--------+---------+-----------+-------------+----------+-------+---------------------+---------------------+
1 row in set (0.00 sec)

All votes:

mysql> select * from votingapi_vote where entity_id = 231976;
+------+------+--------------------------------------+-------------+-----------+-------+------------+---------+------------+------------------------------------------------------------------+
| id   | type | uuid                                 | entity_type | entity_id | value | value_type | user_id | timestamp  | vote_source                                                      |
+------+------+--------------------------------------+-------------+-----------+-------+------------+---------+------------+------------------------------------------------------------------+
|   81 | vote | 5e92ddac-a5fc-4116-8593-b79b455d88de | node        |    231976 |    80 | percent    | 1102096 | 1643891124 | 1ca26437f10b63d9dda54e4817eec1149d8abc624db9662017fd0b155a400812 |
| 1306 | vote | abf94994-aafd-4e5f-82b1-818c3e9b998f | node        |    231976 |   100 | percent    |   65391 | 1651589373 | 06da468d76d04615412313570310f5b372728bf235e5ff498852460ecac12057 |
| 2161 | vote | 9bdd8f0c-206e-4c5a-8b52-a18709ba989a | node        |    231976 |    60 | percent    |       1 | 1663307997 | 687d9fcb1da0f889d325c71802c50afd629fd2da7845f53903cc9bc43359969d |
+------+------+--------------------------------------+-------------+-----------+-------+------------+---------+------------+------------------------------------------------------------------+
3 rows in set (0.00 sec)

The average (the value that should be indexed):

mysql> select * from votingapi_result where entity_id = 231976;
+------+------+-------------+-----------+-------+------------+--------------+------------+
| id   | type | entity_type | entity_id | value | value_type | function     | timestamp  |
+------+------+-------------+-----------+-------+------------+--------------+------------+
| 6661 | vote | node        |    231976 |    80 | percent    | vote_average | 1663307997 |
| 6666 | vote | node        |    231976 |     3 | percent    | vote_count   | 1663307997 |
| 6671 | vote | node        |    231976 |   240 | percent    | vote_sum     | 1663307997 |
+------+------+-------------+-----------+-------+------------+--------------+------------+
3 rows in set (0.00 sec)

Steps to reproduce

  • Add a rating field to a content type
  • Try to index the field on a search api index
  • Show the rating field on a indexed view
  • Vote with multiples users
  • It will index the last vote, not the average

Proposed resolution

Add a processor for rating fields, to obtain the desired result instead of the last vote.

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain eduardo morales alberti Spain, πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024