<commit_before>#!/usr/bin/env python
# encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

import datetime
import json

from elasticsearch import Elasticsearch

import pytablewriter as ptw


es = Elasticsearch(hosts="localhost:9200")
index_name = "es_writer_example"

writer = ptw.ElasticsearchWriter()
writer.stream = es
writer.table_name = index_name
writer.header_list = [
    "str", "byte", "short", "int", "long", "float", "date", "bool", "ip",
]
writer.value_matrix = [
    [
        "abc", 100, 10000, 2000000000, 200000000000, 0.1,
        datetime.datetime(2017, 1, 2, 3, 4, 5), True, "127.0.0.1",
    ],
    [
        "def", -10, -1000, -200000000, -20000000000, 100.1,
        datetime.datetime(2017, 6, 5, 4, 5, 2), False, "::1",
    ],
]

# delete existing index ---
es.indices.delete(index=index_name, ignore=404)

# create an index and put data ---
writer.write_table()

# display the result ---
es.indices.refresh(index=index_name)

print("----- mappings -----")
response = es.indices.get_mapping(index=index_name, doc_type="table")
print("{}\n".format(json.dumps(response, indent=4)))

print("----- documents -----")
response = es.search(
    index=index_name,
    doc_type="table",
    body={
        "query": {"match_all": {}}
    }
)
for hit in response["hits"]["hits"]:
    print(json.dumps(hit["_source"], indent=4))<commit_msg>Update an example<commit_after>#!/usr/bin/env python
# encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

import datetime
import json

from elasticsearch import Elasticsearch

import pytablewriter as ptw


es = Elasticsearch(hosts="localhost:9200")

writer = ptw.ElasticsearchWriter()
writer.stream = es
writer.index_name = "es writer example"
writer.header_list = [
    "str", "byte", "short", "int", "long", "float", "date", "bool", "ip",
]
writer.value_matrix = [
    [
        "abc", 100, 10000, 2000000000, 200000000000, 0.1,
        datetime.datetime(2017, 1, 2, 3, 4, 5), True, "127.0.0.1",
    ],
    [
        "def", -10, -1000, -200000000, -20000000000, 100.1,
        datetime.datetime(2017, 6, 5, 4, 5, 2), False, "::1",
    ],
]

# delete existing index ---
es.indices.delete(index=writer.index_name, ignore=404)

# create an index and put data ---
writer.write_table()

# display the result ---
es.indices.refresh(index=writer.index_name)

print("----- mappings -----")
response = es.indices.get_mapping(index=writer.index_name, doc_type="table")
print("{}\n".format(json.dumps(response, indent=4)))

print("----- documents -----")
response = es.search(
    index=writer.index_name,
    doc_type="table",
    body={
        "query": {"match_all": {}}
    }
)
for hit in response["hits"]["hits"]:
    print(json.dumps(hit["_source"], indent=4))
{'fraction_non_alphanumeric': 0.19360764144011755, 'fraction_numerical': 0.07127112417340191, 'mean_word_length': 3.6152542372881356, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4649643', 'n_tokens_mistral': 1093, 'n_tokens_neox': 950, 'n_words': 213}