improved sapi_solr query condition on field_reference for multiple references

This commit is contained in:
Bachir Soussi Chiadmi 2021-02-24 11:39:20 +01:00
parent 5dfb746132
commit 9f0b257c62
1 changed files with 8 additions and 3 deletions

View File

@ -23,6 +23,7 @@ class Base extends ControllerBase {
private $offset = 0; private $offset = 0;
private function sapiQuery(){ 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->index = Index::load('database');
$this->query = $this->index->query(); $this->query = $this->index->query();
@ -37,9 +38,13 @@ class Base extends ControllerBase {
// $this->query->setFulltextFields(['field_reference']); // $this->query->setFulltextFields(['field_reference']);
// Set additional conditions. // Set additional conditions.
// in case we search for material reference like W0117 // in case we search for material references like W0117
if (preg_match('/^[WTRPCMFGSO]\d{4}$/i', $this->keys, $matches)) { if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) {
$this->query->addCondition('field_reference', $this->keys); $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. // Restrict the search to specific languages.