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

I can't see how to iterate over response returned from query #537

Closed
brettskiii opened this issue Aug 6, 2018 · 6 comments
Closed

I can't see how to iterate over response returned from query #537

brettskiii opened this issue Aug 6, 2018 · 6 comments

Comments

@brettskiii
Copy link

I'm doing something like this:

response = client.search index: 'daemon-statistics', body: {from: 0, size: 2, sort: [{event_at: {order: "desc"}}], query:  {bool: {
     must: [
         {match: {
           tenant_id: "108"
             }},
         {match: {
           event: "daemon-job-name"
         }},
        
     ]
   } } }

which returns a string..

--- took: 5 timed_out: false _shards: total: 5 successful: 5 skipped: 0 failed: 0 hits: total: 2358 max_score: null hits: - _index: "daemon-statistics" _type: "event" _id: "LsRzDWUBhKWdreNuciC3" _score: null _source: daemon_uuid: "8fe5e891-dda4-4bec-b7bc-c4ed7e83e2d4" event: "daemon-job-name" sequence_id: 15 event_at: "2018-08-06T04:16:34.438Z" time_elapsed: 161 tenant_id: 108 campaign_id: 2282 sort: - 1533528994438 - _index: "daemon-statistics" _type: "event" _id: "LcRzDWUBhKWdreNuciA8" _score: null _source: daemon_uuid: "8fe5e891-dda4-4bec-b7bc-c4ed7e83e2d4" event: "daemon-job-name" sequence_id: 15 event_at: "2018-08-06T04:16:34.277Z" time_elapsed: 155 tenant_id: 108 campaign_id: 269 sort: - 1533528994277

JSON.parse(response) will result in:

JSON::ParserError at /
784: unexpected token at '---
took: 3

or as suggested in the docs, the mash = Hashie::Mash.new response results in:

NoMethodError at /
undefined method `each_pair' for #<String:0x00007fa7d10008e0>
Did you mean?  each_char

I'm a bit lost been looking around for a solution, but can't seem to understand how to take the response returned from the client which comes in as a string and convert it to something I can iterate the results over.

@brettskiii
Copy link
Author

Just to add to this, it looks like YAML as the result, so I can convert it doing this:

response = JSON.parse(YAML.load(response).to_json)

and then I can iterate OK, but is this right?

@karmi
Copy link
Contributor

karmi commented Aug 6, 2018

@brettskiii, this is really weird — the regular response from any method of the client is a Ruby Hash, so you don't have to deserialize it. It does indeed look like in your case a YAML string is being returned — are you sure you don't set the Accept header somewhere to application/yaml or the format URL parameter to yaml? How do you initialize the client?

When you initialize the client with the trace: true option, it will print out a curl-formatted output, so you can see what's being sent to Elasticsearch.

@brettskiii
Copy link
Author

oh dear, yes that's it. I grabbed an example of the web that disabled ssl verification, and in the initialize it had format YAML. All good thank you pointed me exactly to the right area to fix 👍

@karmi
Copy link
Contributor

karmi commented Aug 6, 2018

Argh!, right, from here I guess :) Glad it's solved!

@brettskiii
Copy link
Author

hehe yeah, that's it :)

@suratpyari
Copy link

disabling ssl did not solve my issue. Also its already a json. Posted a new issue elastic/elasticsearch-rails#1041

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

3 participants