Skip to content

Commit

Permalink
Keep sortable fields in mind for Elasticsearch/Opensearch and use boo…
Browse files Browse the repository at this point in the history
…l filter instead of bool must (#72)
  • Loading branch information
alexander-schranz authored Jan 13, 2023
1 parent 01617ac commit 5752b25
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
12 changes: 6 additions & 6 deletions packages/seal-elasticsearch-adapter/ElasticsearchConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public function search(Search $search): Result
match (true) {
$filter instanceof Condition\IdentifierCondition => $query['ids']['values'][] = $filter->identifier,
$filter instanceof Condition\SearchCondition => $query['query_string']['query'] = $filter->query,
$filter instanceof Condition\EqualCondition => $query['bool']['must'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\NotEqualCondition => $query['bool']['must_not'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\GreaterThanCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gt'] = $filter->value,
$filter instanceof Condition\GreaterThanEqualCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gte'] = $filter->value,
$filter instanceof Condition\LessThanCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lt'] = $filter->value,
$filter instanceof Condition\LessThanEqualCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lte'] = $filter->value,
$filter instanceof Condition\EqualCondition => $query['bool']['filter'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\NotEqualCondition => $query['bool']['filter']['bool']['must_not'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\GreaterThanCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gt'] = $filter->value,
$filter instanceof Condition\GreaterThanEqualCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gte'] = $filter->value,
$filter instanceof Condition\LessThanCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lt'] = $filter->value,
$filter instanceof Condition\LessThanEqualCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lte'] = $filter->value,
default => throw new \LogicException($filter::class . ' filter not implemented.'),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function createPropertiesMapping(array $fields): array
$field instanceof Field\IdentifierField => $properties[$name] = [
'type' => 'keyword',
'index' => $field->searchable,
'doc_values' => $field->filterable || $field->sortable,
],
$field instanceof Field\TextField => $properties[$name] = \array_replace([
'type' => 'text',
Expand All @@ -85,21 +86,22 @@ private function createPropertiesMapping(array $fields): array
$field instanceof Field\BooleanField => $properties[$name] = [
'type' => 'boolean',
'index' => $field->searchable,
'doc_values' => $field->filterable || $field->sortable,
],
$field instanceof Field\DateTimeField => $properties[$name] = [
'type' => 'date',
'index' => $field->searchable,
'doc_values' => $field->filterable,
'doc_values' => $field->filterable || $field->sortable,
],
$field instanceof Field\IntegerField => $properties[$name] = [
'type' => 'integer',
'index' => $field->searchable,
'doc_values' => $field->filterable,
'doc_values' => $field->filterable || $field->sortable,
],
$field instanceof Field\FloatField => $properties[$name] = [
'type' => 'float',
'index' => $field->searchable,
'doc_values' => $field->filterable,
'doc_values' => $field->filterable || $field->sortable,
],
$field instanceof Field\ObjectField => $properties[$name] = [
'type' => 'object',
Expand Down
12 changes: 6 additions & 6 deletions packages/seal-opensearch-adapter/OpensearchConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public function search(Search $search): Result
match (true) {
$filter instanceof Condition\IdentifierCondition => $query['ids']['values'][] = $filter->identifier,
$filter instanceof Condition\SearchCondition => $query['query_string']['query'] = $filter->query,
$filter instanceof Condition\EqualCondition => $query['bool']['must'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\NotEqualCondition => $query['bool']['must_not'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\GreaterThanCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gt'] = $filter->value,
$filter instanceof Condition\GreaterThanEqualCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gte'] = $filter->value,
$filter instanceof Condition\LessThanCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lt'] = $filter->value,
$filter instanceof Condition\LessThanEqualCondition => $query['bool']['must'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lte'] = $filter->value,
$filter instanceof Condition\EqualCondition => $query['bool']['filter'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\NotEqualCondition => $query['bool']['filter']['bool']['must_not'][]['term'][$this->getFilterField($search->indexes, $filter->field)]['value'] = $filter->value,
$filter instanceof Condition\GreaterThanCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gt'] = $filter->value,
$filter instanceof Condition\GreaterThanEqualCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['gte'] = $filter->value,
$filter instanceof Condition\LessThanCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lt'] = $filter->value,
$filter instanceof Condition\LessThanEqualCondition => $query['bool']['filter'][]['range'][$this->getFilterField($search->indexes, $filter->field)]['lte'] = $filter->value,
default => throw new \LogicException($filter::class . ' filter not implemented.'),
};
}
Expand Down

0 comments on commit 5752b25

Please sign in to comment.