Replica to sort "foreign" fields

Hi, guys. My first post here.
I started using Algolia some days ago with Laravel (via Scout). Worked beautifully, except for the sorting feature.

  1. The app I am working on shows a searchable grid with several columns showing information about visitors to a store.
  2. Each row can be expanded to see what the visitor bought or asked for.
  3. From the below JSON, the grid takes the following: id, dt, tm, origin.name, profile.name, user.name and comments. I also search for product.name.
  4. So far, everything works very well!
  5. Here is the thing: while I can create replicas to sort, for instance, the id, I could not find a way to create replicas that have, say, origin.sort_order (a numeric field) as a “sort-by-attribute” field.
    Is there a way to accomplish it?

For the record, here is how I set up the Visit to set the searchable tables/fields:

class Visit extends Model
{
    use Searchable;
    public function toSearchableArray()
    {
        $array = $this->toArray();
        $array['products'] = $this->products->toArray();
        $array['origin'] = $this->origin->toArray();
        $array['profile'] = $this->profile->toArray();
        $array['user'] = $this->user->toArray();
        return $array;
    }...

Resulting JSON:

{
  "hits": [
    {
      "id": 99,
      "unit": "UN",
      "dt": "2016-12-01",
      "dt_unix": 1480605371,
      "tm": "18:51:19",
      "tm_unix": null,
      "profile_id": 10,
      "origin_id": 6,
      "avg": "94.00",
      "max": "83.00",
      "min": "93.00",
      "prec": "0.26",
      "comment": "Quia ullam laborum consequuntur perferendis.",
      "user_id": 2,
      "created_at": "2017-11-19 18:50:08",
      "updated_at": "2017-11-19 18:50:08",
      "products": [
        {
          "id": 20,
          "name": "Ralph Lauren",
          "sort_order": 20,
          "created_at": "2017-10-07 09:30:00",
          "updated_at": "2017-11-19 18:41:56",
          "pivot": {
            "visit_id": 99,
            "product_id": 20,
            "qtd": 3,
            "amount": "264.00",
            "created_at": "2017-11-19 18:50:08",
            "updated_at": "2017-11-19 18:50:08"
          }
        },
        {
          "id": 15,
          "name": "Michael Kors",
          "sort_order": 15,
          "created_at": "2017-10-07 09:30:00",
          "updated_at": "2017-11-19 18:41:56",
          "pivot": {
            "visit_id": 99,
            "product_id": 15,
            "qtd": 6,
            "amount": "95.00",
            "created_at": "2017-11-19 18:50:08",
            "updated_at": "2017-11-19 18:50:08"
          }
        },
        {
          "id": 22,
          "name": "Reading Glasses",
          "sort_order": 22,
          "created_at": "2017-10-07 09:30:00",
          "updated_at": "2017-11-19 18:41:56",
          "pivot": {
            "visit_id": 99,
            "product_id": 22,
            "qtd": 2,
            "amount": "69.00",
            "created_at": "2017-11-19 18:50:08",
            "updated_at": "2017-11-19 18:50:08"
          }
        }
      ],
      "origin": {
        "id": 6,
        "name": "Puerto Rico",
        "sort_order": 8,
        "created_at": "2017-10-07 09:30:00",
        "updated_at": "2017-11-19 18:41:55"
      },
      "profile": {
        "id": 10,
        "name": "Lady Young",
        "sort_order": 10,
        "type_id": 3,
        "subtype_id": 4,
        "created_at": "2017-10-05 16:39:00",
        "updated_at": "2017-11-19 18:41:57"
      },
      "user": {
        "id": 2,
        "name": "User 2 (User)",
        "email": "user@example.org",
        "sort_order": 1,
        "created_at": "2017-11-02 16:20:52",
        "updated_at": "2017-11-19 18:41:58"
      },
      "objectID": "99",
      "_highlightResult": {
        "dt": {
          "value": "2016-12-01",
          "matchLevel": "none",
          "matchedWords": []
        },
        "comment": {
          "value": "Quia ullam laborum consequuntur perferendis.",
          "matchLevel": "none",
          "matchedWords": []
        },
        "products": [
          {
            "name": {
              "value": "Ralph Lauren",
              "matchLevel": "none",
              "matchedWords": []
            }
          },
          {
            "name": {
              "value": "Michael Kors",
              "matchLevel": "none",
              "matchedWords": []
            }
          },
          {
            "name": {
              "value": "Reading Glasses",
              "matchLevel": "none",
              "matchedWords": []
            }
          }
        ],
        origin": {
          "name": {
            "value": "Puerto Rico",**
            "matchLevel": "none",
            "matchedWords": []
          }
        },
        "profile": {
          "name": {
            "value": "Lady Young",
            "matchLevel": "none",
            "matchedWords": []
          }
        },
        "user": {
          "name": {
            "value": "User 2 (User)",
            "matchLevel": "none",
            "matchedWords": []
          },
          "email": {
            "value": "user@example.org",
            "matchLevel": "none",
            "matchedWords": []
          }
        }
      },
      "_rankingInfo": {
        "nbTypos": 0,
        "firstMatchedWord": 0,
        "proximityDistance": 0,
        "userScore": 446,
        "geoDistance": 0,
        "geoPrecision": 0,
        "nbExactWords": 0,
        "words": 0,
        "filters": 0
      }
    }, ...

Any help will be appreciated!

Hi Sergio!
Though “origin.sort_order” does may not appear in the drop-down menu of items that you can add as a “sort-by-attribute”, you should be able to manually type in ‘origin.sort_order’ and have it function as expected. I replicated two of your records in a test index and was able to have it work:

You can see that the two records are identical except that one has an origin.sort_order: 8, and one has an origin.sort_order: 10, and therefore he one with origin.sort_order: 8 is ranked below the one with 10. You can see that this is the explicit cause for the ranking by looking next to the little ranking trophy in the bottom of the record.

Please let me know if this is not what you were looking for or if it’s not working for you!

1 Like

Thanks, Maria!
I will try it tomorrow and will post the results here.