Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

add more complex sort option (geo_distance eg) #208

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Builders/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ class FilterBuilder extends Builder
public $wheres = [
'must' => [],
'must_not' => [],
'should' => [],
];


/**
* The condition array.
*
* @var array
*/
public $functionScore = [
'query' => [],
'functions' => [],
];

/**
Expand Down Expand Up @@ -341,6 +353,42 @@ public function whereGeoBoundingBox($field, array $value)
return $this;
}

/**
* Add a whereShould condition.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html Geo bounding box query
*
* @param string $field
* @param $value
* @return $this
*/
public function whereShould($field, $value)
{
$this->wheres['should'][] = [
'match' => [
$field => $value
],
];

return $this;
}


/**
* Add a function scroe query
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html Geo bounding box query
*
* @param array $value
* @return $this
*/
public function whereFunctionScore($value)
{
$this->functionScore['functions'][] = $value;

return $this;
}

/**
* Add a whereGeoPolygon condition.
*
Expand Down Expand Up @@ -401,6 +449,18 @@ public function orderBy($field, $direction = 'asc')
return $this;
}

/**
* Add complex sort array.
* @param array $sort
* @return $this
*/
public function sort($sort)
{
$this->orders[] = $sort;

return $this;
}

/**
* Explain the request.
*
Expand Down
55 changes: 44 additions & 11 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio
$payload = new TypePayload($builder->model);

if (is_callable($rule)) {
$payload->setIfNotEmpty('body.query.bool', call_user_func($rule, $builder));
if(count($builder->functionScore['functions']) === 0){
$payload->setIfNotEmpty('body.query.bool', call_user_func($rule, $builder));
}else{
$payload->setIfNotEmpty('body.query', []);
}
} else {
/** @var SearchRule $ruleEntity */
$ruleEntity = new $rule($builder);

if ($ruleEntity->isApplicable()) {
$payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload());
if(count($builder->functionScore['functions']) === 0){
$payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload());
}else{
$payload->setIfNotEmpty('body.query', []);
}

if ($options['highlight'] ?? true) {
$payload->setIfNotEmpty('body.highlight', $ruleEntity->buildHighlightPayload());
Expand All @@ -125,8 +133,13 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio
$payloadCollection->push($payload);
}
} else {
$payload = (new TypePayload($builder->model))
->setIfNotEmpty('body.query.bool.must.match_all', new stdClass());
if(count($builder->functionScore['functions']) === 0){
$payload = (new TypePayload($builder->model))
->setIfNotEmpty('body.query.bool.must.match_all', new stdClass());
}else{
$payload = (new TypePayload($builder->model))
->setIfNotEmpty('body.query', []);
}

$payloadCollection->push($payload);
}
Expand All @@ -141,17 +154,33 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio
->setIfNotNull('body.from', $builder->offset)
->setIfNotNull('body.size', $builder->limit);

foreach ($builder->wheres as $clause => $filters) {
$clauseKey = 'body.query.bool.filter.bool.'.$clause;
if(count($builder->functionScore['functions']) > 0){
$payload->setIfNotEmpty('body.query.function_score', $builder->functionScore);
foreach ($builder->wheres as $clause => $filters) {
$clauseKey = 'body.query.function_score.query.bool.'.$clause;

$clauseValue = array_merge(
$payload->get($clauseKey, []),
$filters
);
$clauseValue = array_merge(
$payload->get($clauseKey, []),
$filters
);

$payload->setIfNotEmpty($clauseKey, $clauseValue);
}
}
else{
foreach ($builder->wheres as $clause => $filters) {
$clauseKey = 'body.query.bool.filter.bool.'.$clause;

$clauseValue = array_merge(
$payload->get($clauseKey, []),
$filters
);

$payload->setIfNotEmpty($clauseKey, $clauseValue);
$payload->setIfNotEmpty($clauseKey, $clauseValue);
}
}


return $payload->get();
});
}
Expand Down Expand Up @@ -325,6 +354,10 @@ public function map(Builder $builder, $results, $model)
$model->highlight = new Highlight($hit['highlight']);
}

if (isset($hit['sort'])) {
$model->sort = $hit['sort'];
}

return $model;
}
})
Expand Down