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

.search_type and .preferences always error #642

Open
paulsuda opened this issue Apr 23, 2018 · 5 comments
Open

.search_type and .preferences always error #642

paulsuda opened this issue Apr 23, 2018 · 5 comments
Labels

Comments

@paulsuda
Copy link

paulsuda commented Apr 23, 2018

I have a query like this, which works just fine...

UsersIndex.query(match: {name: 'test'}).object_hash

Following the docs, I tried something like this to use the user ID to guarantee the same shards respond for a give user...

UsersIndex.query(match: {name: 'test'}).preference('MY_USER_ID_STRING').object_hash

... this throws this error...

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [preference].","line":1,"col":15}],"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [preference].","line":1,"col":15},"status":400}
from /home/vagrant/.rbenv/versions/2.3.6/lib/ruby/gems/2.3.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb:202:in `__raise_transport_error'

... which is not that surprising, it looks like it's putting it in the body. This can be seen inspecting with binding.pry...

[42] pry(main)> UsersIndex.query(match: {name: 'test'}).preference('MY_USER_ID_STRING')
=> <UsersIndex::Query {:index=>["users"], :type=>["user"], :body=>{:preference=>"MY_USER_ID_STRING", :query=>{:match=>{:name=>"test"}}}}>

... same thing happens when I try to set a search_type like this according to the docs...

UsersIndex.query(match: {name: 'test'}).search_type(:dfs_query_then_fetch).object_hash

... it gives a similar error ...

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [search_type].","line":1,"col":16}],"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [search_type].","line":1,"col":16},"status":400}
from /home/vagrant/.rbenv/versions/2.3.6/lib/ruby/gems/2.3.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb:202:in `__raise_transport_error'

... also because it's putting search_type in the query body.

Elasticsearch docs say both of those should be in the query string instead.

I looked around and tried to find where to fix this, but haven't had time yet to really figure out the Chewy code base. Hopefully this is an easy quick fix for someone that knows their way around? Thanks to anyone who can help with this!

@sp2410
Copy link

sp2410 commented May 29, 2018

Any solution to this ? I am having the same problem

@pyromaniac
Copy link
Contributor

It actually looks like an elasticsearch-ruby issue. Its API accepts everything and should decide where to put particular params. Try to update it. Or downgrade.

@mmarini
Copy link

mmarini commented Jun 18, 2018

I've been having the same issue attempting to set the search type to dfs_query_then_fetch.

I'm on elasticsearch-ruby version 5.0.5. When Chewy calls elasticsearch-ruby's search method,

https://github.com/elastic/elasticsearch-ruby/blob/86a36ec0db704b2a62dd4d5fe9edf887625b1826/elasticsearch-api/lib/elasticsearch/api/actions/search.rb#L173

the __validate_and_extract_params method will only parse the keys at the first level of the hash passed in. search_type is being missed because it's within the body key of the hash.

I think chewy should not be putting search_type within the body, but at a level above

mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 25, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 26, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 26, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 26, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 26, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
mattzollinhofer added a commit to mattzollinhofer/chewy that referenced this issue Aug 26, 2018
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
@pyromaniac
Copy link
Contributor

Hey, try the latest master

@yonnage
Copy link

yonnage commented Jan 29, 2019

Using the latest master, .preference isn't working but .search_type is.

Looks like preference is added to the body instead of the query string.

UsersIndex.query(match: {name: 'test'}).preference('MY_USER_ID_STRING').render

{:index=>["users"], :type=>["user"], :body=>{:preference=>"MY_USER_ID_STRING", :query=>{:match=>{:name=>"test"}}}}

and errors with...

UsersIndex.query(match: {name: 'test'}).preference('MY_USER_ID_STRING').object_hash

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [preference]","line":1,"col":15}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":...

Also tested .search_type which is added to the query string and does appear to work.

Does preference need to be added to the list of QUERY_STRING_STORAGES?

@dalthon dalthon added the bug label Feb 19, 2021
cyucelen pushed a commit to cyucelen/chewy that referenced this issue Jan 28, 2023
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
cyucelen pushed a commit to cyucelen/chewy that referenced this issue Jan 28, 2023
Per ES docs: search_type, request_cache, and the allow_partial_search_results
should not be in the body. They should be sent as query string parameters. My
understanding is that chewy should act like this:

  $ IssuesIndex.request_cache(true).render
  # good (new)
  $ {:index=>["issues"], :type=>["issue"], :body=>{}, :request_cache=>true}
  # bad (old)
  $ {:index=>["issues"], :type=>["issue"], :body=>{:request_cache=>true}

I believe this resolves issue toptal#586 and part of issue toptal#642.

(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4)
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

6 participants