如何在Python中将此嵌套字典加载到ElasticSearch中?

时间:2019-02-26 23:28:22

标签: python json elasticsearch

我有如下数据:

df =     {
        "3498573": [
            {
                "274985537": 0.43453,
                "274933900": 0.5344,
                "27845502": 0.32359
            }
        ],
        "4394879": [
            {
                "932847": 0.23984734
                "3454325": 0.2398472
            }
        ]
    }

它是字典内列表内的嵌套字典。

另一种表示方式是:

{
    "customer_id": [
        {
            "p_id": score,
            "p_id": score,
            "p_id": score
        }
    ],
    "customer_id": [
        {
            "p_id": score
            "p_id": score
        }
    ]
}

我想知道如何将这些数据索引到ElasticSearch中。我正在使用elasticsearch python API,并且创建了如下映射:

request_body = {
    "settings" : {
        "number_of_shards": 5,
        "number_of_replicas": 1
    },

    'mappings': {
        'recommended': {
            'properties': {
                'customer_id': {
                    "type": "nested",
                    "properties": {
                        "master_product_id": {
                            "type": "text"
                        },
                        "score": {
                            "type": "integer"
                        }
                    }
                }
            }}}
}
print("creating 'example_index' index...")
es.indices.create(index = 'example_index2', body = request_body)

尝试像这样直接加载数据时出现<400>错误:

requests.post(address, data=df, headers=headers)

为了加载数据,我是否需要像下面那样对数据进行不同的调整?如果是这样,我该如何塑造它?

nested = ({

    "customer_id": "13984209",
    "recommended_products": [
        {
            "master_product_id": "945873",
            "score": 0.94879437
        }
    ],
    "customer_id": "309248",
    "recommended_products": [
        {
            "master_product_id": "2383",
            "score": 0.36367326
        }
    ]

})

0 个答案:

没有答案