0

我在 python 中使用 Elasticsearch。我在 pandas 框架中有数据(3 列),然后我添加了两列 _index 和 _type 并使用 pandas 内置方法将数据转换为每条记录的 json。

data =  data.to_json(orient='records') 

这是我的数据,

[{"op_key":99140046678,"employee_key":991400459,"Revenue Results":6625.76480192,"_index":"revenueindex","_type":"revenuetype"},     
 {"op_key":99140045489,"employee_key":9914004258,"Revenue Results":6691.05435536,"_index":"revenueindex","_type":"revenuetype"},
......
}]

我的映射是:

user_mapping =  {
        "settings" : {
            "number_of_shards": 3,
            "number_of_replicas": 2
        },

        'mappings': {
            'revenuetype': {
                'properties': {
                    'op_key':{'type':'string'},
                    'employee_key':{'type':'string'},
                    'Revenue Results':{'type':'float','index':'not_analyzed'},
                }
            }
        }
    }

然后在使用 helpers.bulk(es,data) 时遇到此错误:

    Traceback (most recent call last):
      File "/Users/adaggula/Documents/workspace/ElSearchPython/sample.py", line 59, in <module>
        res = helpers.bulk(client,data)
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 188, in bulk
        for ok, item in streaming_bulk(client, actions, **kwargs):
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 160, in streaming_bulk
        for result in _process_bulk_chunk(client, bulk_actions, raise_on_exception, raise_on_error, **kwargs):
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 89, in _process_bulk_chunk
        raise e
    elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: index is
 missing;2: type is missing;3: index is missing;4: type is missing;5: index is 
missing;6: ....... type is missing;999: index is missing;1000: type is missing;')

看起来对于每个 json 对象,都缺少索引和类型。如何克服这一点?

4

1 回答 1

0

Pandas 数据帧到 json 的转换 是解决问题的技巧。

data =  data.to_json(orient='records')
data= json.loads(data)
于 2016-07-04T10:10:49.040 回答