# Create Elastic index from Python script

**URL:** https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354
**Category:** Elasticsearch
**Created:** [November 13, 2020, 2:28pm UTC](https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354 "2020-11-13T14:28:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jose\_Angel\_Morena\_Si](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jose_angel_morena_si/32/76522_2.png) [@Jose\_Angel\_Morena\_Si](https://discuss.elastic.co/u/Jose_Angel_Morena_Si)
#### Post date: [November 13, 2020, 2:28pm UTC](https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354/1 "2020-11-13T14:28:10Z")

</div>

Hello,

I am trying to create an index in ES 7.9.1 with python 3. In order to do so I am using elasticsearch python package.

Script:

```auto
      doc_id=hex(random.getrandbits(128))[2:]
      doc = {
        'response_time': measure_response_time(app_url, request_timeout),
        '@timestamp': datetime.now()
      }
      index_name='java-response-time-'+str(datetime.today().strftime('%Y-%m-%d'))
      logger.info('index: '+index_name)
      res = es.index(index=index_name,id=doc_id, body=doc)
      logger.info('Estado nuevo indice: '+str(res['result']))
      res = es.get(index=index_name, id=doc_id)
      logger.info("Tiempo de respuesta: "+ str(res['_source']['response_time']))
      es.indices.refresh(index=index_name)
      time.sleep(int(request_frequency))

```

When I specify @timestamp as a time field:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/e/8ef15993c9d6cac464871956e8a550ecb4c7bc25.jpeg)

Documents are missing in "Discover" page:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/9/49fbe3c9da7ade93d87f00d66ff20a2a0aa40543.png)

Reminder: This only happens when I select the field @timestamp as a date time field. If I don't select it, then documents appear.

What is the correct date time format that I must use in my python script?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![abrx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abrx/32/43881_2.png) [@abrx](https://discuss.elastic.co/u/abrx)
#### Post date: [November 13, 2020, 3:18pm UTC](https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354/2 "2020-11-13T15:18:52Z")

</div>

Hi !  
It's just an idea but do you have same UTC zone on your python client / elasticsearch server ? (Maybe you can find out by looking at the indexed doc with no `@timestamp` specified)  
Not sure that Elasticsearch will index data happening in the future.  
You can (should) add the UTC info in datetime.now (default default parameter `tz=None`)

---

<div class="post-metadata">

### Author: ![Jose\_Angel\_Morena\_Si](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jose_angel_morena_si/32/76522_2.png) [@Jose\_Angel\_Morena\_Si](https://discuss.elastic.co/u/Jose_Angel_Morena_Si)
#### Post date: [November 16, 2020, 9:50am UTC](https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354/3 "2020-11-16T09:50:41Z")

</div>

Hello,

Your comment shed some light on the matter and I managed to make the script work!

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/1/81407d984aca8615a556c2af75b1420759645669.png)

Eventually, what made it work was the following code:

```auto
      local_tz = pytz.timezone('Europe/Madrid')
      dt = local_tz.localize(datetime.today())
      doc = {
        'response_time': measure_response_time(app_url, request_timeout),
        '@timestamp': dt,
        'time': datetime.now().strftime("%Y-%m-%d %H:%M")
      }

```

Comment, I set the Kibana timezone in Advaced Settings to: "Europe/Madrid". By default it takes the one from the web navigator,  
Thank you @abrx!

---

<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: [December 14, 2020, 9:50am UTC](https://discuss.elastic.co/t/create-elastic-index-from-python-script/255354/4 "2020-12-14T09:50:44Z")

</div>

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