在 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)
我目前正在外壳中运行它。我什至放了两行:
global payload
payload = docs
并在 shell 中运行
# after the above function fails
In [96]: helpers.bulk(es, payload)
Out[96]: (4, [])
所以它有效吗?相同的有效载荷?一样的功能?刚才它住在函数之外?考虑到这些问题,我什至不能确信这个库会在生产中工作。