From 18e20e08f167311441841fc4a55e1e39c2939020 Mon Sep 17 00:00:00 2001 From: airycanon Date: Mon, 11 Jun 2018 17:25:06 +0800 Subject: [PATCH 1/2] Search_after support. --- src/DSL/SearchBuilder.php | 15 +++++++++++++++ src/Fillers/EloquentFiller.php | 5 +++++ tests/DSL/SearchBuilderTest.php | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/DSL/SearchBuilder.php b/src/DSL/SearchBuilder.php index 362e67f..c8231fd 100644 --- a/src/DSL/SearchBuilder.php +++ b/src/DSL/SearchBuilder.php @@ -683,6 +683,21 @@ public function nested($field, \Closure $closure, $score_mode = 'avg') return $this; } + /** + * Set a search_after query. + * + * @param $value + * @return $this + */ + public function searchAfter($value) + { + $values = is_array($value) ? $value : func_get_args(); + $this->query->setFrom(-1); + $this->query->setSearchAfter($values); + + return $this; + } + /** * Add aggregation. * diff --git a/src/Fillers/EloquentFiller.php b/src/Fillers/EloquentFiller.php index 714ace2..d585219 100644 --- a/src/Fillers/EloquentFiller.php +++ b/src/Fillers/EloquentFiller.php @@ -72,6 +72,11 @@ public function fillModel(Model $model, $hit = []) $instance->highlight = $hit['highlight']; } + //set sort for search_after query + if(isset($hit['sort'])) { + $instance->sort = $hit['sort']; + } + return $instance; } diff --git a/tests/DSL/SearchBuilderTest.php b/tests/DSL/SearchBuilderTest.php index a2dda60..8dc0c58 100644 --- a/tests/DSL/SearchBuilderTest.php +++ b/tests/DSL/SearchBuilderTest.php @@ -529,6 +529,19 @@ public function it_set_a_field_function_score() ], $builder->toDSL()); } + /** + * @test + */ + public function it_set_a_search_after_query() + { + $builder = $this->getBuilder(); + + $time = time(); + $builder->searchAfter($time); + + $this->assertEquals(['from' => -1, 'search_after' => [$time]], $builder->toDSL()); + } + /** * @test */ From bd22992a4f847846ab01aa6229321382ff44233f Mon Sep 17 00:00:00 2001 From: airycanon Date: Mon, 11 Jun 2018 17:27:15 +0800 Subject: [PATCH 2/2] Format. --- src/DSL/SearchBuilder.php | 1 + src/Fillers/EloquentFiller.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/DSL/SearchBuilder.php b/src/DSL/SearchBuilder.php index c8231fd..55383b2 100644 --- a/src/DSL/SearchBuilder.php +++ b/src/DSL/SearchBuilder.php @@ -687,6 +687,7 @@ public function nested($field, \Closure $closure, $score_mode = 'avg') * Set a search_after query. * * @param $value + * * @return $this */ public function searchAfter($value) diff --git a/src/Fillers/EloquentFiller.php b/src/Fillers/EloquentFiller.php index d585219..296f61f 100644 --- a/src/Fillers/EloquentFiller.php +++ b/src/Fillers/EloquentFiller.php @@ -73,7 +73,7 @@ public function fillModel(Model $model, $hit = []) } //set sort for search_after query - if(isset($hit['sort'])) { + if (isset($hit['sort'])) { $instance->sort = $hit['sort']; }