diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f61454e635218b75bebb90f3108d19a4df26afae..9093cc36fd46a3906f589fd7933e9d7c429705c5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -56,13 +56,13 @@ phpunit: services: - !reference [.with-database] - !reference [.with-chrome] - - name: elasticsearch:7.17.14 + - name: elasticsearch:8.11.1 alias: elasticsearch pull_policy: if-not-present command: - /bin/bash - -c - - echo -Xms256m >> /usr/share/elasticsearch/config/jvm.options && echo -Xmx256m >> /usr/share/elasticsearch/config/jvm.options && /usr/local/bin/docker-entrypoint.sh elasticsearch -Ediscovery.type=single-node + - echo -Xms256m >> /usr/share/elasticsearch/config/jvm.options && echo -Xmx256m >> /usr/share/elasticsearch/config/jvm.options && /usr/local/bin/docker-entrypoint.sh elasticsearch -Ediscovery.type=single-node -Expack.security.enabled=false ################################################################################### # diff --git a/src/ElasticSearch/Parameters/Builder/SearchBuilder.php b/src/ElasticSearch/Parameters/Builder/SearchBuilder.php index 2ef3ba85b3ed8b2c38718bb512cec60c94bcd47a..253500052aac172cdf0d65d33a49f95383b59b07 100644 --- a/src/ElasticSearch/Parameters/Builder/SearchBuilder.php +++ b/src/ElasticSearch/Parameters/Builder/SearchBuilder.php @@ -408,6 +408,29 @@ class SearchBuilder { return $ret; } + /** + * Get args for range query. + * + * @param \Drupal\search_api\Query\ConditionInterface $condition + * Condition. + * + * @return array + * Range args. + */ + protected function getRangeArgs(ConditionInterface $condition) : array { + $field_type = $this->indexFields[$condition->getField()]?->getType() ?? ""; + + $args = [ + 'gte' => $condition->getValue()[0] ?? NULL, + 'lte' => $condition->getValue()[1] ?? NULL, + ]; + + if ($field_type == "date" && is_int($args["gte"]) && is_int($args["lte"])) { + $args["format"] = "epoch_second"; + } + return $args; + } + /** * Get query by Condition instance. * @@ -482,24 +505,17 @@ class SearchBuilder { case 'BETWEEN': $filter = new Range( $condition->getField(), - [ - 'gte' => !empty($condition->getValue()[0]) ? (float) $condition->getValue()[0] : NULL, - 'lte' => !empty($condition->getValue()[1]) ? (float) $condition->getValue()[1] : NULL, - ] + $this->getRangeArgs($condition), ); + break; case 'NOT BETWEEN': $filter = new BoolQuery(); - $filter->addMustNot( - new Range( - $condition->getField(), - [ - 'gte' => !empty($condition->getValue()[0]) ? (float) $condition->getValue()[0] : NULL, - 'lte' => !empty($condition->getValue()[1]) ? (float) $condition->getValue()[1] : NULL, - ] - ) - ); + $filter->addMustNot(new Range( + $condition->getField(), + $this->getRangeArgs($condition), + )); break; default: diff --git a/src/ElasticSearch/Parameters/Factory/MappingFactory.php b/src/ElasticSearch/Parameters/Factory/MappingFactory.php index f31c138d9f78e00b1e857433c155a35f51858b3e..b3dc60e63c13e9b8d06e833c50b45e5359acdcef 100644 --- a/src/ElasticSearch/Parameters/Factory/MappingFactory.php +++ b/src/ElasticSearch/Parameters/Factory/MappingFactory.php @@ -130,7 +130,6 @@ class MappingFactory { case 'text': $mappingConfig = [ 'type' => 'text', - 'boost' => $field->getBoost(), 'fields' => [ "keyword" => [ "type" => 'keyword', diff --git a/tests/src/Kernel/ElasticsearchTest.php b/tests/src/Kernel/ElasticsearchTest.php index f5bcca7aa09096d424056c538d0abf7020c93b39..2988781467300903cd5e6200723bab08e078cbb8 100644 --- a/tests/src/Kernel/ElasticsearchTest.php +++ b/tests/src/Kernel/ElasticsearchTest.php @@ -163,4 +163,48 @@ class ElasticsearchTest extends BackendTestBase { // Aggregations are not allowed on text. } + /** + * {@inheritdoc} + */ + protected function addTestEntity($id, array $values) { + + $created = match ($id) { + 1 => strtotime("2003-07-10"), + 2 => strtotime("2008-06-28"), + 3 => strtotime("2012-06-11"), + 4 => strtotime("2016-09-16"), + 5 => strtotime("2018-04-02"), + default => NULL, + }; + + if ($created) { + $values["created"] = $created; + } + + return parent::addTestEntity($id, $values); + } + + /** + * {@inheritdoc} + */ + protected function searchSuccess() { + parent::searchSuccess(); + + $results = $this->buildSearch() + ->addCondition("created", [ + "2008-06-27", + "2012-06-12", + ], "BETWEEN") + ->execute(); + $this->assertEquals($this->getItemIds([2, 3]), array_keys($results->getResultItems()), 'Search for created range returned correct results.'); + + $results = $this->buildSearch() + ->addCondition("created", [ + strtotime("2008-06-27"), + strtotime("2012-06-12"), + ], "BETWEEN") + ->execute(); + $this->assertEquals($this->getItemIds([2, 3]), array_keys($results->getResultItems()), 'Search for created range returned correct results.'); + } + } diff --git a/tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php b/tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php index 40eb34e1822ac099deb8aa876b2ac0fe0340a840..713dd068955013b0f104a2f8bbe04550bab4e81b 100644 --- a/tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php +++ b/tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php @@ -79,7 +79,6 @@ class MappingFactoryTest extends UnitTestCase { ], $field1Id => [ 'type' => 'text', - 'boost' => 1.1, 'fields' => [ 'keyword' => ['type' => 'keyword', 'ignore_above' => 256], ],