如何使用带有关键字参数的es.index()api为elasticsearch中的文档编制索引?

2024-04-26 09:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Elasticsearch中的一个索引中插入一个文档。为了做到这一点,我使用了一个适用于97/100id的代码片段。但是,对于一个特定的id,它的行为就好像使用的api是错误的一样

我试过阅读文档,它确实允许关键字参数。我已经成功地使用相同的脚本索引了类似的文档

def index_columnar_mapping_in_datatable(es, index_name, datatable_file):
    mapping = {}
    mapping['columns'] = []
    with open(datatable_file, 'r') as json_file:
        text = json_file.read()
        json_content = json.loads(text)
        for col_ in json_content['columns']:
            try:
                del col_['analysis']
            except KeyError:
                pass
            curr_column = {}
            curr_column['_id'] = col_['id']
            curr_column['name'] = col_['name']
            curr_column['data_type'] = col_['data_type']
            mapping['columns'].append(curr_column)
    print(
        "This would be the mapping: {}".format(
            mapping['columns']
        )
    )
    es.index(
        index='datatables',
        doc_type='datatable_v1',
        request_timeout=60,
        id=index_name,
        body={
            "columns": mapping['columns']
        }
    )

输出为:

This would be the mapping: [{'_id': u'c_1', 'name': u'Farzi ID', 'data_type': u'string'}, {'_id': u'c_92', 'name': u'phew', 'data_type': u'string'}, {'_id': u'c_88', 'name': u'ppsl', 'data_type': u'string'}]
Traceback (most recent call last):
  File "index_datatable.py", line 116, in <module>
    index_columnar_mapping_in_datatable(index_name, es, datatable_file)
  File "index_datatable.py", line 34, in index_columnar_mapping_in_datatable
    id=index_name
TypeError: index() takes no keyword arguments

我希望代码运行并在所需索引中索引所需的文档

我的Python版本是Python 2.7.6,pip freeze为Elasticsearch声明了以下内容:

elasticsearch==2.4.1
elasticsearch-dsl==2.2.0

Tags: columnsnamein文档idjsondataindex