python 将某个字段存储为列表类型

实现存储数据格式为

{
        "_index": "nested-20180815",
        "_type": "stb-iptv-montor-m-gather-apk",
        "_id": "AWU8sZboGQQbsn0rAW4J",
        "_score": 1,
        "_source": {
          "mdiNested": [
            {
              "mdiMLR": 0,
              "mdiType": "0"
            },
            {
              "mdiMLR": 0,
              "mdiType": "1"
            },
            {
              "mdiMLR": 0,
              "mdiType": "2"
            },
            {
              "mdiMLR": 0,
              "mdiType": "3"
            },
            {
              "mdiMLR": 0,
              "mdiType": "4"
            },
            {
              "mdiMLR": 0,
              "mdiType": "5"
            }
          ]
        }
      }

代码:

from elasticsearch import Elasticsearch
from elasticsearch import helpers
import json

es_20 = Elasticsearch(hosts="1.0.0.0", port=9200, timeout=15000)
time_ = "20180815"
index_20 = "nested-{0}".format(time_)
type_20 = "stb-iptv-montor-m-gather-apk"


def set_mapping():
    my_mappping = {
        type_20: {
            "properties": {
                "mdiNested": {
                    "properties": {
                        "mdiMLR": {
                            "type": "short"
                        },
                        "mdiType": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
    create_index = es_20.indices.create(index=index_20, body=None)
    create_mapping = es_20.indices.put_mapping(index=index_20, body=my_mappping, doc_type=type_20)
    mdiMLR = [0,1,2,3,4]
    mdiType = ["0","1","2","3","4","5"]
    actions = []
    dict_ ={}
    for mdiMLR_ in mdiMLR:
        dict_list = []
        for type in mdiType:
            t1 ={'mdiMLR': mdiMLR_, 'mdiType': type}
            dict_list.append(t1)
        action = {
                "_index": index_20,
                "_type": type_20,
                "_source": {
                    "mdiNested": dict_list
                }
        }
        actions.append(action)
    helpers.bulk(es_20, actions)

你可能感兴趣的:(python,python)