# Query ElasticSearchData USING Ruby ElasticsearchGem

**URL:** <https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634>\
**Category:** Beats\
**Tags:** metricbeat\
**Created:** [January 5, 2017, 4:56am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634 "2017-01-05T04:56:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 5, 2017, 4:56am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/1 "2017-01-05T04:56:55Z")

</div>

Hi -  
I have a setup with Elastic Search, Kibana and MetricBeat running with 5.1.1 version on my Windows-7 machine.  
Am trying to create a test case to check CPU rate at the API level; using Ruby elasticsearch-5.0.0 gem.

I have installed required gem on my machine and trying to connect to my cluster, which I couldn't proceed up.  
Am getting the unauthorized error, may be because, my set-up is installed with x-pack.  
Could you please let me know how do I connect to the Elastic Search cluster and query the CPU usage from API level (i.e. ealsticsearch.gem).

Thanks in advance.

Prakash.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [January 5, 2017, 9:10am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/2 "2017-01-05T09:10:30Z")

</div>

Hi @prakash1243,

you need to pass the correct credentials when creating the Ruby client as stated in the [README of elasticsearch-transport](https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-transport/README.md#authentication).

If you did not change the defaults, the user is `elastic` and the password `changeme`.

Daniel

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 5, 2017, 9:17am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/3 "2017-01-05T09:17:19Z")

</div>

Hi @danielmitterdorfer

Thanks so much for your reply:

I tried with this statement: Elasticsearch::Client.new url: '[http://elastic:changeme@localhost:9200](http://elastic:changeme@localhost:9200)'  
I've attached the screenshot of the response:

 ![](https://us1.discourse-cdn.com/elastic/original/2X/9/9b1d3952c9273f5678adfc5a560391561db720ed.PNG)  
I believe, I've got connected to the Elastic Search. Could you please let me know how do I query for the Metric Beat CPU usage ? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [January 5, 2017, 9:27am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/4 "2017-01-05T09:27:06Z")

</div>

Hi @prakash1243,

this doesn't look like a (HTTP) response to me but rather a debugger view of the client object.

You could verify it by simply retrieving the version (my Ruby is a bit rusty ;)):

```auto
puts client.info()["version"]["number"]

```

You should see `5.1.1.` in your terminal.

> [@prakash1243](#):
>
> Could you please let me know how do I query for the Metric Beat CPU usage ? Thanks in advance.

I'm not familiar with the document schema. I think you should ask this question in a Beats forum rather than the Elasticsearch forum. Can you please open a new topic there?

Daniel

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 5, 2017, 9:31am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/5 "2017-01-05T09:31:41Z")

</div>

Hi @danielmitterdorfer:  
Yes, You are absolutely right, I've got 5.1.1 as version on my IRB console.  
So, I believe, I've got connected to the cluster. I'd like to know, how do we query for the cpu usage of a node.  
Thank you very much for your help.

Regards,  
Prakash

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 6, 2017, 6:34am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/6 "2017-01-06T06:34:50Z")

</div>

Hi @andrewkroh-

Could you please help getting the Metric beats data using Elastic Search Gem with Ruby.

Thanks in advance !!

---

<div class="post-metadata">

**Author:** ![ruflin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruflin/32/3116_2.png) [@ruflin](https://discuss.elastic.co/u/ruflin)\
**Post date:** [January 6, 2017, 3:31pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/7 "2017-01-06T15:31:11Z")

</div>

Not sure I can follow: How is Metricbeat and Ruby client connected? Do you have a specific issue with querying the data?

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 6, 2017, 3:51pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/8 "2017-01-06T15:51:38Z")

</div>

Hi @ruflin

My Scenario is: I've to check the CPU Usage of a windows server, to make sure continue the Test cases execution  
If it goes beyond the threshold, I'd abort the Test case execution.

I would be able to check CPU only via API (Not with Kibana). So I tried using Ruby gem (elasticsearch-5.1.1), As I mentioned in my previous posts, Am able to connect to the cluster and able to get the version.

I'd need a help in querying the data to get the CPU Usage. Please let me know if am unclear. Thanks !

Regards,  
Prakash

---

<div class="post-metadata">

