Search code examples
pythonelasticsearchkibanaelasticsearch-py

how to feed data to Elasticseach as Integer using Python?


i am using this python script to feed my data to elasticsearch 6.0. How can i store the variable Value with type float in Elasticsearch? I can't use the metric options for the visualization in Kibana, because all the data is stored automatically as string

from elasticsearch import Elasticsearch

Device=""
Value=""
for key, value in row.items():  
    Device = key
    Value = value
    print("Dev",Device,  "Val:", Value)                     
    doc = {'Device':Device, 'Measure':Value ,  'Sourcefile':filename}
    print('   doc:    ', doc)
    es.index(index=name, doc_type='trends', body=doc)

Thanks

EDIT:

After the advice of @Saul, i could fix this problem with the following code:

import os,csv
import time
from elasticsearch import Elasticsearch
#import pandas as pd
import requests

Datum = time.strftime("%Y-%m-%d_")
path = '/home/pi/Desktop/Data'


os.chdir(path)
name = 'test'
es = Elasticsearch() 

    #'Time': time ,
#url = 'http://localhost:9200/index_name?pretty'
doc = {
  "mappings": {
    "doc": { 
      "properties": { 
        "device":    { "type": "text"  }, 
        "measure":     { "type": "text"  }, 
        "age":      { "type": "integer" },  
        "created":  {
          "type":   "date", 
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
#headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
#r = requests.post(url, data=json.dumps(data), headers=headers)
r= es.index(index=name, doc_type='trends', body=doc)

print(r)

Solution

  • You need to send a HTTP Post request using python request, as follows:

    url = "http://localhost:9200/index_name?pretty”
    data = {
      "mappings": {
        "doc": { 
          "properties": { 
            "title":    { "type": "text"  }, 
            "name":     { "type": "text"  }, 
            "age":      { "type": "integer" },  
            "created":  {
              "type":   "date", 
              "format": "strict_date_optional_time||epoch_millis"
            }
          }
        }
      }
    }
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    r = requests.post(url, data=json.dumps(data), headers=headers)
    

    Please replace index_name in the URL with the name of the index you are defining in to elasticsearch engine.

    If you want to delete the index before creating it again, please do as follows:

    url = "http://localhost:9200/index_name”
    data = { }
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    r = requests.delete(url, data=json.dumps(data), headers=headers)
    

    please replace index_name in the URL with your actual index name. After deleting the index, create it again with the first code example above including the mappings that you would need. Enjoy.