Skip to content

Commit

Permalink
Merge pull request #70 from CheapHasz/search-after
Browse files Browse the repository at this point in the history
Search after
  • Loading branch information
CheapHasz authored Mar 20, 2019
2 parents adc9da1 + 149526a commit 98ba7f7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class QueryBuilder
/** @var array */
protected $queryBody;

/** @var Filter[] */
/**
* @var Filter[]
* @deprecated
*/
protected $filterCollection;
/** @var Clause */
protected $postFilter;
Expand All @@ -41,6 +44,11 @@ class QueryBuilder
protected $scriptScore;
/** @var null|FunctionScoreOptions */
protected $functionsScoreOptions;
/**
* @var array
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html
*/
protected $searchAfter;

public function __construct($offset = self::DEFAULT_OFFSET, $limit = self::DEFAULT_LIMIT, $minScore = self::DEFAULT_MIN_SCORE)
{
Expand All @@ -51,6 +59,7 @@ public function __construct($offset = self::DEFAULT_OFFSET, $limit = self::DEFAU
$this->aggregationCollection = [];
$this->functionScoreCollection = [];
$this->scriptFieldCollection = [];
$this->searchAfter = [];

$this->queryBody['from'] = $offset;
$this->queryBody['size'] = $limit;
Expand Down Expand Up @@ -251,6 +260,16 @@ public function setFunctionsScoreOptions(FunctionScoreOptions $functionsScoreOpt
$this->functionsScoreOptions = $functionsScoreOptions;
}

public function getSearchAfter(): array
{
return $this->searchAfter;
}

public function setSearchAfter(array $searchAfter)
{
$this->searchAfter = $searchAfter;
}

public function hasNoMatchingQueries(): bool
{
$nonFilterQueries = array_filter($this->getClauseCollection(), function (Clause $clause) {
Expand Down Expand Up @@ -299,6 +318,10 @@ public function getQueryBody(): array
$queryBody['post_filter'] = $this->postFilter->formatForQuery();
}

if ($this->searchAfter) {
$queryBody['search_after'] = $this->searchAfter;
}

return array_merge($this->queryBody, $queryBody);
}
}
28 changes: 26 additions & 2 deletions test/functional/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,29 @@ public function iBuildAQueryMatching(TableNode $queryTable)
}
}

/**
* @Given I add sorting on :
*/
public function iAddSortingOn(TableNode $queryTable)
{
$this->queryBuilder = $this->queryBuilder ?? QueryBuilder::createNew();
$queryHash = $queryTable->getHash();
foreach ($queryHash as $queryRow) {
$this->queryBuilder->addSort($queryRow['field'], $queryRow['order']);
}
}

/**
* @Given I add search after :
*/
public function iAddSearchAfter(TableNode $queryTable)
{
$this->queryBuilder = $this->queryBuilder ?? QueryBuilder::createNew();
$queryHash = $queryTable->getHash();
$this->queryBuilder->setSearchAfter(array_column($queryHash,'sort'));
}


/**
* @Given I build the query with filter :
* @Given I build a query with filter :
Expand Down Expand Up @@ -306,8 +329,9 @@ public function theResultShouldContainExactlyIds($idList)
$foundCount += in_array($hit['id'], $idList) ? 1 : 0;
}

$this->assert->integer($this->result->totalHits())->isEqualTo(count($idList));
$this->assert->integer($foundCount)->isEqualTo(count($idList));

$this->assert->integer(\count($this->result->hits()))->isEqualTo(\count($idList));
$this->assert->integer($foundCount)->isEqualTo(\count($idList));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/functional/bootstrap/data/config_my_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ settings:
mappings:
_doc:
properties:
id:
type: keyword
first_name:
type: text
analyzer: standard
Expand Down
15 changes: 14 additions & 1 deletion test/functional/features/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Feature: Search on index
| first_name | Diana | must |
When I execute it on the index named "my_index" for type "_doc"
Then the result should contain exactly ids "[2]"

Scenario: Combining queries and filters inside a Bool Query
Given I build a should bool query with :
| field | value | condition |
Expand Down Expand Up @@ -336,4 +337,16 @@ Feature: Search on index
Given I create geo objects of type "_doc" to index "my_geoindex"
And I search cities with a relation "disjoint" to rhône
When I execute it on the index named "my_geoindex" for type "_doc"
Then the result should contain exactly ids "[2;3]"
Then the result should contain exactly ids "[2;3]"

Scenario: After search
Given I add sorting on :
| field | order |
| age | asc |
| id | asc |
And I add search after :
| sort |
| 35 |
| 5 |
When I execute it on the index named "my_index" for type "_doc"
Then the result should contain exactly ids "[6;3;2]"

0 comments on commit 98ba7f7

Please sign in to comment.