我在Marqo中使用add_documents()添加了一个文档,但没有传递ID,现在我想获取这个文档,却不知道文档ID是什么?
我的代码如下所示:
mq = marqo.Client(url='http://localhost:8882')mq.index("my-first-index").add_documents([ { "Title": title, "Description": document_body }])
我尝试检查文档是否已添加,但;
no_of_docs = mq.index("my-first-index").get_stats()print(no_of_docs)
我得到了;
{'numberOfDocuments': 1}
这意味着文档已被添加。
回答:
如果你没有在键/值对中添加”_id”,那么Marqo默认会为你生成一个随机ID。要访问它,你可以使用文档的标题进行搜索,
doc = mq.index("my-first-index").search(title_of_your_document, searchable_attributes=['Title'])
你应该会得到一个类似这样的字典结果;
{'hits': [{'Description': your_description, 'Title': title_of_your_document, '_highlights': relevant part of the doc, '_id': 'ac14f87e-50b8-43e7-91de-ee72e1469bd3', '_score': 1.0}], 'limit': 10, 'processingTimeMs': 122, 'query': 'The Premier League'}
其中_id
部分就是你的文档ID。