Python elasticsearch helpers.bulk错误地失败了吗? (缺少脚本或doc)

时间:2017-02-27 15:25:40

标签: python elasticsearch elasticsearch-py

在python函数中使用以下有效负载

[{'_id': '979507',                                                                                                                                            [15/1871]
  '_index': 'follow-search-alias',
  '_op_type': 'update',
  '_type': 'follow',
  'script': {'inline': 'ctx._source.followers += follower',
   'lang': 'groovy',
   'params': {'follower': ['3054805']}}},
 {'_id': '979507',
  '_index': 'follow-search-alias',
  '_op_type': 'update',
  '_type': 'follow',
  'script': {'inline': 'ctx._source.following += user_being_followed',
   'lang': 'groovy',
   'params': {'user_being_followed': []}}},
 {'_id': '3054805',
  '_index': 'follow-search-alias',
  '_op_type': 'update',
  '_type': 'follow',
  'script': {'inline': 'ctx._source.followers += follower',
   'lang': 'groovy',
   'params': {'follower': []}}},
 {'_id': '3054805',
  '_index': 'follow-search-alias',
  '_op_type': 'update',
  '_type': 'follow',
  'script': {'inline': 'ctx._source.following += user_being_followed',
   'lang': 'groovy',
   'params': {'user_being_followed': ['979507']}}}]

当我使用helpers.bulk()

时,我在python的elasticsearch中遇到以下错误
RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: script or doc is missing;2: script or doc is missing;3: script or doc is missing;4: script or doc is missing;')

究竟怎么会这样?此数组中的每个元素都有一个脚本标记。该功能本身已经适用于许多其他情况,只是不是这个和其他几个。

在shell中手动运行它,但不在此函数中吗?

功能如下:

@classmethod
def add_follows(cls, follows):
    docs = []
    user_ids = [f.follower_id for f in follows] + [f.followed_id for f in follows]
    users = User.query.filter(User.id.in_(user_ids)).all()
    valid_user_ids = set([u.id for u in users])

    grouped_follows = {}

    for follow in follows:
        if (follow.follower_id not in valid_user_ids) or (follow.followed_id not in valid_user_ids):
            continue
        if not follow.follower_id in grouped_follows:
            grouped_follows[follow.follower_id] =  {
                'followers': [],
                'following': []
                }
        if not follow.followed_id in grouped_follows:
            grouped_follows[follow.followed_id] =  {
                'followers': [],
                'following': []
                }

        grouped_follows[follow.follower_id]['following'].append(str(follow.followed_id))
        grouped_follows[follow.followed_id]['followers'].append(str(follow.follower_id))

    for user_id, data in grouped_follows.items():
        follower_action = {
            '_index': FSC.FOLLOW_SEARCH_INDEX_NAME,
            '_type': FSC.FOLLOW_SEARCH_MAPPING_NAME,
            '_id': str(user_id),
            '_op_type': 'update',
            'script': {
                'inline': 'ctx._source.followers += follower',
                'params': {
                    'follower': data['followers']
                    },
                'lang': 'groovy'
            }
        }

        followed_action = {
            '_index': FSC.FOLLOW_SEARCH_INDEX_NAME,
            '_type': FSC.FOLLOW_SEARCH_MAPPING_NAME,
            '_id': str(user_id),
            '_op_type': 'update',
            'script': {
                'inline': 'ctx._source.following += user_being_followed',
                'params': {
                    'user_being_followed': data['following']
                    },
                'lang': 'groovy'
            }
        }

        docs += [follower_action, followed_action]

    print docs
    if docs:
        helpers.bulk(es, docs, request_timeout=300)

我目前正在shell中运行它。我甚至提出了两行:

global payload
payload = docs

在shell运行中

# after the above function fails
In [96]: helpers.bulk(es, payload)
Out[96]: (4, [])

这样有效吗?有效载荷?功能相同?刚才它还存在于功能之外?鉴于这些问题,我甚至无法确信这个图书馆会在制作中发挥作用。

1 个答案:

答案 0 :(得分:0)

如果没有其他信息,这很难调试,您是否可以启用elasticsearch记录器的日志记录并将输出粘贴到某处,因为它应包含完整信息,包括发送到elasticsearch的实际数据?感谢