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

Filters defined in query are missing from request body #672

Open
jiajiawang opened this issue Mar 29, 2019 · 8 comments
Open

Filters defined in query are missing from request body #672

jiajiawang opened this issue Mar 29, 2019 · 8 comments
Labels

Comments

@jiajiawang
Copy link
Contributor

For queries like

UsersIndex.query(bool: { filter: [{ term: {"country": "US"}}]}).filter(term: { "confirmed": true })

I'm expecting the request body would be

{:query=>{:bool=>{:filter=>[{:term=>{:country=>"US"}}, {:term=>{:confirmed=>true}}]}}}

However, the real request body is

{:query=>{:bool=>{:filter=>{:term=>{:confirmed=>true}}}}}

I tracked down the issue to this line of code https://github.com/toptal/chewy/blob/master/lib/chewy/search/parameters.rb#L157
Is it a bug?

@oricodes89
Copy link

oricodes89 commented Apr 9, 2019

@pyromaniac Are filters not chainable? Of course they are.

@oricodes89
Copy link

@jiajiawang

This generates the query that you want:

UsersIndex.filter(term: {"country": "US"}).filter(term: { "confirmed": true })

Passing bool: { filter: [{ term: {"country": "US"}}]} messes up the internal merging of params, you are attempting to send the raw query to ES.

@jiajiawang
Copy link
Contributor Author

@oricodes89 Sure. But according the documentation, I should be able to chain filters and queries all together.
And the problem is resolvable by changing from query[:query][:bool].merge!(filter) to something like query[:query][:bool][:filter].concat(filter[:filter]).
So I wonder whether it is a bug or there might be some other unexpected results of doing the above fix.

@oricodes89
Copy link

oricodes89 commented Apr 10, 2019

@jiajiawang But you can chain filters and queries, just use the DSL provided by chewy.

UsersIndex.query(term: {"country": "US"}).filter(term: { "confirmed": true })

=> {:query=>{:bool=>{:must=>{:term=>{:country=>"US"}}, :filter=>{:term=>{:confirmed=>true}}}}}

It doesn't return what you expected on your original post:

{:query=>{:bool=>{:filter=>[{:term=>{:country=>"US"}}, {:term=>{:confirmed=>true}}]}}}

But you can use two filter queries like I suggested if you want to render that particular query.

@jiajiawang
Copy link
Contributor Author

@oricodes89 I have no doubt that chaining two filters can result in the request body I want. But that's not my question.
I'm just expecting

UsersIndex.query(bool: { filter: [{ term: {"country": "US"}}]}).filter(term: { "confirmed": true })

would result in the same request body.
But it's not and I believe it's a bug.

@oricodes89
Copy link

@jiajiawang I guess I'm missing something on the documentation, can you share an example on it?

@jiajiawang
Copy link
Contributor Author

@oricodes89 I'm not sure what other examples you want me to share.
This is the simplest example I can think of to explain the issue.

UsersIndex.query(bool: { filter: { term: {"country": "US"}}}) returns
:body=>{:query=>{:bool=>{:filter=>{:term=>{:country=>"US"}}}}}.

UsersIndex.filter(term: { "confirmed": true }) returns
:body=>{:query=>{:bool=>{:filter=>{:term=>{:confirmed=>true}}}}}.

UsersIndex.query(bool: { filter: { term: {"country": "US"}}}).filter(term: { "confirmed": true }) returns
:body=>{:query=>{:bool=>{:filter=>{:term=>{:confirmed=>true}}}}}.
Is this your expected result of chaining them?

@oricodes89
Copy link

Mmm...I guess I never expected it because the gem does not give you any example combining those kinds of queries.

@dalthon dalthon added the bug label Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants