materio_sapi.module 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * Contains materio_sapi.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Solarium\QueryType\Select\Query\Query;
  8. use Drupal\search_api\Query\QueryInterface;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function materio_sapi_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. // Main module help for the materio_sapi module.
  15. case 'help.page.materio_sapi':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('Search Api Materio module') . '</p>';
  19. return $output;
  20. default:
  21. }
  22. }
  23. /**
  24. * Implements hook_search_api_solr_query_alter();
  25. */
  26. function materio_sapi_search_api_solr_query_alter(Query $solarium_query, QueryInterface $query) {
  27. if ($termid = (int)$query->getOption('termid')) {
  28. // get solarium fileds name
  29. $index = $query->getIndex();
  30. $solrFields = $index->getServerInstance()
  31. ->getBackend()
  32. ->getSolrFieldNames($index);
  33. // tag_tid"itm_tag_tid"
  34. // thesaurus_tid"itm_thesaurus_tid"
  35. $tag_fname = $solrFields['tag_tid'];
  36. $thes_fname = $solrFields['thesaurus_tid'];
  37. // $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)");
  38. // $bfparam = "if(or(gt(termfreq({$tag_fname},{$termid}),0),gt(termfreq({$thes_fname},{$termid}),0)),^21,0)";
  39. $bfparam = "if(or(exists(query({$tag_fname}:{$termid})),exists(query({$thes_fname}:{$termid}))),^21,0)";
  40. // boost=if(or(exists(query(itm_tag_tid:396)),exists(query(itm_thesaurus_tid:396))),^21,0 )
  41. $solarium_query->addParam('boost', $bfparam);
  42. // $solarium_query->addParam('debugQuery', 'on');
  43. }
  44. }