<bdo id='LdPx5'></bdo><ul id='LdPx5'></ul>
<i id='LdPx5'><tr id='LdPx5'><dt id='LdPx5'><q id='LdPx5'><span id='LdPx5'><b id='LdPx5'><form id='LdPx5'><ins id='LdPx5'></ins><ul id='LdPx5'></ul><sub id='LdPx5'></sub></form><legend id='LdPx5'></legend><bdo id='LdPx5'><pre id='LdPx5'><center id='LdPx5'></center></pre></bdo></b><th id='LdPx5'></th></span></q></dt></tr></i><div id='LdPx5'><tfoot id='LdPx5'></tfoot><dl id='LdPx5'><fieldset id='LdPx5'></fieldset></dl></div>

    <small id='LdPx5'></small><noframes id='LdPx5'>

    <tfoot id='LdPx5'></tfoot>
  1. <legend id='LdPx5'><style id='LdPx5'><dir id='LdPx5'><q id='LdPx5'></q></dir></style></legend>
    1. ElasticSearch 更新不是即时的,你如何等待 ElasticSearch 完成更新它的索引?

      时间:2023-09-28

        <bdo id='suGZV'></bdo><ul id='suGZV'></ul>

        <small id='suGZV'></small><noframes id='suGZV'>

          <tfoot id='suGZV'></tfoot>
        • <legend id='suGZV'><style id='suGZV'><dir id='suGZV'><q id='suGZV'></q></dir></style></legend>

              <tbody id='suGZV'></tbody>
            <i id='suGZV'><tr id='suGZV'><dt id='suGZV'><q id='suGZV'><span id='suGZV'><b id='suGZV'><form id='suGZV'><ins id='suGZV'></ins><ul id='suGZV'></ul><sub id='suGZV'></sub></form><legend id='suGZV'></legend><bdo id='suGZV'><pre id='suGZV'><center id='suGZV'></center></pre></bdo></b><th id='suGZV'></th></span></q></dt></tr></i><div id='suGZV'><tfoot id='suGZV'></tfoot><dl id='suGZV'><fieldset id='suGZV'></fieldset></dl></div>
              • 本文介绍了ElasticSearch 更新不是即时的,你如何等待 ElasticSearch 完成更新它的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试提高针对 ElasticSearch 进行测试的套件的性能.

                I'm attempting to improve performance on a suite that tests against ElasticSearch.

                测试需要很长时间,因为 Elasticsearch 不会在更新后立即更新它的索引.例如,以下代码运行时不会引发断言错误.

                The tests take a long time because Elasticsearch does not update it's indexes immediately after updating. For instance, the following code runs without raising an assertion error.

                from elasticsearch import Elasticsearch
                elasticsearch = Elasticsearch('es.test')
                
                # Asumming that this is a clean and empty elasticsearch instance
                elasticsearch.update(
                     index='blog',
                     doc_type=,'blog'
                     id=1,
                     body={
                        ....
                    }
                )
                
                results = elasticsearch.search()
                assert not results
                # results are not populated
                

                目前针对此问题的共同解决方案是将 time.sleep 调用放入代码中,以给 ElasticSearch 一些时间来更新其索引.

                Currently out hacked together solution to this issue is dropping a time.sleep call into the code, to give ElasticSearch some time to update it's indexes.

                from time import sleep
                from elasticsearch import Elasticsearch
                elasticsearch = Elasticsearch('es.test')
                
                # Asumming that this is a clean and empty elasticsearch instance
                elasticsearch.update(
                     index='blog',
                     doc_type=,'blog'
                     id=1,
                     body={
                        ....
                    }
                )
                
                # Don't want to use sleep functions
                sleep(1)
                
                results = elasticsearch.search()
                assert len(results) == 1
                # results are now populated
                

                显然这不是很好,因为它很容易失败,假设如果 ElasticSearch 花费超过一秒的时间来更新它的索引,尽管不太可能,测试会失败.当你运行 100 次这样的测试时,它也非常慢.

                Obviously this isn't great, as it's rather failure prone, hypothetically if ElasticSearch takes longer than a second to update it's indexes, despite how unlikely that is, the test will fail. Also it's extremely slow when you're running 100s of tests like this.

                我解决问题的尝试是查询 待处理的集群作业查看是否还有任务需要完成.但是这不起作用,并且此代码将在没有断言错误的情况下运行.

                My attempt to solve the issue has been to query the pending cluster jobs to see if there are any tasks left to be done. However this doesn't work, and this code will run without an assertion error.

                from elasticsearch import Elasticsearch
                elasticsearch = Elasticsearch('es.test')
                
                # Asumming that this is a clean and empty elasticsearch instance
                elasticsearch.update(
                     index='blog',
                     doc_type=,'blog'
                     id=1,
                     body={
                        ....
                    }
                )
                
                # Query if there are any pending tasks
                while elasticsearch.cluster.pending_tasks()['tasks']:
                    pass
                
                results = elasticsearch.search()
                assert not results
                # results are not populated
                

                所以基本上,回到我原来的问题,ElasticSearch 更新不是立即,您如何等待 ElasticSearch 完成对其索引的更新?

                So basically, back to my original question, ElasticSearch updates are not immediate, how do you wait for ElasticSearch to finish updating it's index?

                推荐答案

                从 5.0.0 版本开始,elasticsearch 有一个选项:

                As of version 5.0.0, elasticsearch has an option:

                 ?refresh=wait_for
                

                关于索引、更新、删除和批量 api.这样,在 ElasticSearch 中显示结果之前,请求不会收到响应.(耶!)

                on the Index, Update, Delete, and Bulk api's. This way, the request won't receive a response until the result is visible in ElasticSearch. (Yay!)

                请参阅 https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html了解更多信息.

                See https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html for more information.

                edit:这个功能似乎已经是最新 Python elasticsearch api 的一部分:https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.index

                edit: It seems that this functionality is already part of the latest Python elasticsearch api: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.index

                将您的 elasticsearch.update 更改为:

                Change your elasticsearch.update to:

                elasticsearch.update(
                     index='blog',
                     doc_type='blog'
                     id=1,
                     refresh='wait_for',
                     body={
                        ....
                    }
                )
                

                你不应该需要任何睡眠或轮询.

                and you shouldn't need any sleep or polling.

                这篇关于ElasticSearch 更新不是即时的,你如何等待 ElasticSearch 完成更新它的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用 pysftp 仅同步远程目录中更改的文件? 下一篇:如何同时运行两个函数

                相关文章

                  <bdo id='lT1OM'></bdo><ul id='lT1OM'></ul>
                <tfoot id='lT1OM'></tfoot>

                  <i id='lT1OM'><tr id='lT1OM'><dt id='lT1OM'><q id='lT1OM'><span id='lT1OM'><b id='lT1OM'><form id='lT1OM'><ins id='lT1OM'></ins><ul id='lT1OM'></ul><sub id='lT1OM'></sub></form><legend id='lT1OM'></legend><bdo id='lT1OM'><pre id='lT1OM'><center id='lT1OM'></center></pre></bdo></b><th id='lT1OM'></th></span></q></dt></tr></i><div id='lT1OM'><tfoot id='lT1OM'></tfoot><dl id='lT1OM'><fieldset id='lT1OM'></fieldset></dl></div>
                1. <legend id='lT1OM'><style id='lT1OM'><dir id='lT1OM'><q id='lT1OM'></q></dir></style></legend>

                  <small id='lT1OM'></small><noframes id='lT1OM'>