From fa0c70ccd814fd87c2e8c41ce4124ec59674c657 Mon Sep 17 00:00:00 2001 From: bach Date: Wed, 27 Oct 2021 00:20:07 +0200 Subject: [PATCH] modified custom solr field types, fixed the reference search --- ...field_type.text_string_ascii_und_6_0_0.yml | 8 ++-- .../materio_sapi/src/Controller/Base.php | 44 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/web/modules/custom/materio_sapi/config/install/search_api_solr.solr_field_type.text_string_ascii_und_6_0_0.yml b/web/modules/custom/materio_sapi/config/install/search_api_solr.solr_field_type.text_string_ascii_und_6_0_0.yml index 262cfb9..1997f95 100644 --- a/web/modules/custom/materio_sapi/config/install/search_api_solr.solr_field_type.text_string_ascii_und_6_0_0.yml +++ b/web/modules/custom/materio_sapi/config/install/search_api_solr.solr_field_type.text_string_ascii_und_6_0_0.yml @@ -21,14 +21,14 @@ field_type: filters: - class: solr.LowerCaseFilterFactory - - - class: solr.RemoveDuplicatesTokenFilterFactory - class: solr.ASCIIFoldingFilterFactory preserveOriginal: true - class: solr.WordDelimiterGraphFilterFactory preserveOriginal: 1 + - + class: solr.RemoveDuplicatesTokenFilterFactory - type: query tokenizer: @@ -36,13 +36,13 @@ field_type: filters: - class: solr.LowerCaseFilterFactory - - - class: solr.RemoveDuplicatesTokenFilterFactory - class: solr.ASCIIFoldingFilterFactory preserveOriginal: true - class: solr.WordDelimiterGraphFilterFactory preserveOriginal: 1 + - + class: solr.RemoveDuplicatesTokenFilterFactory solr_configs: {} text_files: {} diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index ab8eaf8..eee3ab4 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -27,6 +27,14 @@ class Base extends ControllerBase { // https://www.hashbangcode.com/article/drupal-8-date-search-boosting-search-api-and-solr-search // https://kgaut.net/blog/2018/drupal-8-search-api-effectuer-une-requete-dans-le-code.html + + // Otherwise, if you use Solr, you can also set the parse mode to “Direct query” + // and use something like 'tm_name:alex AND tm_surname:john' as the keywords (field names may vary). + // That will also ensure scoring works for the keywords (the other way just adds filters, which don’t affect scoring). + // https://www.drupal.org/project/search_api/issues/3049097 + + + $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); $this->index = Index::load('database'); @@ -45,15 +53,7 @@ class Base extends ControllerBase { $this->and_query->setParseMode($parse_mode); // Set fulltext search keywords and fields. $this->and_query->keys($this->keys); - // $this->and_query->setFulltextFields(['field_reference']); - // in case we search for material references like W0117 - if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) { - $ref_conditions = $this->and_query->createConditionGroup('OR'); - foreach ($matches[0] as $key => $value) { - $ref_conditions->addCondition('field_reference', $value); - } - $this->and_query->addConditionGroup($ref_conditions); - } + // in case of term id provided restrict the keys to taxo fields if ($this->term) { $term_conditions = $this->and_query->createConditionGroup('OR'); @@ -72,8 +72,11 @@ class Base extends ControllerBase { // look @ materio_sapi_search_api_solr_query_alter in materio_sapi.module // $this->or_query->setOption('termid', $this->term); } - // FILTERS + + $fulltextFields = []; + if ($this->filters) { + // FILTERS $filters_conditions = $this->and_query->createConditionGroup('AND'); foreach ($this->filters as $filter) { $filter = (int) $filter; @@ -83,8 +86,8 @@ class Base extends ControllerBase { } $this->and_query->addConditionGroup($filters_conditions); }else{ - // Recherche uniquement sur le champ « body » - $this->and_query->setFulltextFields([ + // Recherche uniquement sur les champ thésaurus et tag + $fulltextFields += [ 'thesaurus_name_0', 'thesaurus_synonyms_0', 'thesaurus_name_1', @@ -99,10 +102,12 @@ class Base extends ControllerBase { 'thesaurus_synonyms', 'tag_name', 'tag_synonyms' - ]); + ]; + } + + if(count($fulltextFields)){ + $this->and_query->setFulltextFields($fulltextFields); } - - // Restrict the search to specific languages. $this->and_query->setLanguages([$lang]); @@ -157,6 +162,15 @@ class Base extends ControllerBase { } $this->or_query->addConditionGroup($exclude_and_results_conditions); + if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) { + // in case we search for material references like W0117 + $ref_conditions = $this->or_query->createConditionGroup('OR'); + foreach ($matches[0] as $key => $value) { + $ref_conditions->addCondition('field_reference', $value); + } + $this->or_query->addConditionGroup($ref_conditions); + } + // Restrict the search to specific languages. $this->or_query->setLanguages([$lang]);