Explorar el Código

tried to boost sapi query with termid, do not work

bach hace 3 años
padre
commit
8fd57d2579

+ 3 - 3
config/sync/search_api.index.database.yml

@@ -9,12 +9,12 @@ dependencies:
     - search_api
     - address
   config:
-    - field.storage.node.field_manufacturer
-    - field.storage.taxonomy_term.field_public_address
+    - field.storage.node.body
     - field.storage.node.field_distributor
+    - field.storage.taxonomy_term.field_public_address
     - field.storage.node.field_famille
+    - field.storage.node.field_manufacturer
     - field.storage.node.field_reference
-    - field.storage.node.body
     - field.storage.node.field_tags
     - field.storage.taxonomy_term.field_synonyms
     - field.storage.node.field_thesaurus

+ 27 - 1
web/modules/custom/materio_sapi/materio_sapi.module

@@ -6,7 +6,8 @@
  */
 
 use Drupal\Core\Routing\RouteMatchInterface;
-
+use Solarium\QueryType\Select\Query\Query;
+use Drupal\search_api\Query\QueryInterface;
 /**
  * Implements hook_help().
  */
@@ -22,3 +23,28 @@ function materio_sapi_help($route_name, RouteMatchInterface $route_match) {
     default:
   }
 }
+
+/**
+ * Implements hook_search_api_solr_query_alter();
+ */
+function materio_sapi_search_api_solr_query_alter(Query $solarium_query,  QueryInterface $query) {
+  if ($termid = (int)$query->getOption('termid')) {
+    // get solarium fileds name
+    $index = $query->getIndex();
+    $solrFields = $index->getServerInstance()
+      ->getBackend()
+      ->getSolrFieldNames($index);
+
+    // tag_tid"itm_tag_tid"
+    // thesaurus_tid"itm_thesaurus_tid"
+    $tag_fname = $solrFields['tag_tid'];
+    $thes_fname = $solrFields['thesaurus_tid'];
+    // $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)");
+    // $bfparam = "if(or(gt(termfreq({$tag_fname},{$termid}),0),gt(termfreq({$thes_fname},{$termid}),0)),^21,0)";
+    $bfparam = "if(or(exists(query({$tag_fname}:{$termid})),exists(query({$thes_fname}:{$termid}))),^21,0)";
+    // boost=if(or(exists(query(itm_tag_tid:396)),exists(query(itm_thesaurus_tid:396))),^21,0 )
+    $solarium_query->addParam('boost', $bfparam);
+    // $solarium_query->addParam('debugQuery', 'on');
+  }
+
+}

+ 35 - 10
web/modules/custom/materio_sapi/src/Controller/Base.php

@@ -24,10 +24,31 @@ class Base extends ControllerBase {
 
   private function sapiQuery(){
     // https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
+    // 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
+
     $this->index = Index::load('database');
+
+    // // get solarium fileds name
+    // $solrFields = $this->index->getServerInstance()
+    //   ->getBackend()
+    //   ->getSolrFieldNames($this->index);
+    //
+    // // tag_tid"itm_tag_tid"
+    // // thesaurus_tid"itm_thesaurus_tid"
+    // $taxoSolrFieldsName = [];
+    // foreach (['tag_tid', 'thesaurus_tid'] as $f) {
+    //   $taxoSolrFieldsName[] = $solrFields[$f];
+    // }
+
     $this->query = $this->index->query();
 
     // Change the parse mode for the search.
+    // Les différentes possibilités sont
+    //  - « direct » => Requête directe
+    //  - « terms » => Multiple words
+    //  - « phrase » => Single phrase
+    //  - " edismax " => ???
     $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
       ->createInstance('direct');
     $parse_mode->setConjunction('OR');
@@ -48,17 +69,21 @@ class Base extends ControllerBase {
     }
     // in case of term id provided restrict the keys to taxo fields
     if ($this->term) {
-      $term_conditions = $this->query->createConditionGroup('OR');
-      $term = (int) $this->term;
-      foreach (['tag_tid', 'thesaurus_tid'] as $field) {
-        $term_conditions->addCondition($field, $term);
-      }
-      // foreach (['tag_name', 'thesaurus_name'] as $field) {
-      //   $term_conditions->addCondition($field, $this->keys);
+      // $term_conditions = $this->query->createConditionGroup('OR');
+      // $term = (int) $this->term;
+      // foreach (['tag_tid', 'thesaurus_tid'] as $field) {
+      //   $term_conditions->addCondition($field, $term);
       // }
-      // $term_conditions->addCondition('tag_name', $this->keys);
-      // $term_conditions->addCondition('thesaurus_name', $this->keys);
-      $this->query->addConditionGroup($term_conditions);
+      // $this->query->addConditionGroup($term_conditions);
+
+      // INSTEAD TRY TO BOOST TTHE TAG AND THESAURUS FIELDS
+      // foreach ($taxoSolrFieldsName as $fname) {
+      //   // $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)");
+      //   $bfparam = "if(gt(termfreq({$fname},{$this->term}),0),^21,0)";
+      //   $this->query->addParam('bf', $bfparam);
+      // }
+
+      $this->query->setOption('termid', $this->term);
     }
 
     // Restrict the search to specific languages.