Browse Source

Sorl search based on term tid and scored is functional

Bachir Soussi Chiadmi 6 years ago
parent
commit
e8586eb4da

File diff suppressed because it is too large
+ 0 - 0
sites/all/modules/gui/materiobasemod/js/dist/materio_search_api_ajax.min.js


+ 2 - 0
sites/all/modules/gui/materiobasemod/js/dist/materio_showroom.min.js

@@ -0,0 +1,2 @@
+
+(function($){Drupal.behaviors.field_materio_showroom_location={attach:function(context){$(".field-multiple-drag",".field-type-field-materio-showroom-location").hide();}}})(jQuery);

+ 9 - 9
sites/all/modules/gui/materiobasemod/js/materio_search_api_ajax.js

@@ -35,15 +35,15 @@ MaterioSearchApiAjax = function(){
     });
 
     // filtered search form
-    $('#materio-search-api-advanced-search-form').bind('submit', function(event) {
-      event.preventDefault();
-      // trace('advanced search submited', event);
-      // var $this = $(this);
-      setTimeout(function(){
-        loadResults(getSearchFilters(), 'advanced');
-      },10);
-      return false;
-    });
+    // $('#materio-search-api-advanced-search-form').bind('submit', function(event) {
+    //   event.preventDefault();
+    //   // trace('advanced search submited', event);
+    //   // var $this = $(this);
+    //   setTimeout(function(){
+    //     loadResults(getSearchFilters(), 'advanced');
+    //   },10);
+    //   return false;
+    // });
 
     // /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686
     $("#edit-searchfield")

+ 7 - 0
sites/all/modules/gui/materiobasemod/materio_search_api.admin.inc

@@ -31,6 +31,13 @@ function materio_search_api_settings(){
 			'#title' => t('Taxonomy search api index for %lang contents.', array('%lang'=>$name)),
 		);
 
