Browse Source

improved sapi_solr query condition on field_reference for multiple references

bach 3 years ago
parent
commit
9f0b257c62
1 changed files with 8 additions and 3 deletions
  1. 8 3
      web/modules/custom/materio_sapi/src/Controller/Base.php

+ 8 - 3
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.