Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No DISTINCT result (with Facet + Order) #15

Open
jhlab opened this issue Oct 30, 2017 · 4 comments
Open

No DISTINCT result (with Facet + Order) #15

jhlab opened this issue Oct 30, 2017 · 4 comments

Comments

@jhlab
Copy link

jhlab commented Oct 30, 2017

Hi,
I´m trying to use the Forty Facets gem in a ruby on rails application, but testing it I have the following issue:

In a many to many relation, for example, a person does many tasks, and a task is done by many persons, when I select a task (facet) and I also apply order by the person name, the resulting select statement doesn't have a DISTINCT clause, so the query result has the same person repeated many times ( the number of tasks he works on).

Incorrect (without DISTINCT):
Peter -> Task 1, Task 2, Task 3
Peter -> Task 1, Task 2, Task 3
Peter -> Task 1, Task 2, Task 3
Mary -> Task 1, Task 2, Task 3
Mary -> Task 1, Task 2, Task 3
Mary -> Task 1, Task 2, Task 3

Correct:
Peter -> Task 1, Task 2, Task 3
Mary -> Task 1, Task 2, Task 3

I would like to know how can I fix this issue.

Thanks!

@axelerator
Copy link
Collaborator

Hi,

yeah the many-to-many case is tricky and I fear not completely correct yet :-(
Unfortunately I don't have time to look into right now.

I welcome any pull requests though. You can fork the repo and have a go at it - the library actually is not that complicated.

There are some test and my fixtures actually already contain has_and_belongs_to many - so maybe you can try to reproduce the problem with a test?

As a workaround in your app: Have you tries applying distinct to the search result? search.result.distinct ?

@jhlab
Copy link
Author

jhlab commented Nov 2, 2017

Hi,
Thanks for your answer!

I've tried the workaround suggested within the controller file:

def index
@search = PersonSearch.new(params)
@persons = @search.result.distinct.paginate(page: params[:page])
end

This is working fine for the query result, but not for the facets result, because I am only using distinct for @persons and not for @search.

I have an error when usinsg distinct for @search.
How can I use distinct for the @search ??

Thanks!

@axelerator
Copy link
Collaborator

You can't use it on @search since it's not an active_record object.
It is normal for the facets-counts not to sum up with the result. A person can be accounted twice or more to different facet values due to the has_many relation.

@jhlab
Copy link
Author

jhlab commented Nov 2, 2017

In another example, I have many buildings as a result and I have a facet regarding architects and another regarding types of buildings.

When I select an architect that has worked only with one of those buildings, because in that building has worked also another architect, the result is one building (now with the result.distinct), but in the facet of type of buildings I still have house (2) because that building have a relation with 2 architects.

Building 1 -> architect 1, architect 2

Incorrect Result:
Facet: House (2) (Distinct missing)
Query: One building info (Distinct applied)

Correct Result should be:
Facet: House (1)
Query: One building info

Is there any way to solve this issue?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants