nystudio107 / craft-similar

Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.
https://nystudio107.com/
MIT License
26 stars 5 forks source link

Order by number of relations in common #35

Closed johnj24 closed 3 years ago

johnj24 commented 3 years ago

Question

Is it expected behavior to have elements with the most relations in common be returned first? I've basically copied the tags and categories example from the documentation and I'm getting a seemingly random order.

Additional context

Here's a code example and what it returns:

{# Similar Recipes #}
{% set ids = entry.relatedStory.ids() | merge(entry.tags.ids()) %}
{% set limitCriteria = craft.entries.limit(8) %}
{% set similarRecipes = craft.similar.find({ element: entry, context: ids, criteria: limitCriteria }) %}

{% for similar in similarRecipes %}
  <div>{{ similar.title }} ({{ similar.count }} ids in common)</div>
{% endfor %}

{# Returns #}
Recipe Title 1 - (2 ids in common)
Recipe Title 2 - (2 ids in common)
Recipe Title 3 - (2 ids in common)
Recipe Title 4 - (2 ids in common)
Recipe Title 5 - (4 ids in common)
Recipe Title 6 - (4 ids in common)
Recipe Title 7 - (4 ids in common)
Recipe Title 8 - (2 ids in common)

Thanks!

khalwat commented 3 years ago

It should be explicitly ordering them by the number found, as per:

https://github.com/nystudio107/craft-similar/blob/v1/src/services/Similar.php#L192

...not sure if there was a regression or not though.

khalwat commented 3 years ago

Addressed in https://github.com/nystudio107/craft-similar/commit/8d694f8b8006ee6ca1dae733805f06c4b5594d2e