简体   繁体   English

我怎么知道弹性搜索中的滚动是否有效

[英]how can I know that the scroll in elasticsearch if it works

I'm using elasticsearch-php. 我正在使用elasticsearch-php。

I use this code, in the tests that I do it works well. 我使用此代码,在我做的测试中效果很好。

require_once 'app/init.php';

if (isset($_GET['q'])) {
                $q = $_GET['q'];
                $query= $es->search([
                      'index' => 'rebajas',
                      //'search_type' => 'scan',
                      'scroll' => '2m',
                      //'from' => 0,
                      'size' => 1000,
                      'body'=>[
                        'query'=>[
                          'bool' => [
                            'should' => [
                              'match'=>['titulo'=>$q],

                            ]
                          ]

                        ]

                      ]


                    ]);
                  if ($query['hits']['total']>=1) {

                      $scroll_size = $query['hits']['total'];
                      $results = $query['hits']['hits'];

                      //scrol
                      $scroll_id = $query['_scroll_id'];


                        print " total results:   " . $scroll_size;
                        $count = 0;
                        // first set of scroll results
                        for ($i=0; $i<$scroll_size; $i++) {
                            $count++;
                        }
                        //scroll
                        while (isset($query['hits']['hits']) && count($query['hits']['hits']) > 0) {

                            // **
                            // Do your work here, on the $response['hits']['hits'] array
                            // **
                            $conta =0;
                            foreach ($results as $r ) {
                                $conta++;

                        ?>

                        <h1 class="mt-5"><a href=""><?php echo $conta." " .$r['_source']['titulo'];?></a> <i class="fa fa-search-plus" aria-hidden="true"></i></h1>


                        <?php
                            }

                            // When done, get the new scroll_id
                            // You must always refresh your _scroll_id!  It can change sometimes
                            $scroll_id = $query['_scroll_id'];

                            // Execute a Scroll request and repeat
                            $query = $es->scroll([
                                    "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
                                    "scroll" => "30s"           // and the same timeout window
                                ]
                            );
                        }


                    }

                }

The questions are: How does this scroll work, when it is in production, extracting about ten thousand records, will it not block the server with so many records? 问题是:此滚动条在生产时如何工作,提取大约一万条记录,会不会阻塞这么多记录的服务器? How can I check what is it loading? 我如何检查正在加载什么? This works the same as infinite scroll, that is to say, as I go down the page, the records are loaded. 这与无限滚动的工作原理相同,也就是说,当我向下浏览页面时,将加载记录。

Is this question how to implement an infinite scroll that is backed by elasticsearch? 这是一个问题吗,如何实现由elasticsearch支持的无限滚动? That's a whole class/framework worth of questions. 这是整个课堂/框架中值得提出的问题。 But let me take a stab at it. 但是,让我刺一下它。

If you want an infinite scroll you'll probably want to first break out your data APIs from your webserving endpoints (ie the data shouldn't load with your page [you'll probably want your first page to get served with your webpage, but it doesn't have to and for loading next page you'll almost certainly want to have that as its own API]). 如果您想无限滚动,则可能需要先从Web服务端点中断开数据API(即,数据不应该随页面一起加载[您可能希望第一页随页面一起提供,但是它不需要,并且要加载下一页,您几乎肯定会希望将其作为自己的API])。 Then you'll need to your AJAX to request pages as the user scrolls down your webpage (ie if user scrolls to n / PAGE_SIZE then make request for next page). 然后,当用户向下滚动网页时,您将需要AJAX请求页面(即,如果用户滚动到n / PAGE_SIZE,则请求下一页)。 You can basically google 'infinite scroll implementation' but this captures the intention pretty well . 您基本上可以使用Google“无限滚动实现”,但这可以很好地捕捉意图

That being said this is generally not a great use case for elasticsearch (using ES as a direct infinite scroll backing for a website). 话虽这么说,对于Elasticsearch(将ES用作网站的直接无限滚动支持),这通常不是一个好用例。 Most of the reasons why are encapsulated here . 大部分原因都封装在这里 It basically comes down to the fact that having many open scrolls is expensive in elasticsearch though if you have few users and a strict timeout it may be ok? 基本上可以归结为这样的事实:在弹性搜索中拥有很多打开的滚动条是昂贵的,尽管如果您的用户很少并且超时很严格,那可以吗? Scrolls were not intended for 'real-time' uses but more for data export use cases. 滚动条不是用于“实时”使用的,而是用于数据导出用例的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM