I have a query I need to run against ElasticSearch and am using Search::Elasticsearch module. The issue is getting the data range correctly in the query on the PERL side as I can make this work from curl without any issues. After much tinkering with format, the following no longer throws errors when executing the code but the date range does not change the number of results returned no matter what I set the date to. This leads me to believe that there is something wrong with the code.
For what it is worth, I have noticed that if I change the 'gte' to a 'lte' then I get a count of -1 showing no results are being returned. So, something is "working" if I do that ...
Any hints, changes or suggestions would be much appreciated!
#!/usr/bin/env perl
# global settings
use warnings;
use strict;
$|++;
# libraries
use Data::Dumper;
use Search::Elasticsearch;
use Try::Tiny;
# global variables
my $elk_host1 = '10.0.10.61:9200';
my $elk_host2 = '10.0.10.51:9200';
my $elk_host3 = '10.0.10.52:9200';
my $elk_host4 = '10.0.10.53:9200';
my $elk_user = 'user';
my $elk_pass = 'pass';
my $dt = `date +%F_%T`;
chomp $dt;
my $latest_dt;
print "Starting run at: ".`date`;
my $e = Search::Elasticsearch->new(
nodes => [
"http://$elk_user:$elk_pass\@$elk_host1" ,
"http://$elk_user:$elk_pass\@$elk_host2" ,
"http://$elk_user:$elk_pass\@$elk_host3" ,
"http://$elk_user:$elk_pass\@$elk_host4" ,
] ,
max_requests => 10000,
);
my $results = $e->search (
size => 10000,
index => 'api-*',
body => {
query => {
bool => {
must => {
term => {
'_type' => "alarm",
},
},
filter => {
range => {
'@timestamp' => {
gte => "2018-04-23 00:00:00",
format => "YYYY-MM-DD HH:mm:ss"
}
}
}
}
}
}
);
print "Count: ".$#{ $results->{hits}->{hits} }."\n";