From 9f0b257c62723f7d129d41e9d3786300d906b9a7 Mon Sep 17 00:00:00 2001 From: bach Date: Wed, 24 Feb 2021 11:39:20 +0100 Subject: [PATCH] improved sapi_solr query condition on field_reference for multiple references --- .../custom/materio_sapi/src/Controller/Base.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index 614f22e..e534eac 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -23,6 +23,7 @@ class Base extends ControllerBase { private $offset = 0; private function sapiQuery(){ + // https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code $this->index = Index::load('database'); $this->query = $this->index->query(); @@ -37,9 +38,13 @@ class Base extends ControllerBase { // $this->query->setFulltextFields(['field_reference']); // Set additional conditions. - // in case we search for material reference like W0117 - if (preg_match('/^[WTRPCMFGSO]\d{4}$/i', $this->keys, $matches)) { - $this->query->addCondition('field_reference', $this->keys); + // in case we search for material references like W0117 + if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) { + $conditions = $this->query->createConditionGroup('OR'); + foreach ($matches[0] as $key => $value) { + $conditions->addCondition('field_reference', $value); + } + $this->query->addConditionGroup($conditions); } // Restrict the search to specific languages.