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

element parameter is redundant in certain scenarios #9

Closed joshuabaker closed 3 years ago

joshuabaker commented 6 years ago

I have a scenario where providing a the element is redundant. I just want to get the best matches for the provided context.

The only requirement for this, at present, is to determine the element type and remove the element from the result set. The former can be achieved looking at the criteria and the latter isn’t always required.

Leaving here for myself, in case I find time to put together a PR. Let me know your thoughts.

khalwat commented 5 years ago

Can you give me a code example of what you mean?

joshuabaker commented 5 years ago

Essentially, what I’m talking about is searching.

Below is an example scenario where we’re looking for people that have worked at a specific company and we want to order by matches with some skills.

{% set element = entry %}{# The homepage, perhaps (i.e. redundant for this query) #}

{% set context = craft.categories({
  relatedTo: skillIds,
}) %}

{% set criteria = craft.entries({
  section: 'people',
  relatedTo: companyIds,
  limit: 3,
}) %}

{% set results = craft.similar.find({
  element: element,
  context: context,
  criteria: criteria,
}) %}

The above works, however, when we’re on a page where there’s no element object we have to add a query to get a random element.

khalwat commented 5 years ago

So the element is required because that's what it uses to create the Element Query. I suppose it could fall back to a default generic ElementQuery or EntryQuery or such, but I'm wondering if having to be explicit/consistent about it makes sense regardless?

joshuabaker commented 5 years ago

Is the criteria element type not sufficient for this though?

khalwat commented 3 years ago

I think it's probably best to keep the API consistent in the end.

joshuabaker commented 3 years ago

giphy