**Author:** ![ruflin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruflin/32/3116_2.png) [@ruflin](https://discuss.elastic.co/u/ruflin)\
**Post date:** [January 9, 2017, 3:14pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/9 "2017-01-09T15:14:59Z")

</div>

Ok, this sounds more like you have problems with the ruby client then metricbeat itself I understand it correctly. Did you check the docs for the ruby client? [https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/\_the\_ruby\_client.html](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/_the_ruby_client.html)

There it also shows you how you can do some simple queries. What did you try so far that didn't work?

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 12, 2017, 4:41am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/10 "2017-01-12T04:41:58Z")

</div>

Hi @ruflin-  
I have this query so far (GET metricbeat-2017.01.09/metricsets/\_search?q=cpu) .  
I'd like to know how to run the GET queries with the Ruby Client Gem. Could you please help me out. Thanks !

Prakash.

---

<div class="post-metadata">

**Author:** ![ruflin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruflin/32/3116_2.png) [@ruflin](https://discuss.elastic.co/u/ruflin)\
**Post date:** [January 12, 2017, 10:48am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/11 "2017-01-12T10:48:29Z")

</div>

Please check the docs. Here you see some example queries ([https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/\_the\_ruby\_client.html#\_example\_usage](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/_the_ruby_client.html#_example_usage)) but there are much more details in the client api docs [http://www.rubydoc.info/gems/elasticsearch](http://www.rubydoc.info/gems/elasticsearch)

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 23, 2017, 4:47am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/12 "2017-01-23T04:47:11Z")

</div>

Thanks for your reponse @ruflin.

Am having hard time to execute the below query with the examples I had gone thru, Could you please help with executing the below query using Ruby Client

GET metricbeat-2017.01.20/metricsets/\_search  
{  
"size": 0,  
"query": {  
"range": {  
"@timestamp": {  
"gte": "19/01/2017",  
"lte": "23/01/2017",  
"format": "dd/MM/yyyy||yyyy"  
}  
}  
},  
"aggs": {  
"NAME": {  
"avg": {  
"field": "system.fsstat.total\_size.total"  
}  
}  
}  
}

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ruflin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruflin/32/3116_2.png) [@ruflin](https://discuss.elastic.co/u/ruflin)\
**Post date:** [January 24, 2017, 8:59pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/13 "2017-01-24T20:59:31Z")

</div>

Can you share what you tried so far based on the examples and didn't work?

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 27, 2017, 4:33am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/14 "2017-01-27T04:33:44Z")

</div>

Hi @ruflin:

Here is my query:

res = client.search index: 'metricbeat-2017.01.23',  
body: {  
size: 0,  
query: {   
match:  
#{ range: {  
#date: { gte: "{{1484896203312}}", lte: "{{1484897124046}}" } },  
{ "beat.hostname": "AAEINBLR03199L" }  
},  
aggregations: { tags: { terms: { field: 'system.fsstat.total\_size.used' } } }   
}   
end

I wanted to put the range, to select the data between time stamps, which I couldn't able to do that. so I had commented it out for now.

Would you please let me know, how do I put the range inbetween and execute this query.

When I use the search\_template method, I get the below error message:

 ![](https://us1.discourse-cdn.com/elastic/original/2X/d/d74de10cfdeb246ac8f7daf4a1a6956cd685345d.png)

Thanks !

---

<div class="post-metadata">

**Author:** ![prakash1243](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakash1243](https://discuss.elastic.co/u/prakash1243)\
**Post date:** [January 30, 2017, 4:09am UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/15 "2017-01-30T04:09:30Z")

</div>

Hi @ruflin-

I've to do the multiple filters and aggregate the final results.

Filters is like, host name and @timestamp and get the aggregated disk info of a node. Please let me know. Thanks !

Regards,  
Prakash

---

<div class="post-metadata">

**Author:** ![ruflin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruflin/32/3116_2.png) [@ruflin](https://discuss.elastic.co/u/ruflin)\
**Post date:** [January 30, 2017, 1:36pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/16 "2017-01-30T13:36:13Z")

</div>

For the range, this one here should help: [https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/range.rb#L8](https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/range.rb#L8)

For the error: You must paste it here in text not screenshot, otherwise it is very hard to read.

I'm not really an expert on Ruby or the es client, but I strongly recommend you to either check some examples online or check the source code which has good docs inside.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 27, 2017, 1:36pm UTC](https://discuss.elastic.co/t/query-elasticsearchdata-using-ruby-elasticsearchgem/70634/17 "2017-02-27T13:36:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
