我如何知道文档何时被索引?

How can I tell when documents have been indexed?

我昨天问的 的一些后续内容,这表明 W10 中的 Elasticsearch-as-a-service 在服务启动后需要一定的有限时间来允许请求,甚至在几秒钟后Elasticsearch 对象实际上已经在 Python 脚本中传递,我现在发现如果我将文档添加到索引并立即查询索引我得不到任何结果(但如果我等待几秒钟我会得到预期的结果)。

我正在阅读一本关于 ES 的书,其中有一些关于索引更新每秒只发生一次的内容(该书涵盖 ES 1.7,我使用的是 7.10)。

问题是,在添加文档后,是否有一些命令我可以 运行(Python elasticsearch 模块或可能是 REST URL...)在新文档被编入索引之前不会 return ,或者在被编入索引后以某种方式指示索引中现在有多少文档?

注意我正在使用这种命令来索引:

es_obj.index( index='my_index', body=record_as_json_string )

回答

是的,您可以通过多种方式使用刷新API来实现。

例如下面会立即插入并刷新。

curl -X PUT "localhost:9200/test/_doc/1?refresh&pretty" -H 'Content-Type: application/json' -d'
{"test": "test"}
'
curl -X PUT "localhost:9200/test/_doc/2?refresh=true&pretty" -H 'Content-Type: application/json' -d'
{"test": "test"}
'

来自文档:

Refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, so that the updated document appears in search results immediately. This should ONLY be done after careful thought and verification that it does not lead to poor performance, both from an indexing and a search standpoint.

来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

你应该做吗?

保留默认设置以提供更好的性能。由于 ES 主要用于存储大型数据集,而刷新是一项成本较高的操作,每次插入后刷新可能会导致您遇到无法预料的延迟和性能问题。 上面提到的source highlights when to use what. Refer this为了更好地解释性能调整。