# Py-script querys againt elastic problem

**URL:** <https://discuss.elastic.co/t/py-script-querys-againt-elastic-problem/171773>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-graph\
**Created:** [March 11, 2019, 1:24pm UTC](https://discuss.elastic.co/t/py-script-querys-againt-elastic-problem/171773 "2019-03-11T13:24:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel19](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@Daniel19](https://discuss.elastic.co/u/Daniel19)\
**Post date:** [March 11, 2019, 1:24pm UTC](https://discuss.elastic.co/t/py-script-querys-againt-elastic-problem/171773/1 "2019-03-11T13:24:02Z")

</div>

My code looks like this. its do get data, monitoring the total amount of hits and so on.

```
Preformatted text
    def get_last_hour():
    i = 0
    swc_arr = []
    res1 = es.search(index='testindex', body={
        "query" : {
            "range" : {
                "timestamp" : {
                    "gte" : "now"
                }
            }
        }

    })
    print("Number of %d Hits:" % res1['hits']['total'])
    for hit in res1['hits']['hits']:
        print("%(timestamp)s %(build_nb)s: %(swc_name)s: %(metric_name)s" % hit["_source"]) 

```

but what i tried to get is something more like this to work.

```
def get_last_hour():

    res1 = es.search(index='testindex', body={
        "query" : {
            "range" : {
                "timestamp" : {
                    "gte" : "now"
                }
            }
        } 
    })

    print("Number of %d Hits:" % res1['hits']['total'])
    for hit in res1['hits']['hits']:
        swc_arr = []
        swc_arr.append(hit)
        print("%(timestamp)s %(build_nb)s: %(swc_name)s: %(metric_name)s: %(metric_value)s" % hit["_source"])

    for item in swc_arr:
        print(swc_arr[item])
```

---

<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:** [April 8, 2019, 1:24pm UTC](https://discuss.elastic.co/t/py-script-querys-againt-elastic-problem/171773/2 "2019-04-08T13:24:02Z")

</div>

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