# Help Using Python to Load Data into ES

**URL:** <https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134>\
**Category:** Elasticsearch\
**Created:** [September 5, 2019, 1:33am UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134 "2019-09-05T01:33:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![beezit](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@beezit](https://discuss.elastic.co/u/beezit)\
**Post date:** [September 5, 2019, 1:33am UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/1 "2019-09-05T01:33:05Z")

</div>

I am having some issues getting the following python script to load data into ElasticSearch, can someone please guide me. Here is my python script. My goal is to use python to load this into ElastiSearch and also view my index in Kibana.

Excuse the long json file.

```
import json
from datetime import datetime
from elasticsearch import Elasticsearch
# use ES default connection
es = Elasticsearch()
directory = r'path to file'
i = 1
for filename in os.listdir(directory):
    if filename.endswith(".json"):
        print(filename)
        f = open(filename)
        docket_content = f.read()
        es.index(index='STates_Index', ignore=400, doc_type='docket', 
        id=i, body=json.loads(docket_content))
        i = i + 1                                                   

```

Here is the json file that I am working with.

```
    {
"type" : "FeatureCollection",
"name" : "Outliner",
"features" : [
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-77.9741261597, 35.2186618283],
					[-78.3147315937, 35.7579537654],
					[-79.2513965371, 35.6444186208],
					[-78.541801883, 34.7645212497],
					[-77.9741261597, 35.2186618283]
				]
			]
		},
		"properties" : {
			"Name" : "North Carolina",
			"abbrev" : "NC"
		}
	},
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-79.4784668264, 37.1771430737],
					[-79.4784668264, 36.8649214259],
					[-78.541801883, 36.8649214259],
					[-78.541801883, 37.1771430737],
					[-79.4784668264, 37.1771430737]
				]
			]
		},
		"properties" : {
			"Name" : "Virginia",
			"abbrev" : "VA"
		}
	},
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-81.2098777824, 34.5942185327],
					[-81.718192491, 34.6305267262],
					[-81.7465762772, 34.2331537199],
					[-81.2382615686, 34.1968455264],
					[-81.2098777824, 34.5942185327]
				]
			]
		},
		"properties" : {
			"Name" : "South Carolina",
			"abbrev" : "SC"
		}
	}
]
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![beezit](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@beezit](https://discuss.elastic.co/u/beezit)\
**Post date:** [September 6, 2019, 2:56am UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/2 "2019-09-06T02:56:09Z")

</div>

Any ideas please?

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [September 6, 2019, 5:38am UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/3 "2019-09-06T05:38:49Z")

</div>

Hi,

What is the problem you are getting?

To start, your index name should be lower case [index-name-type-name-and-field-name-rules](https://discuss.elastic.co/t/index-name-type-name-and-field-name-rules/133039)

Secondly, to make the data visible in Kibana you need to have a timestamp in the document.

Paul.

---

<div class="post-metadata">

**Author:** ![beezit](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@beezit](https://discuss.elastic.co/u/beezit)\
**Post date:** [September 9, 2019, 7:18pm UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/4 "2019-09-09T19:18:12Z")

</div>

I have modified to lower case and also added a timestamp as requested. GeoJSON included below. Also made some adjustments to the code, I do see it in the Kibana Indices list, and not on the map. Can someone please help point me in the right direction? I am not getting any errors in my code listed below: What am I missing?

```
#load package modules
import sys, json
import os
import time
from datetime import datetime
from elasticsearch import Elasticsearch, helpers
from pprint import pprint

root_path = "C:/Users/Base/Desktop/states/"
raw_data_path = root_path + "outlineddate/"
json_filename = "outlinedate.json"
directory = raw_data_path + json_filename

es = Elasticsearch(['localhost'],port=9200)

with open(directory, 'r', encoding='utf-8') as f:
    data = json.loads(f.read())
pprint(data)

es.index(index='statesdates_index', doc_type='doc', body=data)

```

Here is the Updated GeoJSON

```
{
"type" : "FeatureCollection",
"name" : "Outline_date",
"features" : [
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-77.9741261597, 35.2186618283],
					[-78.3147315937, 35.7579537654],
					[-79.2513965371, 35.6444186208],
					[-78.541801883, 34.7645212497],
					[-77.9741261597, 35.2186618283]
				]
			]
		},
		"properties" : {
			"timestamp" : "20190909071754.9718596",
			"Name" : "North Carolina",
			"abbrev" : "NC"
		}
	},
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-79.4784668264, 37.1771430737],
					[-79.4784668264, 36.8649214259],
					[-78.541801883, 36.8649214259],
					[-78.541801883, 37.1771430737],
					[-79.4784668264, 37.1771430737]
				]
			]
		},
		"properties" : {
			"timestamp" : "20190909071755.0440474",
			"Name" : "Virginia",
			"abbrev" : "VA"
		}
	},
	{
		"type" : "Feature",
		"geometry" : {
			"type" : "Polygon",
			"coordinates" : [
				[
					[-81.2098777824, 34.5942185327],
					[-81.718192491, 34.6305267262],
					[-81.7465762772, 34.2331537199],
					[-81.2382615686, 34.1968455264],
					[-81.2098777824, 34.5942185327]
				]
			]
		},
		"properties" : {
			"timestamp" : "20190909071755.0442809",
			"Name" : "South Carolina",
			"abbrev" : "SC"
		}
	}
]

```

}

---

<div class="post-metadata">

**Author:** ![Doc\_Kaos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doc_kaos/32/53671_2.png) [@Doc\_Kaos](https://discuss.elastic.co/u/Doc_Kaos)\
**Post date:** [September 9, 2019, 8:08pm UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/5 "2019-09-09T20:08:39Z")

</div>

I would drop the `doc_type` parameter, it can't be used in ES7 and there's no point starting with one. In ES6 it has to be `_doc` I believe. But you can safely just leave it out.

Now that you can see the index, you may need to make sure the locations are being stored as GeoPoints [https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)

---

<div class="post-metadata">

**Author:** ![beezit](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@beezit](https://discuss.elastic.co/u/beezit)\
**Post date:** [September 9, 2019, 8:45pm UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/6 "2019-09-09T20:45:29Z")

</div>

So two things,

(1) They are actually boundaries of 3 states.  
(2) I went to your link and could not understand what I am looking at, I understand it to be mappings, correct me if i am wrong. How do I apply this to any of the states I am working on, an example will be very appreciated. Thanks.

---

<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:** [October 7, 2019, 8:45pm UTC](https://discuss.elastic.co/t/help-using-python-to-load-data-into-es/198134/7 "2019-10-07T20:45:37Z")

</div>

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