# Preprocess data with python

**URL:** <https://discuss.elastic.co/t/preprocess-data-with-python/216523>\
**Category:** Elasticsearch\
**Created:** [January 25, 2020, 1:52pm UTC](https://discuss.elastic.co/t/preprocess-data-with-python/216523 "2020-01-25T13:52:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristian\_Bovino](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_bovino/32/61426_2.png) [@Cristian\_Bovino](https://discuss.elastic.co/u/Cristian_Bovino)\
**Post date:** [January 25, 2020, 1:52pm UTC](https://discuss.elastic.co/t/preprocess-data-with-python/216523/1 "2020-01-25T13:52:19Z")

</div>

Hi Everyone,  
I'm quite new to the Elastic suite. My current situation is:

Data --\> Logstash (with filters) --\> Elasticsearch --\> Kibana

I would like to process data with a python script (ML Model) before store it into Elasticsearch.  
What is the best method to do it? I currently use Elastic Stack 7.5.0 version.

Thanks

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [January 25, 2020, 5:40pm UTC](https://discuss.elastic.co/t/preprocess-data-with-python/216523/2 "2020-01-25T17:40:43Z")

</div>

Look at the python elasticsearch module helpers [bulk](https://elasticsearch-py.readthedocs.io/en/master/helpers.html)

I just build a list of events: (This is in an outer loop using the elasticsearch\_dsl to read another index merging items)

```
   es_row = { "_index": "rollup-ironport-spf-"+datetime.today().strftime('%Y.%m.%d'),
               "_type": "doc",
               "_id": mid_id,
               "@timestamp": spf_timestamp,
               "event_timestamp": hit["syslog_timestamp"],
               "mid": mid_id,
               "icid": icid_id,
            }

   s = Search(using=client, index="syslog-ironport-*") \
       .query("match", message=icid_id).extra(size=100) \
       .filter("range", ** {'@timestamp': {'gte': 'now-1d', 'lt': 'now'}}) \

   icid_results = s.execute()

   for icid in icid_results.hits:
       if "From:" in icid["cisco_message"] and mid_id in icid["cisco_message"]:
# print("....", icid["syslog_timestamp"], icid["cisco_message"])
           es_row.update(from_grok.match(icid["cisco_message"]))

       if "To:" in icid["cisco_message"] and mid_id in icid["cisco_message"]:
# print("....", icid["syslog_timestamp"], icid["cisco_message"])
           es_row.update(to_grok.match(icid["cisco_message"]))

       if "dns host" in icid["cisco_message"]:
# print("....", icid["syslog_timestamp"], icid["cisco_message"])
           es_row.update(icid_grok.match(icid["cisco_message"]))

   es_out.append(dict(es_row))

```

Then send to elastic:

```
spf_index = bulk(client, es_out)
```

---

<div class="post-metadata">

**Author:** ![Cristian\_Bovino](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_bovino/32/61426_2.png) [@Cristian\_Bovino](https://discuss.elastic.co/u/Cristian_Bovino)\
**Post date:** [January 26, 2020, 12:36pm UTC](https://discuss.elastic.co/t/preprocess-data-with-python/216523/3 "2020-01-26T12:36:06Z")

</div>

Thank Len, this is a solution I was looking for.

---

<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:** [February 23, 2020, 12:36pm UTC](https://discuss.elastic.co/t/preprocess-data-with-python/216523/4 "2020-02-23T12:36:08Z")

</div>

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