+		$form['advancedsearchindex_'.$lcode] = array(
+			'#type'=>'select',
+			'#options'=>$index_options,
+			'#default_value' => variable_get('advancedsearchindex_'.$lcode, -1),
+			'#title' => t('Advanced search api index for %lang contents.', array('%lang'=>$name)),
+		);
+
 		$form['brevessearchindex_'.$lcode] = array(
 			'#type'=>'select',
 			'#options'=>$index_options,

+ 141 - 40
sites/all/modules/gui/materiobasemod/materio_search_api.module

@@ -180,30 +180,47 @@ function materio_search_api_entity_property_info_alter(&$info){
 
   $node_props = &$info['node']['properties'];
 
-  for ($i=1; $i <= 5 ; $i++) {
-    $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
-      'type'=>'text',
-      'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
-      'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
+  $fields = msa_get_custom_onto_taxo_searchable_fields();
+
+  foreach ($fields['termname'] as $field) {
+    $node_props[$field['name']] = array(
+      'type'=>$field['type'],
+      'label'=> $field['label'],
+      'getter callback'=>$field['getter']
     );
-    $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
-      'type'=>'text',
-      'label'=> t('Main onthologie term '.$i. ' as tid'),
-      'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
+  }
+  foreach ($fields['tid'] as $field) {
+    $node_props[$field['name']] = array(
+      'type'=>$field['type'],
+      'label'=> $field['label'],
+      'getter callback'=>$field['getter']
     );
   }
 
-  $node_props['materio_search_api_onthologie_term_others_text'] = array(
-    'type'=>'list<text>',
-    'label'=> t('Others onthologie terms as text (+ synonyms)'),
-    'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
-  );
+  // for ($i=1; $i <= 5 ; $i++) {
+  //   $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
+  //     'type'=>'text',
+  //     'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
+  //     'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
+  //   );
+  //   $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
+  //     'type'=>'text',
+  //     'label'=> t('Main onthologie term '.$i. ' as tid'),
+  //     'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
+  //   );
+  // }
+  //
+  // $node_props['materio_search_api_onthologie_term_others_text'] = array(
+  //   'type'=>'list<text>',
+  //   'label'=> t('Others onthologie terms as text (+ synonyms)'),
+  //   'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
+  // );
 
-  $node_props['materio_search_api_onthologie_term_others_tid'] = array(
-    'type'=>'list<text>',
-    'label'=> t('Others onthologie terms as tid'),
-    'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
-  );
+  // $node_props['materio_search_api_onthologie_term_others_tid'] = array(
+  //   'type'=>'list<text>',
+  //   'label'=> t('Others onthologie terms as tid'),
+  //   'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
+  // );
 
   $node_props['materio_search_api_taglibres_text'] = array(
     'type'=>'list<text>',
@@ -211,7 +228,6 @@ function materio_search_api_entity_property_info_alter(&$info){
     'getter callback'=>'materio_search_api_get_taglibres_terms_text',
   );
 
-
   $node_materiau_props = &$info['node']['bundles']['materiau']['properties'];
 
   $node_materiau_props['materio_search_api_node_propertie_companie'] = array(
@@ -220,7 +236,6 @@ function materio_search_api_entity_property_info_alter(&$info){
     'getter callback'=>'materio_search_api_get_node_propertie_companie',
   );
 
-
   // TAXONOMIE TERMS PROPERTIES
 
   $term_props = &$info['taxonomy_term']['properties'];
@@ -239,6 +254,38 @@ function materio_search_api_entity_property_info_alter(&$info){
   // );
 }
 
+function msa_get_custom_onto_taxo_searchable_fields(){
+
+  for ($i=1; $i <= 5 ; $i++) {
+    $fields_name['termname'][] = array(
+      'type' => 'text',
+      'name' => 'materio_search_api_onthologie_term_'.$i.'_text',
+      'label' => t('Main onthologie term '.$i. ' as text (+ synonyms)'),
+      'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_text'
+    );
+    $fields_name['tid'][] = array(
+      'type' => 'text',
+      'name' => 'materio_search_api_onthologie_term_'.$i.'_tid',
+      'label' => t('Main onthologie term '.$i. ' as tid'),
+      'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_tid'
+    );
+  }
+
+  $fields_name['termname'][] = array(
+    'type'=>'list<text>',
+    'name' => 'materio_search_api_onthologie_term_others_text',
+    'label' => t('Others onthologie terms as text (+ synonyms)'),
+    'getter' => 'materio_search_api_get_taxonomy_terms_others_text'
+  );
+  $fields_name['tid'][] = array(
+    'type'=>'list<text>',
+    'name' => 'materio_search_api_onthologie_term_others_tid',
+    'label' => t('Others onthologie terms as tid'),
+    'getter' => 'materio_search_api_get_taxonomy_terms_others_tid'
+  );
+  return $fields_name;
+}
+
 /**
 * taxonomy propoerties as text + synonymes
 */
@@ -750,9 +797,11 @@ function materio_search_api_advanced_search_form($form, &$form_state){
   // global $user;
   // $form = array();
 
-  // TODO: get vid from settings
+  // get vid from settings
   $vid = variable_get('msa-advancedsearchvocabulary', null);
   if($vid){
+    // global $language;
+    // dsm($language, "language");
 
     $voc = taxonomy_vocabulary_load($vid);
     $voc_machinename = $voc->machine_name;
@@ -775,18 +824,33 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     // get the keywords from args and reconstruct the filter lines with default values
     $args_values = array();
     if($path == 'explore/filters' && $args[0] !== "ajax"){
-      $keywords = explode("+", $args[0]);
-      // TODO: what if a keyword contains a +
-      // dsm($keywords, 'keywords');
-      foreach ($keywords as $key => $value) {
-        $name = str_replace('"', '', $value);
-        //Get the term
-        $terms = taxonomy_get_term_by_name($name, $voc_machinename);
-        $term = array_shift($terms);
+      // WORKING WITH TERM NAMES instead of tids
+      // $keywords = explode("+", $args[0]);
+      // // dsm($keywords, 'keywords');
+      // foreach ($keywords as $key => $value) {
+      //   $name = str_replace('"', '', $value);
+      //   //Get the term
+      //   $terms = taxonomy_get_term_by_name($name, $voc_machinename);
+      //   $term = array_shift($terms);
+      //   // dsm($term, $term->tid.' : '.$term->name);
+      //   // get parents
+      //   if(isset($term->tid)){
+      //     $parents = taxonomy_get_parents_all($term->tid);
+      //     // dsm($parents, 'parents');
+      //     // build default values
+      //     for ($p=count($parents)-1; $p >=0 ; $p--) {
+      //       $args_values[$key][] = $parents[$p]->tid;
+      //     }
+      //   }
+      // }
+      // WORKING WITH TIDS instead of term name
+      $tids = explode("+", $args[0]);
+      foreach ($tids as $key => $tid) {
+        $term = taxonomy_term_load($tid);
         // dsm($term, $term->tid.' : '.$term->name);
         // get parents
         if(isset($term->tid)){
-          $parents = taxonomy_get_parents_all($term->tid);
+          $parents = taxonomy_get_parents_all($tid);
           // dsm($parents, 'parents');
           // build default values
           for ($p=count($parents)-1; $p >=0 ; $p--) {
@@ -843,11 +907,14 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     }
 
     // build first level taxonomy tree and select options
-    $taxotree = taxonomy_get_tree($vid, 0, 1, false);
+    $taxotree = taxonomy_get_tree($vid, 0, 1, true);
+    // $taxotree = msa_get_taxo_tree($vid, 0, 1);
+    // dsm($taxotree, "taxotree");
+    // return $form;
     foreach ($taxotree as $index => $term) {
       // TODO: get translated tag name
       if(!in_array($term->tid, $hidden_terms)){
-        $level_0_ops[$term->tid] = t($term->name);
+        $level_0_ops[$term->tid] = $term->name;
       }
     }
 
@@ -909,11 +976,13 @@ function materio_search_api_advanced_search_form($form, &$form_state){
       if($default0){
 
           // build second level taxonomy tree and select options
-          $taxotree1 = taxonomy_get_tree($vid, $default0, 1, false);
+          $taxotree1 = taxonomy_get_tree($vid, $default0, 1, true);
+          // dsm($taxotree1, 'taxotree1');
           $level_1_ops = array();
           foreach ($taxotree1 as $index => $term) {
             // TODO: get translated tag name
-            $level_1_ops[$term->tid] = t($term->name);
+            // $term = taxonomy_term_load($tid);
+            $level_1_ops[$term->tid] = $term->name;
           }
 
           // get the default value
@@ -948,7 +1017,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){
           if($default1){
 
               // build second level taxonomy tree and select options
-              $taxotree2 = taxonomy_get_tree($vid, $default1, 1, false);
+              $taxotree2 = taxonomy_get_tree($vid, $default1, 1, true);
               if(count($taxotree2)){
                 // $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
                 $default2 = null;
@@ -1052,6 +1121,36 @@ function materio_search_api_advanced_search_form($form, &$form_state){
   return $form;
 }
 
+function msa_get_taxo_tree($vid, $parent, $depth){
+  $query = new EntityFieldQuery();
+  $query->entityCondition('entity_type', 'taxonomy_term')
+    ->propertyCondition('vid', $vid);
+    // ->propertyCondition('parent', $parent);
+    // ->propertyCondition('status', 1);
+    // ->entityCondition('bundle', $voc_machinename)
+
+  $result = $query->execute();
+
+  if (isset($result['taxonomy_term'])) {
+    $terms = $result['taxonomy_term'];
+
+    // At first we need to get field's id. If you already know field id, you can ommit this step
+    // Get all fields attached to a given node type
+    $fields = field_info_instances('taxonomy_term');
+
+    // Get id of body field
+    // $field_id = $fields['name']['field_id'];
+
+    // Attach a field of selected id only to get value for it
+    field_attach_load('taxonomy_term', $terms, FIELD_LOAD_CURRENT, array('field_id' => 'name'));
+
+    // Get values of our node field
+    // $output = field_get_items('node', $stories, 'field_story_image');
+  }
+
+  return $terms;
+}
+
 function materio_search_api_advanced_search_select_callback($form, &$form_state) {
   return $form['filters'];
 }
@@ -1078,14 +1177,16 @@ function materio_search_api_advanced_search_form_submit($form, &$form_state){
 
   $values = $form_state['values'];
   $filterlines = isset($form_state['filterlines']) ? $form_state['filterlines'] : 0;
-  $keywords = array();
+  // $keywords = array();
 
   // extract last tag of each lines
   for ($l=0; $l < $filterlines ; $l++) {
     for ($i=2; $i > 0 ; $i--) {
       if(isset($values['filter-'.$l.'-'.$i]) && $values['filter-'.$l.'-'.$i]){
-        $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
-        $keywords[$term->tid] = '"'.$term->name.'"';
+        // $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
+        // $keywords[$term->tid] = '"'.$term->name.'"';
+        // $tids[] = '"'.$values['filter-'.$l.'-'.$i].'"';
+        $tids[] = $values['filter-'.$l.'-'.$i];
         continue 2;
       }else{
         continue;
@@ -1095,7 +1196,7 @@ function materio_search_api_advanced_search_form_submit($form, &$form_state){
 
   // dsm($keywords, 'keywordstid');
 
-  $keywords_str = implode("+", $keywords);
+  $keywords_str = implode("+", $tids);
 
   // dsm($keywords_str);
 

+ 85 - 29
sites/all/modules/gui/materiobasemod/materio_search_api.pages.inc

@@ -346,49 +346,96 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit)
   return $return;
 }
 
+
 function msa_solrquery_filterssearch($keys, $language, $user, $offset, $limit){
+  // dsm($keys, "keys");
+  $keys = explode("+", $keys);
+  foreach ($keys as $value) {
+    $tids[] = intval($value);
+  }
+  // dsm($tids, "tids");
   // dsm($offset, 'offset');
   // dsm($limit, 'limit');
 
+  // init empty results array in wich we will concaten different queries
+  $return = array(
+    "results" => array()
+  );
 
   # define search api solr index
-  $taxo_index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
-  $taxo_index = search_api_index_load($taxo_index_machine_name);
+  $advancedsearch_index_machine_name = variable_get('advancedsearchindex_'.$language->language, -1);
+  // dsm($advancedsearch_index_machine_name);
+  $taxo_index = search_api_index_load($advancedsearch_index_machine_name);
 
   # choose solr query options
-  $query_options = array('conjunction'=>'AND', 'parse mode'=>'terms');
-
-  #create the solr query for taxonomy search
-  $taxo_query = search_api_query($taxo_index_machine_name, $query_options)
-    ->keys($keys);
-    // ->range($offset, $limit);
-
-  # apply bundle options to solr query if usefull
-  if(count($bundles_filter)){
-    $filter = $taxo_query->createFilter('OR');
-    foreach ($bundles_filter as $type)
-      $filter->condition('type', $type, '=');
-
-    $taxo_query->filter($filter);
+  $query_options = array('conjunction'=>'AND');
+
+  // get searchable field names and run one by one the search
+  // eventualy concat all results
+  $fields = msa_get_custom_onto_taxo_searchable_fields();
+  // run a search for each tag (results will be intersected later)
+  foreach ($tids as $i => $tid) {
+    $tmps_results[$i] = array();
+    // run a search for a tag for each field
+    foreach ($fields['tid'] as $field) {
+      # create the solr query for taxonomy search
+      $query = search_api_query($advancedsearch_index_machine_name, $query_options);
+        // ->keys("*:*");
+        // ->range($offset, $limit);
+
+      // https://www.drupal.org/node/2596523
+      $filter = $query->createFilter('AND');
+      $filter->condition($field['name'], $tid);
+      $query->filter($filter);
+
+      # add user access solr query option
+      $query->setOption('search_api_access_account', $user);
+      // $query->setOption('search_api_bypass_access', true);
+
+      # execute
+      $results = $query->execute();
+      // dsm($results, "results");
+
+      // concat each field results for one tag
+      $tmps_results[$i] += $results['results'];
+    }
   }
+  // dsm($tmps_results, "tmp_resuslts");
 
-  // $query->setOption('search_api_bypass_access', true);
 
-  # add user access solr query option
-  $taxo_query->setOption('search_api_access_account', $user);
-
-  // $taxo_query->setOption('q.op', 'OR');
-
-  #execute first time to get the all items, to be able to filter the full text research
-  $taxo_total_results = $taxo_query->execute();
-  // dsm($taxo_total_results, "taxo total results");
+  // do the intersect and score routine
+  if (count($tmps_results) > 1) {
+    // if we have more than one tag
+    $results_intersected = array_intersect_key($tmps_results[0], $tmps_results[1]);
+    if(count($tmps_results > 2)){
+      // if we have more than 2 tags
+      for ($i=2; $i < count($tmps_results); $i++) {
+        $results_intersected = array_intersect_key($results_intersected, $tmps_results[$i]);
+      }
+    }
+    // dsm($results_intersected, 'results_intersected');
+    foreach ($results_intersected as $tid => $v) {
+      $scores = array();
+      for ($j=0; $j < count($tmps_results); $j++) {
+        $i = 0;
+        foreach ($tmps_results[$j] as $key => $vv) {
+          if ($tid === $key) {
+            $scores[] = $i;
+            break;
+          }
+          $i++;
+        }
+      }
+      $results_intersected[$tid]['score'] = array_sum($scores) / count($scores);
+    }
+    uasort($results_intersected, "msa_cmp_filtered_results");
+    // dsm($results_intersected, 'results_intersected scored');
+  }
 
-  $return = array(
-    "results" => $taxo_total_results['results']
-  );
+  $return['results'] = $results_intersected;
 
   # count global results
-  $return['result count'] = count($return['results']);
+  $return['result count'] = count($results_intersected);
 
   # create items array with the good range
   $return['items'] = array();
@@ -409,6 +456,15 @@ function msa_solrquery_filterssearch($keys, $language, $user, $offset, $limit){
 
 }
 
+function msa_cmp_filtered_results($a, $b){
+  // dsm($a, "a");
+  // dsm($b, "b");
+  if ($a['score'] == $b['score']) {
+    return 0;
+  }
+  return ($a['score'] < $b['score']) ? -1 : 1;
+}
+
 
 # not used anymore as free users not exists anymore
 function msa_solrquery_breves($typed, $language, $user, $offset, $limit){

Some files were not shown because too many files changed in this diff