简单保存记录到没有objectID的algolia

时间:2016-01-11 05:41:04

标签: python algolia

我试图在没有objectID的情况下将新对象保存到我的Algolia索引,但由于某种原因它会返回此错误:

Traceback (most recent call last):
  File "search.py", line 9, in <module>
    "picture": "pic.jpg"})
  File "C:\Python27\lib\site-packages\algoliasearch\index.py", line 212, in save_object
    path = '/%s' % safe(obj['objectID'])
KeyError: 'objectID'

我使用的python代码(基于docs):

from algoliasearch import algoliasearch

client = algoliasearch.Client("***", "***")
algolia_var = client.init_index('*****')


res = algolia_var.save_object({"age": "new",
    "categories": "new",
    "picture": "pic.jpg"})

任何人都可以向我解释我做错了什么吗?它不应该自动生成objectID吗?

1 个答案:

答案 0 :(得分:1)

save_object更新现有对象。您需要方法add_object将此对象添加到索引并生成id。

res = algolia_var.add_object({"age": "new", "categories": "new", "picture": "pic.jpg"})